簡體   English   中英

MariaDB 游標無法正確使用 Python

[英]MariaDB cursor not working with Python Correctly

我在使用 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 程序中使用的是相同的數據庫。

execute所做的只是執行查詢。 然后您需要從游標中fetch數據,您可以使用(例如) fetchone

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM