Got error ‘sqlite3.InterfaceError: Error binding parameter 0 – probably unsupported type.’ when insert data into table using sqlite3 python
我试图从 url 链接获取数据并将其存储在 db 文件中。当我将检索到的数据转换为数据帧并将其插入表中时,我收到一条错误消息,提示”sqlite3.InterfaceError: Error binding parameter 1-probably unsupported type.”
这是我的代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import pandas as pd
import requests import sqlite3 from pandas.io import sql database_file = ‘z5192421.db’ def if_table_exist(cnx,table_name):
def write_in_sqlite(dataframe, database_file, table_name): if not if_table_exist(cnx, table_name): def post(): post() |
还有回溯:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Traceback (most recent call last):
File“/Users/chenhao/PycharmProjects/untitled/9321/post_to_stack.py”, **line 34, in <module> post()** File“/Users/chenhao/PycharmProjects/untitled/9321/post_to_stack.py”, **line 32, in post File“/Users/chenhao/PycharmProjects/untitled/9321/post_to_stack.py”, **line 19, in write_in_sqlite File“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py”, **line 512, in to_sql File“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py”, **line 1734, in to_sql File“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py”, **line 755, in insert File“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py”, **line 1464, in _execute_insert |
- 这将有助于提供完整的堆栈跟踪,包括引发错误的行……
好的,现在更清楚了。当您尝试将数据框加载为新的 SQLite 表时会出现错误。
问题是数据框有包含dict对象的对象列,不能直接加载到SQLite中。
可能的解决方法:
在将数据加载到数据库之前,您可以简单地将有问题的 dict 值转换为字符串:
1
2 |
for i in [‘country’, ‘indicator’]:
dfItem[i] = dfItem[i].apply(json.dumps) |
这会将 dict 对象更改为它们的 json 表示形式,它将作为 TEXT 字段很好地加载到 SQLite 表中。
但是恕我直言,您最好将目录展平(注意两个字典中都有 ‘id’ 和 ‘value’ 的字段)以直接存储可用值。由于我不知道你打算如何使用这张桌子,我不能说更多……
来源:https://www.codenong.com/60914812/