2013-07-15 2 views
0

다음 예에서는 mysql 테이블에 삽입하려는 SQL 값을 인쇄했습니다. 어떻게 실제로이 명령문을 실행합니까?mysql에 값을 삽입하십시오.

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/로그/메시지

답변

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() 
+0

하여 탈출해야합니다 데이터 또한 ... –

관련 문제