哎,我很新的這一切,所以請原諒的愚蠢:)Python的MySQL的語句,返回錯誤
import os
import MySQLdb
import time
db = MySQLdb.connect(host="localhost", user="root", passwd="********", db="workspace")
cursor = db.cursor()
tailoutputfile = os.popen('tail -f syslog.log')
while 1:
x = tailoutputfile.readline()
if len(x)==0:
break
y = x.split()
if y[2] == 'BAD':
timestring = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7]")
if y[2] == 'GOOD':
print y[4] + '\t' + y[7]
,所以我運行的程序,這是我得到
[email protected]:~/$ python reader.py
Traceback (most recent call last):
File "reader.py", line 17, in ?
cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7]")
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[4], y[7]' at line 1")
[email protected]:~/$
錯誤信息
所以我假定錯誤顯然是從SQL語句
cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7]")
來這裏是什麼ÿ一個例子[4]和y [7]看起來像。
YES Mail.Sent.To.User:[email protected]:23.17
發生此錯誤是因爲在嘗試將它們插入數據庫之前,我應該轉義這些值嗎? 還是我完全錯過了點?
任何幫助,將不勝感激! 在此先感謝。
你可能也NE編輯在[y4]和[y7] – Simon 2008-11-02 23:34:55