繁体   English   中英

mysql.connector 类似于 SQLAlchemy 的 pd.to_sql

[英]mysql.connector analogue to SQLAlchemy's pd.to_sql

While programming on Python and working with SQL dbs I previously used mysql.connector library, but I found out that it takes a lot of rows of code to get your data into SQL table because you need to write the whole SQL query column by column. From the other side while using pandas there are easy methods working with SQLalchemy library only: - pd.to_sql - pd.read_sql_table (Well, I got errors while using mysql db.cursor() and couldn't find any tutorial besides SQLalchemy + Pandas )。 这两种方法可以让您轻松从 SQL 表中获取 dataframe 并从 Z6555075B5DC747 中创建 SQL 表。

I wonder if there is such analogue in mysql.connector to easily convert dataframe to SQL table and vice versa, since still the syntax of this library for me is more convenient for other actions rather than SQL???

PS MySQL 启动代码仅用于提供信息,并未用于提供的代码,但我需要以某种方式找到类似的

# ------------------- IMPORT -------------------------
import mysql.connector
import pandas as pd
import sqlalchemy

# ------------------- MYSQL + SQL Alchemy -------------------------
mydb = mysql.connector.connect(
    host='host',
    user='user',
    passwd='pass',
    database='db'
)

mycursor = mydb.cursor()
engine = sqlalchemy.create_engine('mysql+mysqldb://user:pass@host/db', pool_recycle=3600)

# ------------------- FUNCTIONS ----------------------
def get_function():
    df = pd.read_html(
        "https://www.url.com")
    df[3].to_sql(name=table, con=engine, index=False, if_exists='replace')


# -------------------- MAIN --------------------------
table = 'table_name'
get_function()
print(pd.read_sql_table(table, engine, columns=[]))

你的问题不是很清楚,所以我不完全确定你想要做什么,但我会尽力回答。

所以 SQLAlchemy 是一个所谓的对象关系映射器(如 Java 表中的 Hibernate 表)和对象之间的映射关系(列、行、行)。

Pandas是一个数据分析库,可以使用SQLAlchemy。 SQLAlchemy 本身支持广泛的数据库,包括 MySQL。

Now I didn't understand whether you'd like to use Pandas + SQLAlchemy + MySQL, or whether you just want a simple way to work with MySQL directly. 在第一种情况下,您可以简单地使用 Pandas,在后一种情况下,您可以直接使用 SQLAlchemy。 Pandas 提供文档SQLAlchemy也提供文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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