简体   繁体   English

SQLite3 Python 变量

[英]SQLite3 Python variables

I've written this code for an SQLite3 database in python:我在 python 中为 SQLite3 数据库编写了以下代码:

 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.尝试后,它显示此错误:文件“store_data.py”,第 68 行,在 main (v_day,v_month,v_year,v_hour,v_minute,temp,hum)""") sqlite3.OperationalError: no such column: v_day I '已经尝试将变量名称放入 VALUES' 列表中,但我出现了相同的错误。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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