sqlite3.OperationalError: near "(": syntax error Python " SQL Lite

Question:

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.

Asked By: AGS

||

Answers:

Your SQL query appears to be wrong.

INSERT INTO isaCommune VALUES(?,?,?, ?,?,?,?)     
('id_codedep_codecommune', 'nom_commune', 'code_postal', 'code_commune', 'code_departement', 'nom_departement', 'code_region')

values should be behind the (?,?,?,..).

Also, the names of the columns have to come first, than the values. So (names columns) values (values).

Answered By: FreddyV

Your sql query is wrong..
Refer this tutorial..
Or it should be like this:-

"""INSERT INTO isaCommune  ('id_codedep_codecommune', 'nom_commune', 'code_postal', 'code_commune', 'code_departement', 'nom_departement', 'code_region')
VALUES(1,'test',1001,10012,12,'test',1236)"""
Answered By: Manvi

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')" 

Answered By: Pickymtr

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…

Answered By: Manvi