sqlite3.OperationalError: near "(": syntax error Python " SQL Lite [duplicate] - python

This question already has answers here:
Passing SQLite variables in Python
(1 answer)
How to use variables in SQL statement in Python?
(5 answers)
Closed last month.
I have a small problem with a piece of code, I copied it from a web, but I have the following error:
sqlite3.OperationalError: near "(": syntax error
The code is the following:
# Import required modules
import csv
import sqlite3
# Connecting to the geeks database
connection = sqlite3.connect('isaDBCommune.db')
# Creating a cursor object to execute
# SQL queries on a database table
cursor = connection.cursor()
# Table Definition
create_table = '''CREATE TABLE IF NOT EXISTS isaCommune(
id_codedep_codecommune INTEGER NOT NULL,
nom_commune TEXT NOT NULL,
code_postal INTEGER NOT NULL,
code_commune INTEGER NOT NULL,
code_departement INTEGER NOT NULL,
nom_departement TEXT NOT NULL,
code_region INTEGER NOT NULL
)'''
# Creating the table into our
# database
cursor.execute(create_table)
# Opening the person-records.csv file
file = open('commune.csv')
# Reading the contents of the
# person-records.csv file
contents = csv.reader(file)
# SQL query to insert data into the
# person table
insert_records = "INSERT INTO isaCommune (id_codedep_codecommune, nom_commune, code_postal, code_commune, code_departement, nom_departement, code_region) VALUES ('id_codedep_codecommune', 'nom_commune', 'code_postal', 'code_commune', 'code_departement', 'nom_departement', 'code_region')"
# Importing the contents of the file
# into our person table
cursor.executemany (insert_records, contents)
# SQL query to retrieve all data from
# the person table To verify that the
# data of the csv file has been successfully
# inserted into the table
select_all = "SELECT * FROM isaCommune"
rows = cursor.execute(select_all).fetchall()
What would be the solution? I have searched all over Stack Overflow and I can't find the solution
THX
Any solution ? Or explanation to this error that for me is hidden?
New error with correction ...
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

This will be your answer:-
import csv
import sqlite3
connection = sqlite3.connect('isaDBCommune.db')
cursor = connection.cursor()
create_table = '''CREATE TABLE IF NOT EXISTS isaCommune(
id_codedep_codecommune TEXT NOT NULL,
nom_commune TEXT NOT NULL,
code_postal TEXT NOT NULL,
code_commune TEXT NOT NULL,
code_departement TEXT NOT NULL,
nom_departement TEXT NOT NULL,
code_region TEXT NOT NULL
)'''
cursor.execute(create_table)
file = open('commune.csv')
contents = csv.reader(file)
for l in contents:
insert_records = """INSERT INTO isaCommune ('id_codedep_codecommune', 'nom_commune', 'code_postal','code_commune','code_departement','nom_departement','code_region')
VALUES(?,?,?,?,?,?,?)"""
a = (l[0],l[1],l[2],l[3],l[4],l[5],l[6],)
cursor.execute(insert_records, a)
select_all = "SELECT * FROM isaCommune"
rows = cursor.execute(select_all).fetchall()
for row in rows:
print(row)
Hope it will work now...

You need to replace the '?' by the value you want to insert in the corresponding column depending on its type INTEGER, TEXT etc..
For example:
insert_records = "INSERT INTO isaCommune VALUES(1, 'test', 1, 1, 1, 'test', 1) ('id_codedep_codecommune', 'nom_commune', 'code_postal', 'code_commune', 'code_departement', 'nom_departement', 'code_region')"

Related

mysql LOAD DATA INFILE of csv from python (Not Working)

After some data manipulation I store two columns in a txt file in a csv format as following:
result.txt ->
id,avg
0,38.0
1,56.5
3,66.5
4,48.666666666666664
then I store the data in a table, which is where i find trouble, i tried running a .sql query that stores the data successfully, but executing the same query from python doesnt seem to work for some reason.
python code->
.
.
.
open('C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/result.txt', 'w').write(res)
print(res)
try:
with mysql.connector.connect(
host="localhost",
user='root',
password='tt',
database="dp",
) as connection:
clear_table_query = "drop table if exists test_db.marks;"
create_table_query = '''
create table test_db.marks (
id varchar(255) not null,
avg varchar(255) not null,
primary key (id)
);
'''
# droping the table and recreating it works fine
add_csv_query = "LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/result.txt' INTO TABLE marks FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES;"
print(add_csv_query) # query is printed correctly
with connection.cursor() as cursor:
cursor.execute(clear_table_query)
cursor.execute(create_table_query)
cursor.execute(add_csv_query)
cursor.execute("SELECT * FROM test_db.marks;") # this produces -> Unread result found
except mysql.connector.Error as e:
print(e)
connection.close()

Python, Oracle_cx, using query result as parameter list into looping insert statement

In the past I wrote a pl/sql script that takes a table name and a column name (that indicates source) as arguments and then profiles all the columns in the table giving useful counts.
I'm currently teaching myself python and am re-writing that pl/sql script in a way that can be executed against other sql databases, not just oracle. So I am new to Python. I'm going through Automate the Boring Stuff on Udemy. At the moment I'm not concerned with sql injection because I'm just learning the Python language. I have left out the create table statements to reduce the amount of code I'm pasting.
The script is inserting the correct records on the first pass of the loop however it does not start the 2nd loop. Here is the IDLE output, and then the code.
================================================ RESTART: C:\Users\nathan\Documents\_work\_data_profiling_script\profiling_python_tester.py ================================================
('ETL_INS_DTM',)
insert into PROFILING_NWS6_PRT
select 'PROFILING_NWS6', 'ETL_INS_DTM', SRCRECNO, count(*), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null
from PROFILING_NWS6
group by SRCRECNO
order by 1,2,3
executed
committed
**Traceback (most recent call last):
File "C:\Users\nathan\Documents\_work\_data_profiling_script\profiling_python_tester.py", line 39, in <module>
for row in cursor:
cx_Oracle.InterfaceError: not a query**
import cx_Oracle
conn = cx_Oracle.connect("system", "XXXX", "localhost/xe")
cursor = conn.cursor()
## parameter declaration
##########################################################################
# These 2 parameters populated by user
v_st = 'PROFILING_NWS6' # Source Table - table in which we are profiling the data
v_srcno = 'SRCRECNO' # Source Number - numeric column in v_st that identifies the source system
# These 3 parameters automatically populated
v_prt = v_st + '_PRT' # Profile Report Table - table name we want our report created as
v_log = v_st + '_LOG' # Log Table - script logging goes here, used for monitoring and debugging
v_top = v_st + '_TOP' # Top Table - temporary table to hold top 5 counts
# write script that populates Profile Report Table with rows for each source/column combination from source table
# these are required to join to when updating analysis fields
##########################################################################
sql = "Select column_name from user_tab_columns where table_name = '"+ v_st + "' and column_name <> '" + v_srcno + "'"
cursor.execute(sql)
for row in cursor:
print(row)
sql = """insert into {x_prt}
select '{x_st}', '{x_row}', {x_srcno}, count(*), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null
from {x_st}
group by {x_srcno}
order by 1,2,3""".format(x_prt = v_prt, x_srcno = v_srcno, x_st = v_st, x_row = row[0])
print(sql)
cursor.execute(sql)
print('executed')
cursor.execute('commit')
print('committed')
#close connections
##########################################################################
cursor.close()
conn.close()
The cursor in for row in cursor: is still in use until the loop completes. When you do cursor.execute(sql) inside the loop, it changes the cursor object. So on the 2nd loop, the cursor item being iterated is the cursor from the commit inside the loop. Solution is to create or use a different cursor object inside the loop.
cursor = conn.cursor() # original cursor, as above
insert_cursor = conn.cursor() # new one for insert
sql = "Select column_name from user_tab_columns where table_name " # etc
for row in cursor.execute(sql):
print(row)
sql = """second sql""".format(...)
print(sql)
insert_cursor.execute(sql)
print('executed')
insert_cursor.execute('commit')
print('committed')
cursor.close()
insert_cursor.close()
conn.close()
Also, for row in cursor: should be for row in cursor.fetchall():. Or just
for row in cur.execute(sql):.
This coding architecture will do a lot of 'round-trips' between Python & the DB, so it will be far from optimal. Small improvements include using connection.autocommit instead of a full SQL commit (or connection.commit() call). Then you could look at using executemany() instead of multiple execute() calls. Overall, for Oracle, just use a PL/SQL call since this will take just one round-trip.

Cannot to insert record into table Incorrect number of bindings supplied

Cannot to insert into ShopifyMonitor table record (there 2 field: id, name)
full traceback of error:
File "D:\Related To Python (Tutorials)\Python-Test\Working With Database\goo.py", line 174, in <module>
c.execute(make_shopify_name, (shopify_name))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 10 supplied.
.
get_site = str(input('Enter site here: '))
url = fix_url(get_site)
shopify_name = fix_url(get_site, True)
basepath = os.path.dirname(__file__)
db_name = '{}/shopify.sqlite3'.format(basepath)
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS ShopifyMonitor (
id integer PRIMARY KEY AUTOINCREMENT,
name text UNIQUE NOT NULL
);"""
sql_create_tasks_table = """ CREATE TABLE IF NOT EXISTS Product (
id integer PRIMARY KEY AUTOINCREMENT,
product_id text NOT NULL,
updated_at text NOT NULL,
title text NOT NULL,
link_to_product text UNIQUE NOT NULL,
vendor text NOT NULL,
sku text NOT NULL,
quantity text NOT NULL,
options text,
price text NOT NULL,
collection_id text,
collection_updated text,
shopify_name text NOT NULL,
FOREIGN KEY(shopify_name) REFERENCES ShopifyMonitor(name)
);"""
make_shopify_name = '''INSERT INTO ShopifyMonitor(name) VALUES (?) '''
conn = create_connection(db_name)
if conn is not None:
# create projects table
create_table(conn, sql_create_projects_table)
# create tasks table
create_table(conn, sql_create_tasks_table)
else:
print("Error! cannot create the database connection.")
c = conn.cursor()
c.execute(make_shopify_name, (shopify_name))
conn.commit()
It looks like your post is mostly code; please add some more details.add some more details.
The issue is subtle:
c.execute(make_shopify_name, (shopify_name))
Should be:
c.execute(make_shopify_name, (shopify_name,)) # note comma after shopify_name
The second parameter passed into execute should be a tuple of parameters for the query - even if there's only one parameter, it still has to be a tuple.
At the moment all you have is parentheses around a variable name - the parentheses will basically be ignored by Python as they don't mean anything.
It's a common misconception that it's the parentheses that make a tuple - it's not, it's the comma:
x = (1) # x is 1
x = 1, # x is a tuple containing a single value, the integer 1
x = (1,) # as above - but the parentheses aren't actually required syntactically here

psycopg2 adding quotes around my string that is creating problems

I have the following script:
create_table_WAC = """
create table if not exists %s (
w_geocode text,
C000 text,
CFS04 text,
CFS05 text,
createdate text
)
"""
target_directory = Path(sys.argv[1]).resolve()
for file in target_directory.rglob('*.csv'):
table_name = 'opendata_uscensus_lodes_' + str(file.stem)
print(table_name)
# MAKE SURE THIS IS THE RIGHT TABLE FOR THE FILES
cur.execute(create_table_WAC, (table_name,))
with open(file,'r') as file_in:
# MAKE SURE THIS HAS THE RIGHT TABLE NAME IN THE COPY STATEMENT
cur.copy_expert("copy %s from stdin with csv header delimiter ','", table_name, file_in)
conn.commit()
conn.close()
When I run it, it throws this error related to the CREATE TABLE command. I don't understand why there are '' added -- and how do I remove them?
Here is the error:
psycopg2.ProgrammingError: syntax error at or near "'opendata_uscensus_lodes_ca_wac_SA02_JT03_2003'"
LINE 2: create table if not exists 'opendata_uscensus_lodes_ca_wac_S...
Use SQL string composition:
from psycopg2 import sql
create_table_WAC = """
create table if not exists {} ( -- note changed placeholder
w_geocode text,
C000 text,
CFS04 text,
CFS05 text,
createdate text
)
"""
# ...
cur.execute(sql.SQL(create_table_WAC).format(sql.Identifier(table_name)))
Read the comprehensive explanation in the documentation.

syntax error unexpected character after line continuation character

Can anybody tell me what's wrong in my program? When I run this program I get the following error message:
syntaxerror unexpected character after line continuation character
import sqlite3
sqlite_file = 'my_first_db.sqlite' # NAME OF THE SQL DATABASE FILE
table_name1 = 'my_table_1' # NAME OF THE TABLE THAT TO BE CREATED.
table_name2 = 'my_table_2' # NAME OF THE SECOND TABLE THAT TO BE CREATED.
new_filed = 'my_1st_coulmn' # NAME OF THE COULMN
filed_type = 'INTEGER' # COULMN DATA TYPE
# CONNECTING TO DATA BASE FILE
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
# CREATEING NEW SQLITE TABLE WITH 1 COULMN
c.execute('create table {tn} ({nf}) {ft})'\ .format(tn=table_name1,nf=new_filed,ft=filed_type))
# Creating a second table with 1 column and set it as PRIMARY KEY
# note that PRIMARY KEY column must consist of unique values!
c.execute('create table {tn} ({nf}) {ft} primary key)'\.format(tn=table_name2,nf=new_filed,ft=filed_type))
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()
\ is used for line continuation while writing long queries.
so just remove \ before .format() at creation and execution of query if you continued the code after .
# CREATEING NEW SQLITE TABLE WITH 1 COULMN
c.execute('create table {tn} ({nf}) {ft})'.format(tn=table_name1,nf=new_filed,ft=filed_type))
and for more info read this.. https://pyformat.info/

Categories