python pandas_mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pandas_mysql相关的知识,希望对你有一定的参考价值。

from sqlalchemy import create_engine
MYSQL_USER = "root"
MYSQL_PASS = "root"
MYSQL_HOST = "localhost"
MYSQL_PORT = 3306
MYSQL_DB = "db"
MYSQL_CHARSET = "utf8mb4"
DB_URL = "mysql+pymysql://%s:%s@%s:%s/%s?charset=%s" % (MYSQL_USER,MYSQL_PASS,MYSQL_HOST,MYSQL_PORT,MYSQL_DB,MYSQL_CHARSET)

MYSQL_INFO = {
    "user": "root",
    "pass": "root",
    "host": "192.168.0.4",
    "port": 3306,
    "db": "dbtest",
    "param": "charset=utf8mb4"
}
DB_URL = "mysql+pymysql://{user}:{pass}@{host}:{port}/{db}?{param}".format(**MYSQL_INFO)


engine = create_engine('mysql+pymysql://root:root@localhost:3306/database?charset=utf8', echo=True)

conn = engine.connect()

df.to_sql(name='table_name', con=engine, if_exists = 'append', index=False)
df.to_sql(name='table_name', con=conn, if_exists = 'append', index=False)

# 插入大量数据
import io  
  
output = io.StringIO()  
# ignore the index  
df_a.to_csv(output, sep='\t',index = False, header = False)  
output.getvalue()  
# jump to start of stream  
output.seek(0)  
  
connection = engine.raw_connection() #engine 是 from sqlalchemy import create_engine  
cursor = connection.cursor()  
# null value become ''  
cursor.copy_from(output,table_name,null='')  
connection.commit()  
cursor.close()  

以上是关于python pandas_mysql的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python