【发布时间】:2019-06-29 09:13:09
【问题描述】:
考虑一个有 6 行的 users 表
+_______________________+
| userid | name |
+-----------------------+
| 1 | john |
| 2 | steve |
| 3 | joe |
| 4 | jason |
| 5 | abraham |
| 6 | leonard |
+-----------------------+
我正在使用以下 SQL 查询:
SELECT userid,name FROM users where userid IN (2,3,4,5);
返回 4 行 -
| 2 | steve |
| 3 | joe |
| 4 | jason |
| 5 | abraham |
Pymysql 等效代码如下:
def get_username(user_ids):
data=[]
conn = init_db()
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.executemany("SELECT userid,name from users WHERE userid IN (%s)",user_ids)
rows=cur.fetchall()
for row in rows:
data.append([row['userid'],row['name']])
cur.close()
conn.close()
return data
user_ids=[2,3,4,5]
get_usernames(user_ids)
这段代码只返回最后一行 [[5,abraham]] 。如何获取所有这些行?。
【问题讨论】: