Python" InterfaceError:绑定参数2的错误 - 可能是不支持的类型。"

时间:2015-11-04 00:27:48

标签: python sql sqlite pandas

当我运行以下代码时,我不断得到" InterfaceError:错误绑定参数2 - 可能不支持的类型"错误,我需要帮助确定问题所在。一切正常,直到我尝试将数据发送到sql。

anagramsdf.to_sql('anagrams',con=conn,if_exists='replace',index=False)
cdf=pd.read_sql("select (distinct ID) from anagrams;",conn)
import pandas as pd
import sqlite3

conn = sqlite3.connect("anagrams")
xsorted=sorted(anagrams,key=sorted)
xunique=[x[0] for x in anagrams]

xunique=pd.Series(xunique)
xanagrams=pd.Series(anagrams)
anagramsdf=pd.concat([xunique,dfcount,xanagrams],axis=1)
anagramsdf.columns=['ID','anagram_count','anagram_list']

c=conn.cursor()
c.execute("create table anagrams(ID, anagram_count, anagram_list)")
conn.commit()

anagramsdf.to_sql('anagrams',con=conn,if_exists='replace',index=False)
cdf=pd.read_sql("select (distinct ID) from anagrams;",conn)

cdf=pd.read_sql("select max(anagram_count) from anagrams;",conn)
cdf
def print_full(x):
    pd.set_option('display.max_rows', len(x))
    print(x)
    pd.reset_option('display.max_rows')

cdf=pd.read_sql("select * from anagrams where anagram_count=12;",conn)
pd.set_option('max_colwidth',200)

完整追溯错误:

Traceback (most recent call last):
  File "sqlpandas.py", line 88, in <module>
    anagramsdf.to_sql('anagrams',con=conn,if_exists='replace',index=False)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 982, in to_sql
    dtype=dtype)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 549, in to_sql
    chunksize=chunksize, dtype=dtype)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 1567, in to_sql
    table.insert(chunksize)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 728, in insert
    self._execute_insert(conn, keys, chunk_iter)
  File "/Users/andrewclark/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 1357, in _execute_insert
    conn.executemany(self.insert_statement(), data_list)
sqlite3.InterfaceError: Error binding parameter 2 - probably unsupported type.
来自Dataframe的

Snippet:

             ID  anagram_count           anagram_list
0            aa              1                  (aa,)
1      anabaena              1            (anabaena,)
2      baaskaap              1            (baaskaap,)
3      caracara              1            (caracara,)
4      caragana              1            (caragana,)

3 个答案:

答案 0 :(得分:2)

我使用以下代码将数据类型更改为字符串,这解决了问题:

anagramsdf.dtypes
anagramsdf['ID']= anagramsdf['ID'].astype('str')
anagramsdf['anagram_list']= anagramsdf['anagram_list'].astype('str')
anagramsdf.to_sql('anagramsdf',con=conn,if_exists='append',index=False)

答案 1 :(得分:0)

使用熊猫0.23.4我有一列具有日期时间值(格式为'%Y-%m-%d%H:%M:%S')的数据类型为“字符串”的字符串,它引发了相同的错误当我尝试将其传递给“ to_sql”方法时。转换为“ datetime” dtype后,它开始工作。希望对遇到同样问题的人有所帮助:)。

要转换:

#!/bin/ksh
awk ' BEGIN {FS=";"}
{
 if (NR==1)
        if(length($0)!=10) 
    {
    print(length($0))
    thelength=length($0)
    print "The length of the first line is: ",thelength;
    exit 1;
    }
    }
    END { print "STOP"  }' $1

答案 2 :(得分:0)

Sparrow的解决方案对我有用。如果索引未转换为日期时间,则SQL将抛出“错误绑定参数”

我使用了具有日期时间的列,首先将其转换为正确的格式,然后将其用作索引:

df.set_index(pd.to_datetime(df['datetime']), inplace=True)