无法正确使用Python

2024-04-29 07:51:25 发布

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

我和MariaDB有问题

当我在Python中使用以下代码时:-

print(maria)
a= maria.execute("select * from new_table")
print(a)

它打印:-

<MySQLdb.cursors.Cursor object at 0x000002020CB17BC8>
2

但是,当我在终端中使用MariaDB客户端并使用以下命令时:-

select * from new_table

我得到以下资料:

+------+------+
| aval | bval |
+------+------+
|   10 | Ok   |
|   20 | Kk   |
+------+------+

我已经检查了我在终端和Python程序中使用相同的数据库


Tags: 代码from终端newexecuteobjecttableselect
1条回答
网友
1楼 · 发布于 2024-04-29 07:51:25

^{}所做的就是执行查询。然后,您需要fetch从游标读取数据,您可以使用(例如)^{}执行此操作:

maria.execute("select * from new_table")
row = maria.fetchone()
while row is not None:
    print(row)
    row = maria.fetchone()

或者,您可以将光标用作迭代器:

maria.execute("select * from new_table")
for row in maria:
    print(row)

相关问题 更多 >