gpt4 book ai didi

python - MySQL 'SHOW TABLES' 返回计数而不是列表 (Python)

转载 作者:可可西里 更新时间:2023-11-01 07:34:10 25 4
gpt4 key购买 nike

我正在对用于查询数据库的脚本进行故障排除。为了确保一切正常,我将其简化为一个简单的“SHOW TABLES”查询。问题是它返回表的计数而不是它应该返回的名称列表。

import pymysql

connection = pymysql.connect(host='10.0.0.208', user='admin', passwd='Passwrd')

cursor = connection.cursor()
sqlstring = 'SHOW TABLES;'
cursor.execute('USE CustDB')
x = cursor.execute(sqlstring)

print(x)

这只返回“17”。我错过了什么??

最佳答案

根据 documentation , execute 返回受影响的行数

Returns: Number of affected rows

为了得到想要的结果,需要遍历游标

cursor.execute('USE CustDB')
tables = [c for c in cursor]

或者使用fetchall

cursor.execute('USE CustDB')
tables = cursor.fetchall()

关于python - MySQL 'SHOW TABLES' 返回计数而不是列表 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44417912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com