- 此问题在此处已有答案**:
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。
3条答案
按热度按时间px9o7tmv1#
这将是你的答案:
希望现在能起作用...
ijxebb2r2#
您的sql查询是错误的。参考this教程。或者它应该是这样的:-
nzkunb0c3#
您需要根据相应列的类型(INTEGER、TEXT等),将"?"替换为要插入相应列的值。
例如: