12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import pandas as pd
- from sqlalchemy import create_engine
- from base_class import BaseVariableFunction
- from base_class import *
- baseclass = BaseVariableFunction(__file__)
- # 数据库连接信息
- username = 'root'
- password = '123456'
- # password = '123456abc'
- host = 'localhost'
- database = 'mine_01'
- table_name = 'table_01'
- # 创建数据库引擎
- engine = create_engine(f'mysql+pymysql://{username}:{password}@{host}/{database}')
- print("engine=",engine)
- con = engine.connect()#创建连接
- print(con)
-
- # 创建一个示例DataFrame
- # df = pd.DataFrame({
- # 'column1': [1, 2, 3],
- # 'column2': ['a', 'b', 'c']
- # })
- df = pd.read_excel(baseclass.library_path/f"a_case.xlsx",dtype =object)
- # df = pd.read_excel (f"a_case.xlsx",dtype =object)
- df = df.astype({
- "time":int,
- })
-
- print(df.head(5))
- # 将DataFrame写入MySQL表
- try:
- df.to_sql(name=table_name, con=con, if_exists='replace', index=False)
- print(f"DataFrame successfully written to table {table_name}")
- except Exception as e:
- print(f"An error occurred: {e}")
-
|