sqlite3.OperationalError:靠近"(":语法错误Python "SQL Lite [重复]

ddrv8njm  于 2023-01-13  发布在  SQLite
关注(0)|答案(3)|浏览(171)
    • 此问题在此处已有答案**:

Passing SQLite variables in Python(1个答案)
How to use variables in SQL statement in Python?(5个答案)
2天前关闭。
我有一个小问题的一段代码,我复制它从一个网站,但我有以下错误:
sqlite3.OperationalError:靠近"(":语法错误
代码如下所示:

# 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()

解决方案是什么?我已经搜索了整个堆栈溢出,但我没有找到解决方案
THX
有什么解决办法吗?或者解释这个对我来说是隐藏的错误?
已更正的新错误...
sqlite3.ProgrammingError:提供的绑定数不正确。当前语句使用0,但提供了1。

px9o7tmv

px9o7tmv1#

这将是你的答案:

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)

希望现在能起作用...

ijxebb2r

ijxebb2r2#

您的sql查询是错误的。参考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)"""
nzkunb0c

nzkunb0c3#

您需要根据相应列的类型(INTEGER、TEXT等),将"?"替换为要插入相应列的值。
例如:

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

相关问题