python实现数据导入导出mysql

导入需要的python包

import pymysql
import traceback
import pandas as pd
from sqlalchemy import create_engine
import os
import numpy as np

将sql查询结果导出文件

def mysql_to_file(path_file,sql, host, user, password, db_name, port=3306):
    """将sql查询结果导出文件
    
    param path:文件保存路径+文件名+文件后缀
    param sql:sql查询语句
    param host:mysql连接ip
    param user:用户名
    param password:密码
    param db_name:需要使用的数据库
    param port:端口,默认3306
    return df:查询结果,txt或csv格式
    """
    # 打开数据库连接
    conn = pymysql.connect(host=host, port=port, user=user,password=password,db=db_name)

    try:
        results = pd.read_sql(sql, con=conn)
        print(results.head(10))
        conn.close()
        results.to_csv(path_file, sep='\t', index=False)
        
        return results
    except:
        print("=====错误,请检查sql代码=====")
        # 打印错误信息
        traceback.print_exc()
        # 如果发生错误则回滚
        conn.rollback()

将sql查询结果转成DataFrame

def mysql_to_df(sql, host, user, password, db_name, port=3306):
    """将sql查询结果转成DataFrame
    
    param sql:sql查询语句
    param host:mysql连接ip
    param user:用户名
    param password:密码
    param db_name:需要使用的数据库
    param port:端口,默认3306
    return df:查询结果,DateFrame格式
    """
    # 打开数据库连接
    conn = pymysql.connect(host=host, port=port, user=user,password=password,db=db_name)

    try:
        results = pd.read_sql(sql, con=conn)
        print(results.head(10))
        conn.close()
        
        return results
    except:
        print("=====错误,请检查sql代码=====")
        # 打印错误信息
        traceback.print_exc()
        # 如果发生错误则回滚
        conn.rollback()

文件写入mysql

def file_to_mysql(file_name, tab_name, host, user, password, db_name, port=3306):
    """文件写入mysql
    
    param file_name:写入mysql的文件名
    param tab_name:写入数据的表
    param host:mysql连接ip
    param user:用户名
    param password:密码
    param db_name:需要使用的数据库
    param port:端口,默认3306
    return :None
    """
    # 需要确定运行环境上有pymysql
    sql_connect = create_engine(r'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(user,password,host,port,db_name))
    # 读取文件
    df = pd.read_csv(file_name, sep='\t', dtype=np.str)
    # 写入DataFrame
    print("=====文件{0}开始写入{1}.{2}=====".format(file_name, db_name, tab_name))
    pd.io.sql.to_sql(df,tab_name, sql_connect, schema=db_name, if_exists='append', index=False)
    print("=====写入完成=====")

DataFrame写入mysql

def df_to_mysql(df, tab_name, host, user, password, db_name, port=3306):
    """DataFrame写入mysql
    
    param df:需要写入mysql的DataFrame
    param tab_name:写入数据的表
    param host:mysql连接ip
    param user:用户名
    param password:密码
    param db_name:需要使用的数据库
    param port:端口,默认3306
    return :None
    """
    # 需要确定运行环境上有pymysql
    sql_connect = create_engine(r'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(user,password,host,port,db_name))
    # 写入DataFrame
    print("=====DataFrame{0}开始写入{1}.{2}=====".format(df, db_name, tab_name))
    pd.io.sql.to_sql(df,tab_name, sql_connect, schema=db_name, if_exists='append', index=False)
    print("=====写入完成=====")

执行样例

if __name__=="__main__":
    rootpath = 工作路径
    os.chdir(rootpath)
    host = ip
    port = oprt
    user = user 
    password = password 
    db_name = db_name 
    
    file_name = file_name 
    tab_name = tab_name tab_name 
    
    # 文件写入mysql
    #file_to_mysql(file_name, tab_name, host, user, password, db_name)
    # 查询
    sql = """select * from tab_name;"""
    df1 = mysql_to_df(sql, host, user, password, db_name)
    #df2 = mysql_to_file(rootpath+'test_1.txt', sql, host, user, password, db_name)

 

你可能感兴趣的:(Python,mysql)