我已经创建了一个 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'”)
请帮我解决它
我已经创建了一个 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'”)
请帮我解决它
这
'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 语句中使用变量?
你应该试试这个:
'insert into test () values('%s')' %g
... this is because the variable g is a
String` 并且在您正在执行此操作的附加之后必须如下所示:
“插入测试()值('m')”而不是“插入测试()值(m)”
<>
干杯