我使用python编写了一个程序,将读取的信息写入sqlite数据库。
我想逐行读取一个txt文件,当读取一些特定的单词时,sql数据库中相应的值应该加1。
但是当我使用update方法时,出现错误: near ",":语法错误。
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
发布于 2019-08-29 13:01:51
替换insert语句,如下所示:
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, ''))
您不需要指定类型,只使用%s
python就可以正确处理它。但请确保您作为参数提供的值与表中的值一致。
还有你的更新:
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:]))
您的where
子句应该是WHERE FOLDER_NAME= %s and FILE_NAME = %s
或WHERE FOLDER_NAME= %s or FILE_NAME = %s
,并且不能带逗号。
https://stackoverflow.com/questions/57703154
复制相似问题