-2

我已经创建了一个 mysql 表,例如----> create table test(gender char(1));

我的 python sudo 代码是---->

from pymysql import *
g='m'
sql='insert into test values(%s)' %g
cur.execute(sql)
con.commit()
con.close()

但它给了我错误--->(1054,“'字段列表'中的未知列'm'”)

请帮我解决它

4

2 回答 2

3

'insert into test values(%s)' %g

扩展到

'insert into test values(m)'

这显然不是你想要的(什么是m?)

我的建议是使用绑定参数:

g = 'm'
sql = 'insert into test values(?)'
cur.execute(sql, g)

有关详细信息,请参阅Python 中如何在 SQL 语句中使用变量?

于 2013-10-14T12:46:05.607 回答
1

你应该试试这个:

'insert into test () values('%s')' %g ... this is because the variable g is aString` 并且在您正在执行此操作的附加之后必须如下所示:

“插入测试()值('m')”而不是“插入测试()值(m)”

<>

干杯

于 2014-12-10T12:20:34.137 回答