哎,我很新的这一切,所以请原谅的愚蠢:)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