MySQLdb fetcall,AttributeError:'int'对象没有属性'fetchall'

Mis*_*cic 2 python mysql mysql-python

我想检查是否以正确的方式将csv文件导入MySQL数据库。

import MySQLdb

mydb = MySQLdb.connect(host = 'localhost',user = 'milenko',passwd = 'nuklear',db = 'mm')
cur = mydb.cursor()
command = cur.execute('SELECT * FROM jul')
results = command.fetchall()

print (results)
Run Code Online (Sandbox Code Playgroud)

但是我明白了

File "b12.py", line 6, in <module>
    results = command.fetchall()
AttributeError: 'int' object has no attribute 'fetchall'
Run Code Online (Sandbox Code Playgroud)

我看过以前的SO帖子,人们声称数字对象没有fetcall对象。如何一次性删除数据库内容?

小智 5

实际上,您不能调用fetchall()a的结果cursor.execute(),根据MySQLdb文档,cursor.execute()返回所执行查询的受影响行数。要检索数据,您必须直接访问游标结果:

cur = mydb.cursor()
cur.execute('SELECT * FROM jul')
results = cur.fetchall()
Run Code Online (Sandbox Code Playgroud)