python 插入 sql 错误
python insert into sql error
当我尝试通过 SQL 将 python 代码中收集的一些值插入我的数据库时,以下 python 代码出现 SQL 错误.我在以粗体突出显示的代码中收到 SQL 错误。感谢任何帮助!
def add_ingredients(cursor) :
while True:
ingredient = raw_input("Name of ingredient (q to quit):")
if ingredient.lower() !='q':
num = raw_input("Number in storage: ")
description = raw_input("description: ")
sql = '''insert into ingredients
(title, amount, description)
values
**(:title, :amount, :description)'''**
cursor.execute(sql,
{"title":ingredient, "amount":num, "description":description})
print "Added!"
else:
print "Okay, quitting."
break
MySQL 连接器不期望这样的命名参数。如 the documentation 所示,您应该使用 %(var_name)s
格式进行字典插值:
sql = '''insert into ingredients (title, amount, description)
values (%(title)s, %(amount)s, %(description)s)'''
cursor.execute(sql, {"title": ingredient, "amount": num, "description": description})
当我尝试通过 SQL 将 python 代码中收集的一些值插入我的数据库时,以下 python 代码出现 SQL 错误.我在以粗体突出显示的代码中收到 SQL 错误。感谢任何帮助!
def add_ingredients(cursor) :
while True:
ingredient = raw_input("Name of ingredient (q to quit):")
if ingredient.lower() !='q':
num = raw_input("Number in storage: ")
description = raw_input("description: ")
sql = '''insert into ingredients
(title, amount, description)
values
**(:title, :amount, :description)'''**
cursor.execute(sql,
{"title":ingredient, "amount":num, "description":description})
print "Added!"
else:
print "Okay, quitting."
break
MySQL 连接器不期望这样的命名参数。如 the documentation 所示,您应该使用 %(var_name)s
格式进行字典插值:
sql = '''insert into ingredients (title, amount, description)
values (%(title)s, %(amount)s, %(description)s)'''
cursor.execute(sql, {"title": ingredient, "amount": num, "description": description})