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

标签 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

请指教

最佳答案

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

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

关于使用 %s 执行时 Python MySQLdb 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33582670/

相关文章:

sql - sql游标的替代品

r - 加载 Shiny 的应用程序时更改光标

python - 使用 %s 运算符在 Python 中创建 MySQL 数据库

javascript - Python 动态方法

python - 如何检查列表中的整数和字符串

python - 从 subprocess.communicate() 读取流输入

android - 获取 java.lang.IllegalStateException : attempt to re-open an already-closed object: when using cursor in Android

Python、带有变量的 mySQL

mysql - Mac OS Mojave 导入 MySQLDB 导入问题

python - 如何使用 PIL 库查找子图像?