[英]near “,”: syntax error when execute SQLite update
I used python to write a program to write the information read to a sqlite database. 我使用python编写了一个程序,将读取的信息写入sqlite数据库。
I want to read a txt file by line, and when some specific words were read, the corresponding value in the sql database should be added 1. 我想逐行读取txt文件,并且在读取某些特定单词时,应在sql数据库中添加相应的值1。
But when I use update method, error occurs: near ",": syntax error. 但是,当我使用更新方法时,会发生错误:“,”附近:语法错误。
conn = sqlite3.connect(fname + '\\Static_Analysis.db')
print fname + 'Static_Analysis.db'
c = conn.cursor()
c.execute('''CREATE TABLE MAIN
(
FOLDER_NAME TEXT NOT NULL,
FILE_NAME TEXT NOT NULL,
Error_NO integer,
Warning_NO integer,
Advice_NO integer,
Total integer,
Note CHAR(50),
PRIMARY KEY (FOLDER_NAME, FILE_NAME ));''')
c.execute('''CREATE TABLE ERROR_REPORT
(
NAME TEXT NOT NULL,
PRIMARY KEY (NAME));''')
c.execute('''CREATE TABLE WARNING_REPORT
(
NAME TEXT NOT NULL,
PRIMARY KEY (NAME));''')
c.execute('''CREATE TABLE ADVICE_REPORT
(
NAME TEXT NOT NULL,
PRIMARY KEY (NAME));''')
i = 1
for dst in list:
m_i = 0
n_i = 0
p_i = 0
file = open(dst + "\\summary.log", 'r')
for line in file:
if '[Error ]' in line:
m_i = m_i + 1
str = line.split(":")
print str
try:
conn.execute("INSERT INTO MAIN
(FOLDER_NAME, FILE_NAME, Error_NO, Warning_NO,
Advice_NO, Total, Note) VALUES (\"%s\", \"%s\",
\"%d\", \"%d\",
\"%d\",
\"%d\", \"%s\")" % (dst, str[0][9:], 0, 0, 0, 0,
''))
except:
pass
try:
conn.execute(
"""UPDATE MAIN SET
Error_NO = Error_NO + 1, Total = Total + 1
WHERE FOLDER_NAME= ?, FILE_NAME = ? """,
(dst, str[0][9:]))
except Exception as e:
print e.message
Replace your insert statement like this: 像这样替换您的插入语句:
conn.execute("""INSERT INTO MAIN (FOLDER_NAME, FILE_NAME, Error_NO, Warning_NO, Advice_NO, Total, Note)
VALUES(%s, %s, %s, %s, %s, %s, %s)""" % (dst, str[0][9:], 0, 0, 0, 0, ''))
You do not need to specify the type, using just %s
python will handle it correctly. 您无需指定类型,只需使用
%s
python即可正确处理它。 But make sure that the values you give as arguments are in accordance to your table ones. 但是,请确保您提供的参数值与表中的值一致。
And your update also: 而且您的更新还:
conn.execute("""UPDATE MAIN SET Error_NO = Error_NO + 1, Total = Total + 1 WHERE FOLDER_NAME= %s and FILE_NAME = %s """ % (dst, str[0][9:]))
Your where
clause should be either WHERE FOLDER_NAME= %s and FILE_NAME = %s
or WHERE FOLDER_NAME= %s or FILE_NAME = %s
and not with a comma. 您的
where
子句应为WHERE FOLDER_NAME= %s and FILE_NAME = %s
或WHERE FOLDER_NAME= %s or FILE_NAME = %s
且不能带逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.