简体   繁体   中英

Write Pandas Dataframe via SQLAlchemy in MySQL database

I am trying to write a pandas dataframe into a mysql database using pandas. This works perfect for me using

to_sql

But what I want is to write the date, ticker and the adj_close in the table test on my own using sqlalchemy. Somehow I tried doing it but it is not working. using the following:

ins = test.insert()

ins = test.insert().values(date=DataLevels['Date'], ticker=DataLevels['ticker'], adj_close=DataLevels['adj_close'])
ins.compile().params
mysql_engine.execute(ins)

Using it I receive the following error message:

(mysql.connector.errors.ProgrammingError) Failed processing pyformat-parameters; Python 'series' cannot be converted to a MySQL type [SQL: 'INSERT INTO test (date, ticker, adj_close) VALUES (%(date)s, %(ticker)s, %(adj_close)s)'] [parameters: {'ticker': 0
AAPL

Does anbody has a clue how that can work? The code is below wothout the code parts mentioned above:

import sqlalchemy as sqlal
import pandas_datareader.data as pdr
import pandas as pd

mysql_engine = sqlal.create_engine('mysql+mysqlconnector://xxx')
mysql_engine.raw_connection()

metadata = sqlal.MetaData()

test = sqlal.Table('test', metadata,
                                sqlal.Column('date', sqlal.DateTime, nullable=True),
                                sqlal.Column('ticker', sqlal.String(10), sqlal.ForeignKey("product.ticker"), nullable=True), 
                                sqlal.Column('adj_close', sqlal.Float, nullable=True),
                                ) 

metadata.create_all(mysql_engine) 

DataLevels = pdr.DataReader(['ACN','AAPL','^GDAXI'], 'yahoo', '2016-11-19', '2016-12-1')
DataLevels = pd.melt(DataLevels['Adj Close'].reset_index(), id_vars='Date', value_name='adj_close', var_name='minor').rename(columns={'minor': 'ticker'})

要在单个INSERT插入多行,请不要为每个列传递Series ,而要传递记录列表。

test.insert().values(DataLevels[['Date', 'ticker', 'adj_close']].to_dict('records'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM