使用%s

时间:2015-11-07 12:18:21

标签: python cursor mysql-python execute

我在python中启动了一个简单的mysql查询,返回表的所有列。 出于某种原因,此命令有效:

cursor.execute("SHOW columns from students")

虽然以下所有内容都返回了类型错误:

cursor.execute("SHOW columns from %s", 'students')

cursor.execute("SHOW columns from %s", ('students',))

cursor.execute("SHOW columns from %s", ['students'])

cursor.execute("""SHOW columns from %s""", 'students')

错误是:

Traceback (most recent call last):
File "<pyshell#60>", line 1, in <module>
    cursor.execute("SHOW columns from %s", 'students')
File "C:\Anaconda\lib\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

请告知

1 个答案:

答案 0 :(得分:1)

尝试使用格式化,这是格式化字符串的推荐方法:

cursor.execute("SHOW columns from {}".format('students'))