SQLite3接近语法错误

时间:2015-12-21 14:41:44

标签: python sqlite syntax

尝试在我的数据库中插入四种不同的东西,但我收到以下错误:

near ".10586": syntax error 

这是我的代码:

b.execute("INSERT INTO logs VALUES(%s, %s, %s, %s)" % (hostname, os, ip.decode('utf-8'), time.strftime("%x")))

1 个答案:

答案 0 :(得分:1)

由于您使用字符串格式构建查询,因此占位符周围需要引号 - "%s"而不是%s

相反,进行参数化查询并将查询参数传递给execute()的第二个参数 - 这样您就可以让数据库驱动程序担心类型转换,并且会阻止{ {3}}。变化很简单:

b.execute("INSERT INTO logs VALUES(?, ?, ?, ?)", 
          (hostname, os, ip.decode('utf-8'), time.strftime("%x"))) 
相关问题