【问题标题】:SQLite3 Python variablesSQLite3 Python 变量
【发布时间】:2020-04-04 10:40:01
【问题描述】:

我已经在 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)""")

试了之后显示这个错误: 文件“store_data.py”,第 68 行,在 main (v_day,v_month,v_year,v_hour,v_minute,temp,hum)""") sqlite3.OperationalError:没有这样的列:v_day 我已经尝试将变量名称放入 VALUES 列表中,但出现了同样的错误。

谢谢你的回答。

【问题讨论】:

    标签: python sqlite


    【解决方案1】:

    您的元组在您的字符串中。此外,您不应该在查询中表示表列。试试

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

    【讨论】:

      【解决方案2】:

      试试这个:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-05
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 2018-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多