我正在学习如何将 pandas 数据帧写入 SQLite 数据库。
我进入了一个示例代码:
import pandas as pd
import pandas.io.sql as pd_sql
import sqlite3 as sql
con = sql.connect("/home/msalese/Documents/ipyNotebooks/tmp.db")
df =pd.DataFrame({'TestData':[1,2,3,4,5,6,7,8,9]})
pd_sql.write_frame(df, "tbldata2", con)
但上面的代码引发了一个异常:
---------------------------------------------------------------------------
InterfaceError Traceback (most recent call last)
<ipython-input-31-c844f7e3f2e6> in <module>()
----> 1 pd_sql.write_frame(df, "tbldata2", con)
/opt/epdFree7.3.2/lib/python2.7/site-packages/pandas-0.10.1-py2.7-linux-x86_64.egg/pandas/io/sql.pyc in write_frame(frame, name, con, flavor, if_exists, **kwargs)
208 if func is None:
209 raise NotImplementedError
--> 210 func(frame, name, safe_names, cur)
211 cur.close()
212 con.commit()
/opt/epdFree7.3.2/lib/python2.7/site-packages/pandas-0.10.1-py2.7-linux-x86_64.egg/pandas/io/sql.pyc in _write_sqlite(frame, table, names, cur)
219 table, col_names, wildcards)
220 data = [tuple(x) for x in frame.values]
--> 221 cur.executemany(insert_query, data)
222
223 def _write_mysql(frame, table, names, cur):
InterfaceError: Error binding parameter 0 - probably unsupported type.
我认为问题出在代码行 220 上。如果我尝试:
[tuple(x) for x in df.values]
结果是:
[(1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
可能是逗号给 sqlite db 带来噪音。
我不确定,有人可以给我一个提示吗?