0

在下面的示例中,我设法打印了我想在 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

4

1 回答 1

2

这个例子应该有帮助

阅读您的问题后,很难说出您的问题到底是什么(格式化查询,提交过程?)

try:
    cur.execute("INSERT INTO MyTable (id,somerow) VALUES (%s, %s)" % ("someid", "somerow"))
    db.commit()
except MySQLdb.Error, e:
    print e[0], e[1]
    db.rollback()
于 2013-07-15T08:51:15.437 回答