DatabaseError : “not all arguments converted during string formatting” when I use pandas.io.sql.to_sql()

时光总嘲笑我的痴心妄想 提交于 2021-02-08 11:26:59

问题


I have a table:

And I try to use this import this table by sqlalchemy , the code is:

import sqlalchemy as db
import pandas.io.sql as sql

username = 'root'     
password = 'root'     
host = 'localhost'    
port = '3306'         
database = 'classicmodels'   

engine = db.create_engine(f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}')

con = engine.raw_connection() 

#readinto dataframe
df = pd.read_sql(f'SELECT * FROM `{database}`.`offices`;', con)
print(df[:2])
df_append = pd.DataFrame([{'officeCode': 8,'city':'Taipei',
                           'phone':'1234567891','addressLine1':'Taipei DaAn',
                           'addressLine2':'Taipei DaAn2','state':'Taipei',
                           'country':'Taiwan','postalCode':'123','territory':'Asia'}])
df2 = pd.concat([df,df_append],ignore_index=True)
sql.to_sql(frame = df2, name='proj55.address_book5',con=con, if_exists='append',index=False)

But I always encounter these errors:

  • If I pass con = engine.raw_connection() to to_sql(), I'll get this:

    • DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting
  • If I use con = engine.connect(), and pass to read_sql(), I'll get error:

    • AttributeError: 'Connection' object has no attribute 'cursor'
  • If I pass engine to to_sql() and read_sql(), I'll get this:

    • AttributeError: 'Engine' object has no attribute 'cursor'

What should I do?


回答1:


pandas.read_sql use either:

  • a sqlalchemy connection object engine.connect()
  • a db api object only for sqlite

by using raw_connection(), you have a db api connection object, so pandas believes it's a connection to a sqlite database (as we see in your error FROM sqlite_master WHERE )

you need to use con = engine.connect()

import sqlalchemy
import pandas
engine = sqlalchemy.create_engine('...')
with engine.connect() as conn:
    print('sqla:', list(conn.execute('select * from users')))
    df = pandas.read_sql('select * from users', conn)
    print('df:', df)
    df.to_sql('users2', conn)
    print('sqla:', list(engine.connect().execute('select * from users2')))

outputs:

sqla: [(1, 'toto'), (2, 'titi'), (3, 'tutu'), (4, 'tata')]
df:    id  name
0   1  toto
1   2  titi
2   3  tutu
3   4  tata
sqla: [(1, 'toto'), (2, 'titi'), (3, 'tutu'), (4, 'tata')]

as expected




回答2:


you have to put list.string.sql fro it's given value n-1



来源:https://stackoverflow.com/questions/63675368/databaseerror-not-all-arguments-converted-during-string-formatting-when-i-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!