Python MYSQLdb如何使用不同的参数执行insert
如何使用不同的参数执行mysqldb插入? 这个例子说:Python MYSQLdb如何使用不同的参数执行insert,python,insert,mysql-python,Python,Insert,Mysql Python,如何使用不同的参数执行mysqldb插入? 这个例子说: add_employee = ("INSERT INTO employees " "(first_name, last_name, hire_date, gender, birth_date) " "VALUES (%s, %s, %s, %s, %s)") data_employee = ('Geert', 'Vanderkelen', tomorrow, 'M', date
add_employee = ("INSERT INTO employees "
"(first_name, last_name, hire_date, gender, birth_date) "
"VALUES (%s, %s, %s, %s, %s)")
data_employee = ('Geert', 'Vanderkelen', tomorrow, 'M', date(1977, 6, 14))
cursor.execute(add_employee, data_employee)
我想做的是
cursor.execute("INSERT INTO %s (%s) VALUES (%s)", ('animals', 'name', 'fox'))
但我犯了个错误
MySQL Error [1064]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''animals' ('name') VALUES ('fox')' at line 1
我知道MYSQLdb的格式化程序工作不正常,有办法解决吗?那么,有没有可能做到这一点呢
cursor.execute("INSERT INTO %s (%s) VALUES (%s)", ('animals', ('name','color'), ('fox', 'orange'))
编辑:请不要假设所有插入的数据都是字符串。我还希望能够通过这些查询传递BLOB数据
imageofafox = open('fox.jpg', 'rb').read()
sql = "INSERT INTO %s (%s) VALUES (%s)"
cursor.execute(sql, ('animals', 'name, picture', ('fox', imageofafox)))
//编辑:这是针对JavaMySQL的 数据类型错误,数据元组中需要字符串或数字类型
//data_employee = ('Geert', 'Vanderkelen', tomorrow, 'M', date(1977, 6, 14))
data_employee = ('Geert', 'Vanderkelen', 'no idea what tomorrow was', 'M', 'date(1977, 6, 14)')
cursor.execute将自动引用所有给定参数,因此您的查询最初可能无法工作,因为引用的表名和字段名: 只有在使用pythons内置%而不是时,才应将值包装为“以确保:
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % ('animals', 'name', "'fox'"))
如果要包含多个字段和值,请记住将它们作为三个字符串传递,数字和其他值也是自动引用的。mysql将处理数据键入:
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % ('animals', 'name, color', "'fox', 'orange'"))
您可以使用打印和%
print "INSERT INTO %s (%s) VALUES (%s)" % ('animals', 'name, color', "'fox', 'orange'")
但据我所知,不能将数组作为单个参数传递到execute中,只能传递一个参数列表,因此“动物”、“名称”、“颜色”。。。不行
下面是一个完整的脚本,用于测试和找出它在您的环境中不起作用的原因,因为它在我的环境中确实起作用:
import mysql.connector
connection = mysql.connector.connect(user='root', password='', host='127.0.0.1', database='test')
cursor = connection.cursor()
sql = "INSERT INTO %s (%s) VALUES (%s)"
arg = ('animals', 'name', "'fox'")
cursor.execute('set profiling = 1')
try:
cursor.execute(sql % arg)
connection.commit()
except:
cursor.execute('show profiles')
for row in cursor:
print(row)
raise
connection.close()
说明:如果使用cursor.executesql、args,则函数将自动引用所有值。因为您的SQL不仅包含%s的值,还包含表名和字段名,所以不能让它们自动引用,否则SQL将失败。如果使用cursor.executesql%args,则必须自己将引号添加到值中,但查询不会失败,因为表名和字段名没有引号。如果示例有效,可以将其用作示例
add_animals = ("INSERT INTO %s "
"(%s) "
"VALUES (%s)")
data_animals = ('animals', 'name', 'fox')
cursor.execute(add_animals, data_animals)
确保使用commit将数据提交到数据库我的示例不起作用,因此问题中提到的错误不起作用,您的第一个代码导致MySQL错误[1064]:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解在第1sorry行使用“name”值“\'fox\”的正确语法,忘记了游标的自动引号。execute,因为我与%一起使用,而不是与一起使用,所以错误在其他地方…我的初始查询不起作用,因此出现了question@ValeraP. 您没有正确阅读我的解决方案如果您使用逗号,那么表名和字段名将放在“中”,这会引发错误,请使用%并仅在值字段中添加引号,然后您的查询工作!我不能正确地测试它,但我猜这是唯一明显的区别,因为您使用的是mysql.connector,而我使用的是mysqldb。