Reputation: 32296
In the following example, I have managed to print the sql values those I will like to insert in mysql table. How do I actually execute the statement?
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))
The values needs to be extracted from a text file. Typically /var/log/messages
Upvotes: 0
Views: 75
Reputation: 19989
This example should help
after reading your question, it's hard to say what exactly is your problem (formatting the query, commit process?)
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()
Upvotes: 2