简体   繁体   中英

near “,”: syntax error when execute SQLite update

I used python to write a program to write the information read to a sqlite database.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM