Python 在mysql中插入值
在下面的示例中,我成功地打印了我想插入到mysql表中的sql值。 我实际上如何执行该语句Python 在mysql中插入值,python,Python,在下面的示例中,我成功地打印了我想插入到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|13736874
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此示例应该会有所帮助 在阅读您的问题之后,很难说您的问题到底是什么(格式化查询、提交过程?)
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()