简体   繁体   English

使用.to_sql()上传DataFrame时出现“SELECT name FROM sqlite_master”错误

[英]"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对象( EngineConnection )或 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.

相关问题 在 sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;'上执行失败: - Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': 使用to_sql函数时出错 - Error while using the to_sql function 使用 to_sql 从 python 将表上传到 SQL 服务器 - Uploading table to SQL Server from python using to_sql 使用python中的to_sql函数将数据帧插入Sql-server DB时出错 - Error while insert dataframe to Sql-server DB using to_sql function in python 使用to_sql将熊猫数据框中的数据导入SQL数据库时,PC挂起 - PC hangs while importing data from pandas dataframe into SQL database using to_sql 使用 to_sql 将 pandas dataframe 导出到访问表中生成错误 - exporting pandas dataframe into a access table using to_sql generate error 使用to_sql发送到SQLite数据库时,pandas数据框中的长整数值发生更改 - Long integer values in pandas dataframe change when sent to SQLite database using to_sql UnicodeEncodeError: 在 python 中使用 to_sql 时 - UnicodeEncodeError: while using to_sql in python to_sql的数据透视图数据框 - Pivot Dataframe for to_sql 使用 to_sql 将 Pandas dataframe 中的数据批量插入 Sybase 数据库表失败 - Failing bulk insert data from Pandas dataframe into Sybase database table using to_sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM