我有下面的示例python代码,当我尝试执行时,会收到mysql错误消息。Context,正在尝试创建自定义日志,记录消息,插入到MySQL表中进行分析。提供mysql执行的相关部分。
levelnum= str(record.levelno) # 40
levelname=str(record.levelname) # ERROR
msg=str(self.log_msg) # "This error occurred: This is test message"
createtime=str(tm) #2018-06-26 03:43:47
record.name = 'MY_LOGGER'
sql = 'INSERT INTO emp.log (log_level, log_levelname, log, created_at, created_by) VALUES ('+levelnum+ ', '+levelname+ ', '+msg+ ', '+createtime+ ', '+record.name+')'
Error message: ProgrammingError: (1064, u"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 'error msg, 2018-06-26 03:43:47, MY_LOGGER)' at line 1")
正在使用MySQL,并尝试按如下方式插入到表中:
try:
self.sql_cursor.execute(sql)
self.sql_conn.commit()
except pymysql.InternalError as e:
print e
print 'CRITICAL DB ERROR! Logging to database not possible!'
我认为,在SQL查询中传递参数时缺少某些格式,但无法获得正确的格式。
如果有人能帮忙解决这个问题,我将不胜感激。