循环不适用于 sql 更新语句 (mysqldb)
Loop not working for sql update statement (mysqldb)
我有一个名为 'testfolder' 的文件夹,其中包含两个文件 -- 'Sigurdlogfile' 和“2004ADlogfile”。每个文件都有一个名为 entries
的字符串列表。我需要 运行 我的代码,并且正在使用 glob
来执行此操作。我的代码为每个文件创建一个字典并存储使用正则表达式提取的数据,其中字典键存储在下面的 commonterms
中。然后它将每个字典插入到 mysql table 中。它成功地完成了所有这些,但我的第二个 sql 语句没有插入它应该如何(每个文件)。
import glob
import re
files = glob.glob('/home/user/testfolder/*logfile*')
commonterms = (["freq", "\s?(\d+e?\d*)\s?"],
["tx", "#txpattern"],
["rx", "#rxpattern"], ...)
terms = [commonterms[i][0] for i in range(len(commonterms))]
patterns = [commonterms[i][1] for i in range(len(commonterms))]
def getTerms(entry):
for i in range(len(terms)):
term = re.search(patterns[i], entry)
if term:
term = term.groups()[0] if term.groups()[0] is not None else term.groups()[1]
else:
term = 'NULL'
d[terms[i]] += [term]
return d
for filename in files:
#code to create 'entries'
objkey = re.match(r'/home/user/testfolder/(.+?)logfile', filename).group(1)
d = {t: [] for t in terms}
for entry in entries:
d = getTerms(entry)
import MySQLdb
db = MySQLdb.connect(host='', user='', passwd='', db='')
cursor = db.cursor()
cols = d.keys()
vals = d.values()
for i in range(len(entries)):
lst = [item[i] for item in vals]
csv = "'{}'".format("','".join(lst))
sql1 = "INSERT INTO table (%s) VALUES (%s);" % (','.join(cols), csv.replace("'NULL'", "NULL"))
cursor.execute(sql1)
#now in my 2nd sql statement I need to update the table with data from an old table, which is where I have the problem...
sql2 = "UPDATE table, oldtable SET table.key1 = oldtable.key1,
table.key2 = oldtable.key2 WHERE oldtable.obj = %s;" % repr(objkey)
cursor.execute(sql2)
db.commit()
db.close()
问题是,在第二个 sql 语句中,它最终仅从 objkey
之一将数据插入到 table 的所有列中,但我需要它根据代码当前所在的文件 运行 插入不同的数据。我不明白这是为什么,因为我在 for filename in files
循环中定义了 objkey
。我该如何解决这个问题?
与其单独执行 INSERT
和 UPDATE
,不如将它们一起执行以合并旧 table 中的字段。
for i in range(len(entries)):
lst = [item[i] for item in vals]
csv = "'{}'".format("','".join(lst))
sql1 = """INSERT INTO table (key1, key2, %s)
SELECT o.key1, o.key2, a.*
FROM (SELECT %s) AS a
LEFT JOIN oldtable AS o ON o.obj = %s""" % (','.join(cols), csv.replace("'NULL'", "NULL"), repr(objkey))
cursor.execute(sql1)
我有一个名为 'testfolder' 的文件夹,其中包含两个文件 -- 'Sigurdlogfile' 和“2004ADlogfile”。每个文件都有一个名为 entries
的字符串列表。我需要 运行 我的代码,并且正在使用 glob
来执行此操作。我的代码为每个文件创建一个字典并存储使用正则表达式提取的数据,其中字典键存储在下面的 commonterms
中。然后它将每个字典插入到 mysql table 中。它成功地完成了所有这些,但我的第二个 sql 语句没有插入它应该如何(每个文件)。
import glob
import re
files = glob.glob('/home/user/testfolder/*logfile*')
commonterms = (["freq", "\s?(\d+e?\d*)\s?"],
["tx", "#txpattern"],
["rx", "#rxpattern"], ...)
terms = [commonterms[i][0] for i in range(len(commonterms))]
patterns = [commonterms[i][1] for i in range(len(commonterms))]
def getTerms(entry):
for i in range(len(terms)):
term = re.search(patterns[i], entry)
if term:
term = term.groups()[0] if term.groups()[0] is not None else term.groups()[1]
else:
term = 'NULL'
d[terms[i]] += [term]
return d
for filename in files:
#code to create 'entries'
objkey = re.match(r'/home/user/testfolder/(.+?)logfile', filename).group(1)
d = {t: [] for t in terms}
for entry in entries:
d = getTerms(entry)
import MySQLdb
db = MySQLdb.connect(host='', user='', passwd='', db='')
cursor = db.cursor()
cols = d.keys()
vals = d.values()
for i in range(len(entries)):
lst = [item[i] for item in vals]
csv = "'{}'".format("','".join(lst))
sql1 = "INSERT INTO table (%s) VALUES (%s);" % (','.join(cols), csv.replace("'NULL'", "NULL"))
cursor.execute(sql1)
#now in my 2nd sql statement I need to update the table with data from an old table, which is where I have the problem...
sql2 = "UPDATE table, oldtable SET table.key1 = oldtable.key1,
table.key2 = oldtable.key2 WHERE oldtable.obj = %s;" % repr(objkey)
cursor.execute(sql2)
db.commit()
db.close()
问题是,在第二个 sql 语句中,它最终仅从 objkey
之一将数据插入到 table 的所有列中,但我需要它根据代码当前所在的文件 运行 插入不同的数据。我不明白这是为什么,因为我在 for filename in files
循环中定义了 objkey
。我该如何解决这个问题?
与其单独执行 INSERT
和 UPDATE
,不如将它们一起执行以合并旧 table 中的字段。
for i in range(len(entries)):
lst = [item[i] for item in vals]
csv = "'{}'".format("','".join(lst))
sql1 = """INSERT INTO table (key1, key2, %s)
SELECT o.key1, o.key2, a.*
FROM (SELECT %s) AS a
LEFT JOIN oldtable AS o ON o.obj = %s""" % (','.join(cols), csv.replace("'NULL'", "NULL"), repr(objkey))
cursor.execute(sql1)