我用python编写了一个程序,将读取的信息写到sqlite数据库。
我想逐行读取txt文件,当读取某些特定单词时,应在sql数据库中添加相应的值1。
但是当我使用更新方法时,会出现错误:“,”附近:语法错误。
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
答案 0 :(得分:0)
像这样替换您的插入语句:
con sqs
您无需指定类型,仅使用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, ''))
python即可正确处理它。但是请确保您提供的参数值与表中的值一致。
您的更新还:
%s
您的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
,并且不能带逗号。