查询数据库所有id
在list
里的文章,加入article_list
.
article_list = []
for i in range(0, len(list)):
cur.execute('select * from articles where id = {}'.format(list[i]))
article_list.append(cur.fetchone())
怎么把上面的一句话写成一句python语句呢?
article_list.append(
cur.execute('select * from articles where id = {}'.format(list[i]))
for i in range(0, len(list))
)
我这么写,但是返回article_list
是[
.似乎并没有把具体的值append
.
我觉得你首先应该SQLic……
cur.execute('select * from articles where id in ({})'.format(','.join(str(v) for v in list)))
当然更重要的是不要随便自己拼SQL,SQL注入一点都不好玩,一 点 都 不 好 玩
而且不要随便用list来当变量名字啊……
article_list = [cur.fetchone() for i in list if cur.execute('.....')]
因为execute
返回的是结果数量,所以if
后面可以根据需求自行发挥……