'How to select without column names in python with pymysql?
I want to select a row in mysql, I do like this:
import pymysql
conn = pymysql.connect(
host='10.0.0.11',
port=3306,
user='root',
password='159753arazr',
db='bili_1810',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
table_name = ['24569', '25510']
for x in table_name:
sql = "SELECT `播放`, `关注`, `弹幕`, `硬币` FROM `"+x+"` LIMIT %s, 1"
cursor.execute(sql, (9))
conn.commit()
result = cursor.fetchall()
print(result)
conn.close()
The result is:
[{'播放': '25824', '关注': '3071', '弹幕': '141', '硬币': '107'}]
[{'播放': '0', '关注': '1244460', '弹幕': '0', '硬币': '0'}]
But I want it to be like this:
[{'25824', '3071', '141', '107'}]
[{'0', '1244460', '0', '0'}]
Just without the column names, how can I do it?
Solution 1:[1]
As your result shown, you got a list which covers the items you queried in a dict struct.If you want to get your wanted result like that, you can try this:
result = cursor.fetchall()
result_values = [iter_item for iter_item in map(lambda item: [item[key]
for key in item.keys()], result)]
In this way, you can get like [[]] instead of [{}], but this guarantee the value order. If you just want set, you can use this:
result = cursor.fetchall()
result_values = [iter_item for iter_item in map(lambda item: set([item[key]
for key in item.keys()]), result)]
Solution 2:[2]
Just remove the cursorclass=pymysql.cursors.DictCursor By default it gives in the required format without keys.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rouzip |
| Solution 2 | mohneesh_d |
