【发布时间】:2017-12-01 11:12:06
【问题描述】:
我想使用 python 在 MySQL 查询中插入一个变量。我该怎么做?
我正在使用 python 2.7。
import MySQLdb
db = MySQLdb.connect("host","root","password","database" )
cursor = db.cursor()
model=Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.60GHz
mode=("INSERT INTO machinedetails (ModelName) VALUES (%s)")
cursor.execute(mode,model)
data = cursor.fetchone()
print (data)
db.close()
-------------------------
Traceback (most recent call last):
File "System_Info.py", line 149, in <module>
cursor.execute(mode,model)
File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])\
TypeError: not all arguments converted during string formatting
【问题讨论】:
-
您将
mode直接传递给execute方法,而它需要一个可迭代的参数。试试cursor.execute(mode, [model])。 -
已发布多个同名问题。这就是为什么标题必须放在括号中的原因。类似的问题:stackoverflow.com/questions/18053500/… 或 stackoverflow.com/questions/3089038/…
-
您的型号无效... model = "Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.60GHz"
-
为什么还要在 Django 中使用原始 SQL?
-
是的,发布了多个问题,但我的问题与其他问题不同@NoelWidmer
标签: python sql python-2.7