【问题标题】:sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type when inserting date and timesqlite3.InterfaceError:错误绑定参数 1 - 插入日期和时间时可能不支持的类型
【发布时间】:2021-07-22 22:26:17
【问题描述】:

我正在尝试将日期和时间插入到 sqlite 表中。

这是我的代码。

import sqlite3
from datetime import datetime

### Date Time ###
dt = datetime.now()
dates = dt.date()
times = dt.time()


def sql(date, time):
    ### CREATE DB
    con = sqlite3.connect("date.db")
    cur = con.cursor()
    ## CREATE TABLE
    cur.execute("CREATE TABLE if NOT EXISTS d_t (datee, timee)")
    con.commit()
    ## INSERT DATA
    cur.execute("INSERT INTO d_t (datee, timee) VALUES (?,?)", (date, time))
    con.commit()
    ## VIEW DATA
    cur.execute("SELECT * from d_t")
    row = cur.fetchall()
    print(type((row[0][0]))) # Printing_Date_only


sql(dates, times)

但这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Hridoy\Documents\GitHub\Covid19\datedb.py", line 26, in <module>
    sql(dates, times)
  File "C:\Users\Hridoy\Documents\GitHub\Covid19\datedb.py", line 18, in sql
    cur.execute("INSERT INTO d_t (datee, timee) VALUES (?,?)", (date, time))
sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.

我不想将日期和时间作为字符串插入,因为我需要稍后比较两个日期,其中一个将来自数据库。

请寻求解决方案。

【问题讨论】:

    标签: python sqlite datetime


    【解决方案1】:

    在尝试存储日期/时间时,使用 SQLite 可能会有点棘手,而且我认为您不能存储时间(没有日期)。为什么不将日期和时间一起存储为完整的日期时间值?

    以下代码存储日期时间值并修改 sqlite3.connect() 调用以更好地处理日期时间(我们将在查询数据库时返回一个日期时间)。我们还需要在创建表时将d_t表的“date_time”列的类型指定为SQLite时间戳。

    import sqlite3
    from datetime import datetime
    
    ### Date Time ###
    dt = datetime.now()    
    
    
    def sql(dt_value):
        ### CREATE DB
        con = sqlite3.connect("date.db", detect_types=sqlite3.PARSE_DECLTYPES)
        cur = con.cursor()
        ## CREATE TABLE
        cur.execute("CREATE TABLE if NOT EXISTS d_t (date_time timestamp)")
        con.commit()
        ## INSERT DATA
        cur.execute("INSERT INTO d_t (date_time) VALUES (?)", (dt_value,))
        con.commit()
        ## VIEW DATA
        cur.execute("SELECT * from d_t")
        row = cur.fetchall()
        print(type(row[0][0]))  # Print type of datetime
        print(row[0][0])        # Print datetime
        print(row[0][0].date()) # Print date
        print(row[0][0].time()) # Print time
    
    
    sql(dt)
    

    作为替代解决方案,您可能还想尝试我为帮助解决此类问题而编写的“easy_db”库。只需使用 pip 安装即可:

    pip install easy_db
    

    然后我们可以用更少/更简洁的代码完成同样的任务。

    import easy_db
    from datetime import datetime
    
    ### Date Time ###
    dt = datetime.now()
    
    
    def insert_and_read_datetime(dt_value):
        # Create and connect to SQLite database
        db = easy_db.DataBase("date.db")
        
        # Create "d_t" table and add one row of data to it
        # From our input dictionary, a "date" column is automatically created and
        # this column is given the SQLite timestamp type based on the type of dt_value
        db.append("d_t", {"date": dt_value})
        
        # Run a SELECT * query to pull the full "d_t" table
        # Returned data is a list with a dictionary for each row
        data = db.pull("d_t")
        print(type(data[0]["date"]))  # Print type of datetime
        print(data[0]["date"])        # Print datetime
        print(data[0]["date"].date()) # Print date
        print(data[0]["date"].time()) # Print time
    
    insert_and_read_datetime(dt)
    

    祝您的 Covid-19 项目好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多