简体   繁体   中英

sqlite3.OperationalError: near ".27": syntax error

Recently, I'm trying to make some database. (I'm new to programming) When I'm trying to run following code, I'm receiving this error:

sqlite3.OperationalError: near ".27": syntax error

def file_write(self, name, value):                  
    con = sqlite3.connect("c:/Users/DB.db")
    cursor = con.cursor()
    cursor.execute("CREATE TABLE {} (Date text, Value1 int, Value2 int, Value3 int, Value4 int, Value5 int, Value6 float, Value7 float, Value8 float)".format(name))
    for i in range(3):
        cursor.execute("INSERT INTO {0} VALUES({1},{2},{3},{4},{5},{6},{7},{8},{9})".format(name, value[i][0], value[i][1], value[i][2], value[i][3], value[i][4], value[i][5], value[i][6], value[i][7], value[i][8]))
        #cursor.execute("INSERT INTO {0} VALUES({1},{2},{3},{4},{5},{6},{7},{8},{9})".format(name, '2020.03.27', '12500', '13100', '15100', '10950', '31323050', '10440.0', '10524.5', '45.0'))
    con.commit()
    con.close()

Values are DataFrame and remark's values are matched with each value[i][j] . I spent my all day with this problem please help me

The problem is with your date formatting, this will work:

cursor.execute("INSERT INTO {0} VALUES('{1}',{2},{3},{4},{5},{6},{7},{8},{9})".format(name, '2020-03-27','12500', '13100', '15100', '10950', '31323050', '10440.0', '10524.5', '45.0'))

Read more from the docs here: https://www.sqlite.org/datatype3.html#date_and_time_datatype

Also as was mentioned in the comments, you should never format query strings yourself. Untrustworthy inputs formatted into sql queries will make your code vulnerable for sql injection attacks (read more here https://en.wikipedia.org/wiki/SQL_injection )

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