-2

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.

AGS
  • 1
  • 3
  • it should be `INSERT INTO isaCommune ([list of column names]) VALUES ([list of values])` you need to fill the list of column names and list of values. – Sembei Norimaki Jan 10 '23 at 10:27
  • Error sqlite3.OperationalError: table isaCommune has no column named 'id_codedep_codecommune', 'nom_commune', 'code_postal', 'code_commune', 'code_departement', 'nom_departement', 'code_region' 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'])" – AGS Jan 10 '23 at 10:35

2 Answers2

-1

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

Pickymtr
  • 74
  • 6
  • I am reading from a csv, maybe an error there? id_codedep_codecommune;nom_commune;code_postal;code_commune;code_departement;nom_departement;code_region 1001;L ABERGEMENT CLEMENCIAT;1400;1;1;Ain;84 – AGS Jan 10 '23 at 10:39
  • insert_records = "INSERT INTO isaCommune VALUES (id_codedep_codecommune, 'nom_commune', code_postal, code_commune, code_departement, 'nom_departement', code_region) ('id_codedep_codecommune', 'nom_commune', 'code_postal', 'code_commune', 'code_departement', 'nom_departement', 'code_region') " sqlite3.OperationalError: near "(": syntax error – AGS Jan 10 '23 at 10:43
-1

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...

Manvi
  • 171
  • 11