Python 使用 %s 执行时出现 MySQLdb 类型错误

Python MySQLdb type error while using execute with %s

我从 python 中的一个简单 mysql 查询开始,return 查询了 table 的所有列。 由于某种原因,此命令有效:

cursor.execute("SHOW columns from students")

而以下所有return类型错误:

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

请指教

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

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