在下面的示例中,我设法打印了我想在 mysql 表中插入的 sql 值。我如何实际执行该语句?
import re
import MySQLdb
s = "Jul 15 12:12:51 whitelist logger: 1|999999999999|id:d9faff7c-4016-4343-b494-37028763bb66 submit date:1307130919 done date:1307130919 stat:DELIVRD err:0|L_VB3_NM_K_P|1373687445|vivnel2|L_VB3_GH_K_P|promo_camp1-bd153424349bc647|1"
logger_re = re.compile(
"logger: ([^ ]+)\
submit date:(\d+)\
done date:(\d+)\
stat:(.+)\
err:(.+)$")
myvalues = logger_re.search(s).groups()
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="passwd", # your password
db="test") # name of the data base
# you must create a Cursor object. It will let
# you execute all the query you need
cur = db.cursor()
# cur.execute(insert into test.weblogs (output of myvalues))
这些值需要从文本文件中提取。通常 /var/log/messages