我正在使用下面的代码查询我的SQL表,并将结果转换为列表。为什么列表中有不必要的逗号和括号?
查询结果
(34830,),(34650,),(35050,),(34500,),(35050,),(34500,),(34725,),(34550,),(34725,),(34760,),(34760,)
它应该只返回一个只有数字的列表。对吗?
模式简单(链接文本,价格int);
这里有什么问题?我的密码有什么问题吗?
import pymysql
connection = pymysql.connect(host='localhost',
user='root',
password='passme',
db='hpsize') # connection obhect to pass the database details
sql = "SELECT price FROM dummy WHERE link ='https://www.flipkart.com/bose-noise-cancelling-700-anc-enabled-bluetooth-headset/p/itma57a01d3bd591?pid=ACCFGYZEVVGYM8FP'"
my_cursor = connection.cursor()
my_cursor.execute(sql)
result = list(my_cursor.fetchall())
print(result)
connection.close()查询结果
(34830,),(34650,),(35050,),(34500,),(35050,),(34500,),(34725,),(34550,),(34725,),(34760,),(34760,)
发布于 2021-07-24 15:41:08
试一试
connection.row_factory = lambda cursor, row: row[0]而不是list(my_cursor.fetchall())
然后
result = connection.execute("""SELECT * FROM dummy""").fetchall()或者您也可以使用strip()来裁剪不需要的部分。
https://stackoverflow.com/questions/67919036
复制相似问题