文章目录
详细说明
使用pandas
读写数据库的方法(以Mysql为例)如下:
-
首先是打包一个工具函数:
pyimport pandas as pd import numpy as np from sqlalchemy import create_engine, text def get_sql_engine(): # 数据库 mysql_config = { "db": "test_db", "host": "127.0.0.1", "user": "test_user", "password": "test_pass", "port": 3306, } engine = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}".format(mysql_config['user'], mysql_config['password'], mysql_config['host'], mysql_config['port'], mysql_config['db'])) return engine
-
读取数据库
.read_sql()
的方法:pyengine = get_sql_engine() # ======== 写入 =========== data_df = pd.DataFrame(np.random.random(size=(100, 5))) data_df.to_sql('test_data_df', con=engine, if_exists='replace', index=False)
-
读取数据库
read_sql()
的方法:pydata_df = pd.read_sql_query(text('select * from test_data_df'), con=engine.connect())
在读取的时候容易报错,有几个要点:
-
首先导入
from sqlalchemy import text
,然后以text(sql语句)
的形式传入第一个参数 -
在第二个参数需要使用
engine.connect()
-
示例程序
py
import pandas as pd
import numpy as np
from sqlalchemy import create_engine, text
def get_sql_engine():
# 数据库
mysql_config = {
"db": "test_db",
"host": "127.0.0.1",
"user": "test_user",
"password": "test_pass",
"port": 3306,
}
engine = create_engine(
"mysql+pymysql://{}:{}@{}:{}/{}".format(mysql_config['user'], mysql_config['password'], mysql_config['host'],
mysql_config['port'], mysql_config['db']))
return engine
def main():
engine = get_sql_engine()
# ======== 写入 ===========
data_df = pd.DataFrame(np.random.random(size=(100, 5)))
data_df.to_sql('test_data_df', con=engine, if_exists='replace', index=False)
# ======== 读取 ===========
data_df = pd.read_sql_query(text('select * from test_data_df'), con=engine.connect())
if __name__ == '__main__':
main()