简体   繁体   中英

SQLite3 Python variables

I've written this code for an SQLite3 database in python:

 i = datetime.datetime.now()
        v_day = i.day
        v_month = i.month
        v_year = i.year
        v_hour = i.hour
        v_minute = i.minute

    cur.execute("""INSERT INTO weather(
                day,month,year,hour,minut,temperature,humidity) VALUES (
                ?,?,?,?,?,?,?),
                (v_day,v_month,v_year,v_hour,v_minute,temp,hum)""")

After trying it, it displayed this Error: File "store_data.py", line 68, in main (v_day,v_month,v_year,v_hour,v_minute,temp,hum)""") sqlite3.OperationalError: no such column: v_day I've already tried to put the variables names in the VALUES' list, but I occured in the same Error.

Thank you for the answer.

Your tuple is inside your string. Also you shouldn't denote the table columns in the query. Try

    cur.execute("INSERT INTO weather VALUES (?,?,?,?,?,?,?)",
                (v_day,v_month,v_year,v_hour,v_minute,temp,hum))

Try this one:

sql = f"""INSERT INTO weather(day,month,year,hour,minut,temperature,humidity) VALUES ({v_day},{v_month},{v_year},{v_hour},{v_minute},{temp},{hum})"""
cur.execute(sql)

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