我想删除表中与某个值匹配的所有行(userId
)
我一直在看这篇文章(Python MySQLdb iterate through table),试图让它按我所希望的方式工作。你知道吗
我有一个users
表和一个images
表。你知道吗
images
:
在我的python脚本中,我只拉入了用户名。所以首先
我需要使用传入的userName
获取用户的userId
。(我有下面的SELECT
语句,但不知道如何将结果赋给变量)
import MySQLdb
dbb = MySQLdb.connect(host="ipaddress",
user="root",
passwd="myPassword",
db="myDatabase")
user_name = "user"
cursor = dbb.cursor()
cursor.execute("""SELECT userId FROM users WHERE userName ='%s'""" (user_name))
var users_id = 0 // need to assign users_id to the selected userId from above code
那么
我需要遍历images
表并删除带有相应userId
的所有行
cursor2 = dbb.cursor()
cursor2.execute("""SELECT FROM images WHERE userId='%s'""" (users_id))
for cursor2: //don't think this is right...
//DELETE all rows with matching users_id
可以循环使用
SELECT
语句生成的行,就好像execute
方法返回了行列表一样。你知道吗相关问题 更多 >
编程相关推荐