[英]"SELECT name FROM sqlite_master" error while uploading DataFrame using .to_sql()
Context : I'd like to send a concatenated data frame (I joined several dataframes from individual stock data) into a MySQL database, however, I can't seem to create a table and send the data there上下文:我想将一个连接的数据框(我从单个股票数据中加入了几个数据框)发送到 MySQL 数据库中,但是,我似乎无法创建一个表并将数据发送到那里
Problem : When I run this code df.to_sql(name='stockdata', con=con, if_exists='append', index=False)
(source: Writing a Pandas Dataframe to MySQL ), I keep getting this error: 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
.问题:当我运行这段代码
df.to_sql(name='stockdata', con=con, if_exists='append', index=False)
(来源: Writing a Pandas Dataframe to MySQL ),我不断收到这个错误: 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
。
I'm new to MySQL as well so any help is very welcome!我也是 MySQL 的新手,所以非常欢迎任何帮助! Thank you
谢谢
from __future__ import print_function
import pandas as pd
from datetime import date, datetime, timedelta
import numpy as np
import yfinance as yf
import mysql.conector
import pymysql as pymysql
import pandas_datareader.data as web
from sqlalchemy import create_engine
import yahoo_fin.stock_info as si
######################################################
# PyMySQL configuration
user = '...'
passw = '...'
host = '...'
port = 3306
database = 'stockdata'
con.cursor().execute("CREATE DATABASE IF NOT EXISTS {0} ".format(database))
con = pymysql.connect(host=host,
port=port,
user=user,
passwd=passw,
db=database,
charset='utf8')
df.to_sql(name='stockdata', con=con, if_exists='append', index=False)
.to_sql()
expects the second argument to be either a SQLAlchemy Connectable
object ( Engine
or Connection
) or a DBAPI Connection
object. .to_sql()
期望第二个参数是 SQLAlchemy Connectable
对象( Engine
或Connection
)或 DBAPI Connection
对象。 If it is the latter then pandas assumes that it is a SQLite connection.如果是后者,则 pandas 假定它是一个 SQLite 连接。
You need to use SQLAlchemy to create an engine object您需要使用 SQLAlchemy 创建引擎对象
engine = create_engine("mysql+pymysql://…")
and pass that to to_sql()
并将其传递给
to_sql()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.