python插入mysq

2024-10-01 04:56:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力找出我的代码有什么问题。我试图将数据输入到mysql中,python似乎不喜欢我所做的事情。在

试验数据

Name = "TestRegion"
Perent= "perent"
x1 = -100.0
x2 = -150.0
z1 = 94.0
z2 = 200.0

代码

^{pr2}$

我想我的问题是如何将值传递给sql语句。在

我的错误消息是

File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py, line 159, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

Tags: 数据代码namemysqlquery事情x1x2
2条回答

试试这个:

    sql = u'INSERT INTO RegionName (Name, Perent, X1, X2, Z1, Z2) VALUES ("%s","%s",%d,%d,%d,%d)'% (Name, Perent, x1, x2, z1, z2)
    cur.execute(sql)

MySQL适配器不使用?作为占位符,而是使用%s。在

sql = """INSERT INTO RegionName (Name, Perent, X1, X2, Z1, Z2) VALUES (%s, %s, %s, %s, %s, %s)"""
cur.execute(sql, (Name, Perent, x1, x2, z1, z2))

相关问题 更多 >