python3与mysql的条件查询

找了好多博客,试了好多。最后终于试出来了

因为要做一个条件查询,在mysql语言中可以直接如下:

select name, id, imgs from img where id = 3

如果要做变量的查询,可以

select name, id, imgs from img where id in (1, 2, ……)

在python中

1

cursor.execute("select name, id, imgs from img where id = %s" %value)

2

cursor.execute("select name, id, imgs from img where id = %s", [value])

value为一变量
这样就可以把查询语句嵌入循环中,方便使用

#嘻嘻,记录一下

猜你喜欢

转载自blog.csdn.net/qq_38918440/article/details/88748653