【发布时间】:2017-09-16 05:44:28
【问题描述】:
我想删除表中与某个值匹配的所有行 (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
【问题讨论】:
-
什么python脚本?您还没有发布任何代码。
-
@ScottHunter 补充了我能做到的