73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
import configparser
|
|
import pandas as pd
|
|
from sqlalchemy import create_engine
|
|
from urllib.parse import quote_plus
|
|
# 读取配置文件
|
|
config = configparser.ConfigParser()
|
|
config.read('config.ini')
|
|
|
|
# 获取数据库连接配置
|
|
source_config = {
|
|
'host': config.get('source_db', 'host'),
|
|
'user': config.get('source_db', 'user'),
|
|
'password': config.get('source_db', 'password'),
|
|
'database': config.get('source_db', 'database'),
|
|
'port': config.getint('source_db', 'port')
|
|
}
|
|
|
|
target_config = {
|
|
'host': config.get('target_db', 'host'),
|
|
'user': config.get('target_db', 'user'),
|
|
'password': config.get('target_db', 'password'),
|
|
'database': config.get('target_db', 'database'),
|
|
'port': config.getint('target_db', 'port')
|
|
}
|
|
|
|
# 创建数据库引擎
|
|
source_engine = create_engine(
|
|
f"mysql+pymysql://{source_config['user']}:{quote_plus(source_config['password'])}@{source_config['host']}:{source_config['port']}/{source_config['database']}"
|
|
)
|
|
target_engine = create_engine(
|
|
f"mysql+pymysql://{target_config['user']}:{quote_plus(target_config['password'])}@{target_config['host']}:{target_config['port']}/{target_config['database']}"
|
|
)
|
|
|
|
|
|
def process_project_relation():
|
|
"""处理bm_project_relation表数据到bm_agreement_info"""
|
|
# 使用原始SQL查询实现复杂转换逻辑
|
|
sql = """
|
|
SELECT
|
|
(bpr.ID + 500000) AS agreement_id,
|
|
bpr.`CODE` as agreement_code,
|
|
CASE
|
|
WHEN bpr.type = 'xmb' THEN (bpr.unit_id + 4000)
|
|
WHEN bpr.type = 'sgd' THEN (bpr.unit_id + 5000)
|
|
WHEN bpr.type = 'fbs' THEN (bpr.unit_id + 6000)
|
|
WHEN bpr.type = 'hq' THEN (bpr.unit_id + 6000)
|
|
ELSE 0
|
|
END AS unit_id,
|
|
(bpr.PROJECT_ID + 3000) as project_id,
|
|
bpr.IS_SLT,
|
|
bpr.CREATE_TIME
|
|
FROM
|
|
bm_project_relation bpr
|
|
"""
|
|
|
|
try:
|
|
# 执行查询并获取结果
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('bm_agreement_info', target_engine,
|
|
if_exists='append', index=False)
|
|
|
|
print(f"成功转换并导入 {len(df)} 条记录到 bm_agreement_info")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"处理 bm_project_relation 时发生错误: {str(e)}")
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
process_project_relation() |