highscore= score
cursor.execute("insert into tble values (hscore) hishscore.getvalue"):
que: score will save into variable highscore. That highscore needs to save on to the database in the field hscore. What is the correct code for insertion and getting value.
You want to bind the parameter using the ? placeholder:
cursor.execute("INSERT INTO tble (hscore) VALUES (?)", highscore)
If you wanted to insert multiple values, here's a longer form:
cursor.execute(
"""
INSERT INTO table_name
(column_1, column_2, column_3)
VALUES (?, ?, ?)
""",
(value_1, value_2, value_3)
)
Your order of VALUES was out of place as well. Good luck!
cursor.execute("insert into tablename(column1,column2) values (?,?);",var1,var2)
I needed the semicolon for it to work for me.
Assuming the column name is 'hscore', and the variable with the value to be inserted is 'highscore':
cursor.execute("insert into tablename([hscore]) values(?)", highscore)
you can follow the below code this is going write column values from csv , good example for your use case
import pyodbc
import io
#credential file for server,database,username,password
with io.open('cred.txt','r',encoding='utf-8')as f2:
cred_data=f2.read()
f2.close()
cred_data=cred_data.split(',')
server=cred_data[0]
database=cred_data[1]
username=cred_data[2]
pwd=cred_data[3]
con_obj=pyodbc.connect("DRIVER={SQL Server};SERVER="+server+";DATABASE="+database+";UID="+username+";PWD="+pwd)
data_obj=con_obj.cursor()
#data file with 5 columns
with io.open('data.csv','r',encoding='utf-8')as f1:
data=f1.read()
f1.close()
data=data.split('\n')[1:]
i=1001
for row in data:
lines=row.split(',')
emp=i
fname=lines[0].split(' ')[0]
sname=lines[0].split(' ')[1]
com=lines[1]
dep=lines[2]
job=lines[3]
email=lines[4]
data_obj.execute("insert into dbo.EMP(EMPID,FNAME,SNAME,COMPANY,DEPARTMENT,JOB,EMAIL) values(?,?,?,?,?,?,?)", emp,fname,sname,com,dep,job,email)
con_obj.commit()
i=i+1
Related
I've been trying to learn how to use sqlite3 for python 3.10 and I can't find any explanation of how I'm supposed to grab saved data From a database and insert it into a variable.
I'm attempting to do that myself in this code but It just prints out
<sqlite3.Cursor object at 0x0000018E3C017AC0>
Anyone know the solution to this?
My code is below
import sqlite3
con = sqlite3.connect('main.db')
cur = con.cursor()
#Create a table called "Datatable" if it does not exist
cur.execute('''CREATE TABLE IF NOT EXISTS datatable
(Name PRIMARY KEY, age, pronouns) ''')
# The key "PRIMARY KEY" after Name disallow's information to be inserted
# Into the table twice in a row.
name = 'TestName'#input("What is your name? : ")
age = 'TestAge'#input("What is your age? : ")
def data_entry():
cur.execute("INSERT INTO datatable (name, age)")
con.commit
name = cur.execute('select name from datatable')
print(name)
Expected result from Print(name) : TestName
Actual result : <sqlite3.Cursor object at 0x00000256A58B7AC0>
The execute statement fills the cur object with data, but then you need to get the data out:
rows = cur.fetchall()
for row in rows:
print(row)
You can read more here: https://www.sqlitetutorial.net/sqlite-python/sqlite-python-select/
I need help on the query to insert a value from one table to another. I have three tables, two contains email transaction volume data (Email_Transaction_Volume_13052020 and Email_Transaction_Volume_14052020) and another one in which i need to capture the volume for each subs to (daily_transaction_monitoring_working). Below is my query.
insert into daily_transaction_monitoring_working (Carry_Forward )
select sum(case_count_sum)
from Email_Transaction_Volume_13052020 as b
where
b.status in ('Assigned', 'Awaiting Response', 'Internal Hold') and
b.Email_Source_Case = 'ssc.tseparts'
insert into daily_transaction_monitoring_working (Incoming)
select sum(case_count_sum)
from Email_Transaction_Volume_14052020 as b
where
b.status in ('Closed', 'No Response Required') and
b.Email_Source_Case = 'ssc.tseparts';
However, this resulted into inserting the value incoming and and carry forward into two rows (as shown below). Meanwhile I need it to be on the same row. Could anyone please help me with this. Thank you in advance.
You may provide two subqueries as the source of data for the record you want to insert:
INSERT INTO daily_transaction_monitoring_working (Carry_Forward, Incoming)
(
SELECT SUM(case_count_sum)
FROM Email_Transaction_Volume_13052020
WHERE
status IN ('Assigned', 'Awaiting Response', 'Internal Hold') AND
Email_Source_Case = 'ssc.tseparts'
),
(
SELECT SUM(case_count_sum)
FROM Email_Transaction_Volume_14052020
WHERE
status IN ('Closed', 'No Response Required') AND
Email_Source_Case = 'ssc.tseparts'
);
How to load .jsonl into a table variant as json of snowflake
create or replace table sampleColors (v variant);
insert into
sampleColors
select
parse_json(column1) as v
from
values
( '{r:255,g:12,b:0} {r:0,g:255,b:0} {r:0,g:0,b:255}')
v;
select * from sampleColors;
Error parsing JSON: more than one document in the input
If you want each RGB value in its own row, you need to split the JSONL to a table with one row per JSON using a table function like this:
insert into
sampleColors
select parse_json(VALUE)
from table(split_to_table( '{r:255,g:12,b:0} {r:0,g:255,b:0} {r:0,g:0,b:255} {c:0,m:1,y:1,k:0} {c:1,m:0,y:1,k:0} {c:1,m:1,y:0,k:0}', ' '));
i want match string in json string that like:
"ids":[44,53,1,3,12,45]
i want run query in sqlite send only one digit as id and match one of the above id in sql statement
i write this regex "ids":[\[] for matching start of key
but i don't have any idea to match middle id and escape starting id
example:
i have calc_method table like this:
CREATE TABLE "calc_method" (
"calc_method_id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"calc_method_name" TEXT NOT NULL,
"calc_method_value" TEXT NOT NULL
);
in calc_method_value column i store calcMethod class which convert to json using Gson
class calcMethod{
var memberCafeIds:ArrayList<Long>,
var memberBarIds:ArrayList<Long>
}
after i convert calcMethod to json i have output like below and this value store in calc_method_value column:
{"memberCafeIds":[1,2,14,5,44],"memberBarIds":[23,1,5,78]}
now i want select row that match to my regex pattern like if calc_method_value column have memberBarIds with id 1
SELECT * FROM calc_method WHERE calc_method_value REGEXP '"memberCafeIds":\[[:paramId]'
:paramId is method parameter
Regards, a programmer struggle with regex
In Sqlite, use JSON1 functions to work with JSON, not regular expressions. In particular, json_each() to turn the JSON array into a table you can query:
sqlite> CREATE TABLE ex(json);
sqlite> INSERT INTO ex VALUES ('{"ids":[44,53,1,3,12,45]}');
sqlite> SELECT * FROM ex WHERE 1 IN (SELECT value FROM json_each(ex.json, '$.ids'));
json
-------------------------
{"ids":[44,53,1,3,12,45]}
sqlite> SELECT * FROM ex WHERE 50 IN (SELECT value FROM json_each(ex.json, '$.ids'));
sqlite>
Let's say I have the following dataset imported from a textfile:
Data
--------------------
1,"John Davis","Germany"
2,"Mike Johnson","Texas, USA"
3,"Bill "The man" Taylor","France"
I am looking for a way to remove every " in the data, unless it's followed or preceded by a ,.
So in my case, the data should become:
Data
--------------------
1,"John Davis","Germany"
2,"Mike Johnson","Texas, USA"
3,"Bill The man Taylor","France"
I tried it with the import tekst file component in SSIS, but that gives an error when I set the column delimiter to ". If I don't set a delimiter, it sees the comma in "Texas, USA" as a split delimiter....
Any suggestions/ideas? The textfile is too large to change this manually for every line so that's not an option.
Bit of a cop-out on the last '"', but:
Create table #test ([Data] nvarchar(max))
insert into #test values ('1,"John Davis","Germany"' )
insert into #test values ('2,"Mike Johnson","Texas, USA"' )
insert into #test values ('3,"Bill "The man" Taylor","France"')
select replace(replace(replace(replace([Data],',"',',~'), '",','~,'),'"', ''),'~','"') + '"'
from #test