关于pandas:使用sqlite3 python将数据插入表时出现错误\\’sqlite3.InterfaceError:错误绑定参数0 – 可能是不支持的类型。\\’

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):
    c = cnx.cursor()
    c.execute(f"SELECT name FROM sqlite_master WHERE type = 'table' AND
        name='{table_name}'"
)
    return c.fetchone()


def write_in_sqlite(dataframe, database_file, table_name):
    cnx = sqlite3.connect(database_file)
    c = cnx.cursor()

    if not if_table_exist(cnx, table_name):
        sql.to_sql(dataframe, name=table_name, con=cnx)
    else:
        for index, row in dataframe.iterrows():
            values = '('+','.join(['?']*len(dataframe.columns))+')'
            c.execute(f'INSERT INTO {table_name} VALUES {values}',
                      tuple(row.values))



def post():
    url = 'http://api.worldbank.org/v2/countries/all/indicators/' \\
          'NY.GDP.MKTP.CD?date=2012:2017&format=json&per_page=1000'
    data = requests.get(url).json()[1]
    dfItem = pd.DataFrame.from_records(data)
    write_in_sqlite(dfItem,database_file, 'DATA')

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
    write_in_sqlite(dfItem,database_file, 'DATA')**

  File"/Users/chenhao/PycharmProjects/untitled/9321/post_to_stack.py", **line 19, in write_in_sqlite
    sql.to_sql(dataframe, name=table_name, con=cnx,index = False)**

  File"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py", **line 512, in to_sql
    pandas_sql.to_sql(**

  File"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py", **line 1734, in to_sql
    table.insert(chunksize, method)**

  File"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py", **line 755, in insert
    exec_insert(conn, keys, chunk_iter)**

  File"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/sql.py", **line 1464, in _execute_insert
    conn.executemany(self.insert_statement(), data_list)**
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.


好的,现在更清楚了。当您尝试将数据框加载为新的 SQLite 表时会出现错误。

问题是数据框有包含dict对象的对象列,不能直接加载到SQLite中。

可能的解决方法:

在将数据加载到数据库之前,您可以简单地将有问题的 dict 值转换为字符串:

1
2
for i in ['country', 'indicator']:
    dfItem[i] = dfItem[i].apply(json.dumps)

这会将 dict 对象更改为它们的 json 表示形式,它将作为 TEXT 字段很好地加载到 SQLite 表中。

但是恕我直言,您最好将目录展平(注意两个字典中都有 'id''value' 的字段)以直接存储可用值。由于我不知道你打算如何使用这张桌子,我不能说更多......