Python 删除MySQL表中的某些行
我有一个MySQL数据库,其中包括两个相互链接的表,分别称为“文件”和“路径”。“files”表有几百行,如果相应的文件已从文件系统中删除,我想删除行,即90+% 我可以列出以下Python代码中不再存在的文件:Python 删除MySQL表中的某些行,python,mysql,Python,Mysql,我有一个MySQL数据库,其中包括两个相互链接的表,分别称为“文件”和“路径”。“files”表有几百行,如果相应的文件已从文件系统中删除,我想删除行,即90+% 我可以列出以下Python代码中不再存在的文件: import pymysql import pymysql.cursors import os.path con = pymysql.connect(host='localhost', db='MyVideos116', user='kodi',
import pymysql
import pymysql.cursors
import os.path
con = pymysql.connect(host='localhost',
db='MyVideos116',
user='kodi',
password='****',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with con:
cur = con.cursor()
cur.execute("SELECT f.*, p.* FROM files f INNER JOIN path p ON f.idPath = p.idPath")
rows = cur.fetchall()
for row in rows:
fileExists = os.path.isfile(row["strPath"] + row["strFilename"])
if not fileExists:
print(row["idFile"], row["strPath"], row["strFilename"])
我尝试在WHERE子句中使用os.path.isfile()短语,例如:
cur.execute("SELECT f.*, p.* FROM files f INNER JOIN path p ON p.idPath = f.idPath WHERE os.path.isfile(p.strPath+f.strFilename")
但这似乎是不允许的。在伪代码中,我想做的是:
for each row in files table
if file does not exist then
delete row
end if
next row
看起来很简单,我该怎么做?为什么不在
如果不存在文件:
块中执行删除?您不能将python代码作为mysql代码的一部分执行。正如@IainShelvington所指出的:使用文件名或路径作为字符串参数,从如果文件不存在:
块执行delete语句。