我是python的新手。我尝试写skript来解析mysql。我的部分代码:
db = MySQLdb.connect(host="localhost", user="me", passwd="passwd", db="Data")
with db:
cursor = db.cursor()
with open("my.log","rw") as f:
next(f)
for line in f:
result = parser(line)
resultS = parserS(result[3])
format = '[%a %b %d %H:%M:%S.%f %Y]'
now = datetime.datetime.strptime(result[0], format)
sql = ("INSERT INTO Data.table1 (time, level, id, error, file ,ip, request) VALUES (%s, %s, %s, %s, %s, %s, %s)")
data = (now.strftime('%Y-%m-%d %H:%M:%S %f'), result[1], result[2], ' ' ,resultS[0],resultS[1],resultS[2])
cursor.execute(sql, data)
f.close()
db.close()
当我尝试运行时,请给我留言:
Traceback (most recent call last):
File "parsingApacheErrorFrom.py", line 81, in <module>
cursor.execute(sql, data)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-error (time, log_level, pid, error_status, file ,client_ip, message) VALUES ('2' at line 1")
我尝试在我的sql语法中找到错误,但它对我来说很好。我不明白是什么意思:VALUES(第1行'2')???在mysql表中,第一列是datetime类型,所有其他列都是varchar(255)。