首页 文章

使用python在sqlite3中不支持的类型错误

提问于
浏览
0

我正在废弃一些websties并希望将这些数据插入到sqlite3数据库中 . 我成功创建并连接了表和数据库

conn.execute('''
    CREATE TABLE datatable (id INTEGER PRIMARY KEY, Date TEXT,
                       Time TEXT, html_link BLOB, Desc BLOB, Tup BLOB)
''')

id is the auto_incriment value
date="03-09-2016"
time="12:34:56"
html="http://web1.com"
des="Oct 21, 2010 - In the code below row is a tuple of 200 elements (numbers) and listOfVars is a tuple of 200 strings that are variable names in the testTable ."
arr=(1,2,3)

然后我尝试将值插入此表

conn.execute('''INSERT INTO datatable (Date, Time, html_link, Desc, Tup)
                  VALUES(?,?,?,?,?)''', (date,time,html,des,arr))

但它抛出这个错误

VALUES(?,?,?,?,?)''',(date,time,html,des,arr))sqlite3.InterfaceError:错误绑定参数4 - 可能不支持的类型 .

我知道我在将正确的数据类型指定给sqlite表时犯了一些错误 . 如何找到导致错误的列以及如何解决此问题 .

1 回答

  • 1

    尝试保留你试图用泡菜存储的元组 . 这是工作示例 .

    import sqlite3
    import pickle
    
    conn = sqlite3.connect(":memory:")
    conn.text_factory=str
    cur=conn.cursor()
    cur.execute('''
        CREATE TABLE datatable (id INTEGER PRIMARY KEY, Date TEXT,
                           Time TEXT, html_link BLOB, Desc BLOB, Tup BLOB)
    ''')
    
    date="03-09-2016"
    time="12:34:56"
    html="http://web1.com"
    des="Oct 21, 2010 - In the code below row is a tuple of 200 elements (numbers) and listOfVars is a tuple of 200 strings that are variable names in the testTable ."
    arr=(1,2,3)
    
    
    
    cur.execute('''INSERT INTO datatable (Date, Time, html_link, Desc, Tup) VALUES(?,?,?,?,?)''', (date,time,html,des,pickle.dumps( arr,2 )))
    cur.execute('''SELECT * from datatable''')
    print pickle.loads(cur.fetchall()[0][5])
    conn.close()
    

相关问题