【发布时间】:2015-10-13 00:55:19
【问题描述】:
我一直在不知疲倦地尝试解决在python中使用参数化mysql查询的问题,但还没有找到解决方案。
我想做的是这个简单的事情:-
def parameterized_dml_query(self, sql, param):
print sql, param
cursor = self.connection.cursor()
b='wtf'
cursor.execute("""insert into table(schema_name) values %s""", (b,))
self.connection.commit()
logger.info("Row(s) were updated/ inserted :%s for sql : %s",str(cursor.rowcount), sql)
no_of_rows_update = str(cursor.rowcount)
return no_of_rows_update
但我总是得到这个错误:
(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 ''wtf'' at line 1")
我不知道为什么这个... API 会添加单引号。
其他问题:- 1.有什么办法可以参数化table_name。 2. 如果我有一个像
这样的值列表,则插入 NULLlist_of_keys = ''.join(['key1','key2','key3'])
list_of_values =''.join(['v1', None, 'v2'])
sql = """insert into table(%s) values %s"""
param = (list_of_keys, list_of_values)
我调用 parameterized_dml_query 函数它不起作用。那么处理 None 的最佳方法是什么。
【问题讨论】:
标签: python mysql mysql-python