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 表时会出现错误。
问题是数据框有包含
可能的解决方法:
在将数据加载到数据库之前,您可以简单地将有问题的
1
2 |
for i in [‘country’, ‘indicator’]:
dfItem[i] = dfItem[i].apply(json.dumps) |
这会将
但是恕我直言,您最好将目录展平(注意两个字典中都有
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/267976.html