(python)数据库,语法错误
我对数据库是一个全新的人,我已经使用了一些简单的工具,可以在上找到,但是它返回了一个我不理解的错误(python)数据库,语法错误,python,database,Python,Database,我对数据库是一个全新的人,我已经使用了一些简单的工具,可以在上找到,但是它返回了一个我不理解的错误 try: from sqlite3 import dbapi2 as sqlite except ImportError: from pysqlite2 import dbapi2 as sqlite db_connection = sqlite.connect('program.db') db_curs = db_connection.cursor() def create
try:
from sqlite3 import dbapi2 as sqlite
except ImportError:
from pysqlite2 import dbapi2 as sqlite
db_connection = sqlite.connect('program.db')
db_curs = db_connection.cursor()
def create_customer(cID, fname, sname, dob):
db_curs.execute("CREATE TABLE " + cID + " ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
db_curs.execute("INSERT INTO " + cID + " (first_name, last_name, date_of_birth) VALUES (fname, sname, dob)")
db_connection.commit()
db_curs.execute("SELECT * FROM " + cID )
create_customer("1", "John", "Farnham", "12/08/95")
create_customer("1", "Indianna", "Jones", "05/05/95")
print db_curs.fetchall()
我收到的错误如下:
Traceback (most recent call last):
File "C:\Users\fin0005\Documents\loyalty.py", line 17, in <module>
create_customer("1", "John", "Farnham", "12/08/95")
File "C:\Users\fin0005\Documents\loyalty.py", line 12, in create_customer
db_curs.execute("CREATE TABLE " + cID + " ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
OperationalError: near "1": syntax error
回溯(最近一次呼叫最后一次):
文件“C:\Users\fin0005\Documents\loyalty.py”,第17行,在
创建客户(“1”、“John”、“Farnham”、“12/08/95”)
文件“C:\Users\fin0005\Documents\loyalty.py”,第12行,在create\u customer中
db_curs.execute(“创建表”+cID+”(id整数主键,名字VARCHAR(20),姓氏VARCHAR(30),出生日期)
操作错误:接近“1”:语法错误
在表名周围添加反勾号,这样它就不会认为它正在创建一个整数作为表名
def create_customer(cID, fname, sname, dob):
db_curs.execute("CREATE TABLE `" + cID + "` ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
db_curs.execute("INSERT INTO `" + cID + "` (first_name, last_name, date_of_birth) VALUES (fname, sname, dob)")
db_connection.commit()
db_curs.execute("SELECT * FROM `" + cID + "`")
# In SQL terms, the following blows up
# create table 2 (id int(10) PRIMARY KEY); Due to the 2 being an integer
# create table `2` (id int(10) PRIMARY KEY); Works, due to the 2 being properly identified with backticks :)
# Here's some code as requested in the comment, everything below this point is a self contained example, please do not copy the function above
def initiate_customer_table(table_name):
db_curs.execute("CREATE TABLE IF NOT EXISTS `" + table_name + "` ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
db_connection.commit()
def create_customer(table_name, fname, sname, dob):
db_curs.execute("INSERT INTO `" + table_name + "` (first_name, last_name, date_of_birth) VALUES (%s, %s, %s)", [fname, sname, dob])
db_connection.commit()
# Fetches the user just created
db_curs.execute("SELECT * FROM `" + table_name + "` WHERE id = %s", [db_curs.insert_id()])
# Returns the user
return db_curs.fetchone()
desired_table_name = 'customers'
initiate_customer_table(desired_table_name)
customer_1 = create_customer(desired_table_name, "Bryan", "Moyles", "1800-01-01")
customer_2 = create_customer(desired_table_name, "Awesome", "Guy", "1800-01-01")
如果您计划在生产中使用此代码,我还建议您更进一步,以确保mysql正确转义所有字段。为什么要调用表“1”?!另外请注意,第二个
create_customer(“1”、“Indianna”、“Jones”、“05/05/95”)
将失败,1
绝对不是一个好的表名。OP可能会混淆表名和主键列,这很好!事实上,由于1已经被用作表名,所以它确实会被接受。我还将支持@okm,并说整数作为表名根本不是好的做法。您希望在创建表时具有描述性,您的create_customer方法实际上应该只负责向customer表中添加一条记录,而不是创建整个表。我看不到您复制了我为INSERT提供的行,请复制我提供的整个函数,然后重试:)哦!我理解你所做的,@BryanMoyles:)谢谢你的帮助!但是,我似乎仍然对该数据库的正确使用感到有点困惑,因为我收到了错误:没有这样的列:John-您是否愿意提供一个示例(或教程)来向customer表中添加一条记录,而不是为每个客户创建一个单独的表?再次感谢你们(两位)的帮助!:)不客气,谢谢你的积极能量!我已经在原始代码中添加了一个示例,在该示例中,您启动一次表,然后在该表中创建客户。尝试使用时,请仅将代码复制到新代码下方:)