Python MySQLdb "INSERT INTO" 问题
Python MySQLdb "INSERT INTO" Issue
我的代码尝试遍历字典,"dbMap",并根据 dbMAP 的键和值执行 MySQL INSERT INTO 语句:
for key in dbMap:
try:
cursor.execute("INSERT INTO '%s' (id, additional_details) VALUES (123, '%s')", (key, dbMap[key]))
except UnicodeEncodeError:
pass
当我 运行 上述代码时出现以下错误:
_mysql_exceptions.ProgrammingError: (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 '''random_key'' (id, additional_details) VALUES (123, 'random_value' at line 1")
我没有看到我违反了什么 MySQL 语法,我正在按照以下资源寻求帮助:
http://www.mikusa.com/python-mysql-docs/query.html
http://www.w3schools.com/sql/sql_insert.asp
更新:当我尝试这个时:
for key in dbMap:
try:
cursor.execute("INSERT INTO {} (id, additional_details) VALUES (123, '{}')".format(key, dbMap[key]))
except UnicodeEncodeError:
pass
我得到一个不同的错误:
_mysql_exceptions.OperationalError: (1054, "Unknown column 'id' in 'field list'")
更新 2:
for key in dbMap:
try:
query = "INSERT INTO `%s` (id, additional_details) VALUES(123, %%s)" % key
cursor.execute(query, dbMap[key])
except UnicodeEncodeError:
pass
出现新错误:TypeError:在字符串格式化期间并非所有参数都已转换
非常感谢任何帮助我找出问题所在的帮助
当使用 Python 的 MySQL 库时,我认为您不想在变量周围使用 '
。我相信他们让 %s
按字面解释,而不是让它们被替换。不过,自从我使用这种方法以来已经有一段时间了,所以我可能会偏离基地。试试这个:
try:
cursor.execute("INSERT INTO %s (id, additional_details) VALUES (123, %s)", (key, dbMap[key]))
Python 会为您代换。
此外,考虑使用抽象来防止 SQL 注入。查看 - this post - SO 中最常查看的内容之一。
另外,由于我正在仔细查看您的 Python 行和您的实际错误,我认为您的行没有按照您的想法行事。
for key in dbMap
将生成 dbMap
的值,而不是键值 - 因此当您调用 dbMap[key]
时,由于 key
不是索引,它正在获取一个错误。这也可能意味着您无法通过 INSERT INTO key
获得您正在寻找的正确 table。引人深思。尝试:
for idx, value in enumerate(dbMap):
cursor.execute("INSERT INTO %s (id, additional_details) VALUES (123, %s)", (idx, dbMap[idx]))
这将允许您访问键索引 (idx
) 和 mapDB
值。
我只是跳进了一个解释器来玩这个,看起来你不能在 MySQLdb API 中替换 table 名称,因为它会将你的替换放在引号中(table names can be backticked but not quoted), 所以你必须分两步完成。 iceberg也对,你不提供自己的报价。
我的样本:
>>> query = "INSERT INTO %s (id, login) VALUES (1972, %%s)" % 'users'
>>> cursor.execute(query, ('TEST-LOGIN123',))
>>> 1L
尝试
for key in dbMap:
try:
query = "INSERT INTO `%s` (id, additional_details) VALUES (123, %%s)" % key
cursor.execute(query, (dbMap[key],))
except UnicodeEncodeError:
pass
mysqldb 库只允许您替换值,而不是表格:
Python and MySQLdb: substitution of table resulting in syntax error
def insert_jugador():
if request.method == 'POST':
apellidos = request.form['apellidos']
nombres = request.form['nombres']
direccion = request.form['direccion']
SqlConsulta = mysql.connection.cursor()
SqlConsulta.execute("INSERT INTO JUGADORES (APELLIDOS, NOMBRES,
DIRECCION, ID_LOCALIDAD) VALUES (%s, %s, %s, 5)", (apellidos,
nombres, direccion))
mysql.connection.commit()
return 'Jugador Insertado Exitosamente...'
我的代码尝试遍历字典,"dbMap",并根据 dbMAP 的键和值执行 MySQL INSERT INTO 语句:
for key in dbMap:
try:
cursor.execute("INSERT INTO '%s' (id, additional_details) VALUES (123, '%s')", (key, dbMap[key]))
except UnicodeEncodeError:
pass
当我 运行 上述代码时出现以下错误:
_mysql_exceptions.ProgrammingError: (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 '''random_key'' (id, additional_details) VALUES (123, 'random_value' at line 1")
我没有看到我违反了什么 MySQL 语法,我正在按照以下资源寻求帮助:
http://www.mikusa.com/python-mysql-docs/query.html
http://www.w3schools.com/sql/sql_insert.asp
更新:当我尝试这个时:
for key in dbMap:
try:
cursor.execute("INSERT INTO {} (id, additional_details) VALUES (123, '{}')".format(key, dbMap[key]))
except UnicodeEncodeError:
pass
我得到一个不同的错误:
_mysql_exceptions.OperationalError: (1054, "Unknown column 'id' in 'field list'")
更新 2:
for key in dbMap:
try:
query = "INSERT INTO `%s` (id, additional_details) VALUES(123, %%s)" % key
cursor.execute(query, dbMap[key])
except UnicodeEncodeError:
pass
出现新错误:TypeError:在字符串格式化期间并非所有参数都已转换
非常感谢任何帮助我找出问题所在的帮助
当使用 Python 的 MySQL 库时,我认为您不想在变量周围使用 '
。我相信他们让 %s
按字面解释,而不是让它们被替换。不过,自从我使用这种方法以来已经有一段时间了,所以我可能会偏离基地。试试这个:
try:
cursor.execute("INSERT INTO %s (id, additional_details) VALUES (123, %s)", (key, dbMap[key]))
Python 会为您代换。
此外,考虑使用抽象来防止 SQL 注入。查看 - this post - SO 中最常查看的内容之一。
另外,由于我正在仔细查看您的 Python 行和您的实际错误,我认为您的行没有按照您的想法行事。
for key in dbMap
将生成 dbMap
的值,而不是键值 - 因此当您调用 dbMap[key]
时,由于 key
不是索引,它正在获取一个错误。这也可能意味着您无法通过 INSERT INTO key
获得您正在寻找的正确 table。引人深思。尝试:
for idx, value in enumerate(dbMap):
cursor.execute("INSERT INTO %s (id, additional_details) VALUES (123, %s)", (idx, dbMap[idx]))
这将允许您访问键索引 (idx
) 和 mapDB
值。
我只是跳进了一个解释器来玩这个,看起来你不能在 MySQLdb API 中替换 table 名称,因为它会将你的替换放在引号中(table names can be backticked but not quoted), 所以你必须分两步完成。 iceberg也对,你不提供自己的报价。
我的样本:
>>> query = "INSERT INTO %s (id, login) VALUES (1972, %%s)" % 'users'
>>> cursor.execute(query, ('TEST-LOGIN123',))
>>> 1L
尝试
for key in dbMap:
try:
query = "INSERT INTO `%s` (id, additional_details) VALUES (123, %%s)" % key
cursor.execute(query, (dbMap[key],))
except UnicodeEncodeError:
pass
mysqldb 库只允许您替换值,而不是表格:
Python and MySQLdb: substitution of table resulting in syntax error
def insert_jugador():
if request.method == 'POST':
apellidos = request.form['apellidos']
nombres = request.form['nombres']
direccion = request.form['direccion']
SqlConsulta = mysql.connection.cursor()
SqlConsulta.execute("INSERT INTO JUGADORES (APELLIDOS, NOMBRES,
DIRECCION, ID_LOCALIDAD) VALUES (%s, %s, %s, 5)", (apellidos,
nombres, direccion))
mysql.connection.commit()
return 'Jugador Insertado Exitosamente...'