Python Mysqldb cursor没有“fetchAll”属性

2024-09-29 17:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直收到错误“AttributeError:'Cursor'object has no attribute'fetchAll'”。我的rowCount是451,所有查询都正常。在

我已经研究过这个问题,大多数错误都涉及到对cursor.execute()的返回值调用fetchAll(),但这不是我要做的。在

#initialize cursors
presswiseCursor = presswiseConnection.cursor()
localCursor = localConnection.cursor()

#products
#initialize table
localCursor.execute("DROP TABLE IF EXISTS products;")
localCursor.execute("CREATE TABLE products (id INT NOT NULL, description VARCHAR(255));")
#get data from presswise
rowCount = presswiseCursor.execute("SELECT DISTINCT id, description FROM list_product;")
print(rowCount)
rows = presswiseCursor.fetchAll()
#add data to local
for row in rows:
    localCursor.execute(f'INSERT INTO products (id, description) VALUES({row[0]}{row[1]})')

我希望遍历查询的结果,并在以前使用过这种精确的技术。我的代码怎么了?在


Tags: idexecutedata错误tabledescriptioncursorrows
2条回答

您是否尝试过不将cursor.execute()归因于一个变量,然后调用cursor.fetchAll()?在

然后在fetchAll()之后执行len(rows)来获得行数

另请注意,为什么在sql语句中使用三重引号?在

在python3中使用fetchall()而不是fetchall()。 谢谢您。在

相关问题 更多 >

    热门问题