Python版本 – 2.7.6
熊猫版 – 0.17.1
MysqLdb版本 – 1.2.5
DataFrame.to_sql()抛出pandas.io.sql.DatabaseError:sql执行失败’SELECT name FROM sqlite_master WHERE type =’table’AND name =?;’:不是在字符串格式化过程中转换的所有参数
Python代码段
con = MysqLdb.connect('localhost', 'root', '', 'product_Feed')
cur = con.cursor()
cur.execute("SELECT VERSION()")
connection_result = cur.fetchall()
print connection_result[0][0] #It prints 5.5.44-0ubuntu0.14.04.1
table_column = ['A', 'B', 'C']
created_data = numpy.array([numpy.arange(10)]*3).T
df = pandas.DataFrame(data=created_data ,columns=table_column)
df.to_sql('test_table', con)
执行df.to_sql(‘test_table’,con)行时出错.
错误详情
File "/home/yogi/yogi/mlPython/product_Feed/etl_pf/process_data.py", line 298, in render_df
df.to_sql('test_table', con)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/core/generic.py", line 1003, in to_sql
dtype=dtype)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 569, in to_sql
chunksize=chunksize, dtype=dtype)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 1640, in to_sql
table.create()
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 685, in create
if self.exists():
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 673, in exists
return self.pd_sql.has_table(self.name, self.schema)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 1653, in has_table
return len(self.execute(query, [name,]).fetchall()) > 0
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 1554, in execute
raise_with_traceback(ex)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/io/sql.py", line 1543, in execute
cur.execute(*args)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/MysqLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
pandas.io.sql.DatabaseError: Execution Failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting
如果有人可以建议一些工作,那将是很大的帮助.我不想用cursor.execute()来试试这个
解决方法:
参数:
con:sqlAlchemy引擎或DBAPI2连接(传统模式)
使用sqlAlchemy可以使用该库支持的任何数据库.如果是DBAPI2对象,则仅支持sqlite3.
从版本0.19.0开始不推荐使用:如果不使用sqlAlchemy,则’sqlite’是唯一受支持的选项.
如果使用sqlAlchemy而不是MysqLdb,那就没问题了.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。