129 lines
3.4 KiB
Python
129 lines
3.4 KiB
Python
# -*- encoding:utf-8 -*-
|
||
|
||
'''
|
||
@Author : dingjiawen
|
||
@Date : 2023/5/20 16:32
|
||
@Usage :
|
||
@Desc : 随机向维度表中插入数据
|
||
'''
|
||
|
||
import pymysql
|
||
import pandas as pd
|
||
from pymysql.connections import Connection
|
||
import os
|
||
import random
|
||
import shutil
|
||
import math
|
||
|
||
root = r"E:\面试准备\interview-prepare\个人总结\04大数据\09项目\大型旋转机组健康管理系统软件\数仓\数仓建设\配置类数据库建立\20230327092414/"
|
||
root_file = os.path.join(root, "data_csv\\")
|
||
|
||
if not os.path.exists(root_file):
|
||
os.makedirs(root_file)
|
||
|
||
|
||
def createWindTypeInfo():
|
||
windTypeInfo = []
|
||
|
||
for wt_no in range(25):
|
||
windTypeInfo.append(("jingbian_siqi_" + str(wt_no), "jingbian_siqi", random.randint(0, 4)))
|
||
windTypeInfo.append(("huanengsantanghu_" + str(wt_no), "huanengsantanghu", random.randint(0, 4)))
|
||
windTypeInfo.append(("bandacheng_" + str(wt_no), "bandacheng", random.randint(0, 4)))
|
||
return windTypeInfo
|
||
pass
|
||
|
||
|
||
def createWindTypeDetail():
|
||
windTypeDetail = []
|
||
for type_id in range(5):
|
||
windTypeDetail.append(
|
||
(type_id, random.randint(1000, 2000), random.randint(20000, 50000), random.randint(5000, 10000),
|
||
random.randint(1000, 5000), random.randint(10000, 50000), random.randint(70, 100))
|
||
)
|
||
return windTypeDetail
|
||
pass
|
||
|
||
|
||
def createLocationInfo():
|
||
locationInfo = []
|
||
|
||
locationInfo.append(
|
||
("jingbian_siqi", "靖边四期", 100.65, 82.46, 20000, 20)
|
||
)
|
||
|
||
locationInfo.append(
|
||
("huanengsantanghu", "华能三塘湖", 110.65, 67.46, 20000, 30)
|
||
)
|
||
|
||
locationInfo.append(
|
||
("bandacheng", "扳达城", 60.65, 103.46, 16000, 10)
|
||
)
|
||
|
||
return locationInfo
|
||
pass
|
||
|
||
|
||
def getConnection():
|
||
# 打开数据库连接
|
||
try:
|
||
conn = pymysql.connect(host='Ding202', user='root', passwd='123456', port=3306, database='rt_phm_table')
|
||
print('连接成功!')
|
||
except:
|
||
print('something wrong!')
|
||
return conn
|
||
|
||
def insert_type_detail(conn: Connection, if_clear=True):
|
||
windTypeDetail = createWindTypeDetail()
|
||
cs = conn.cursor() # 获取光标
|
||
if if_clear:
|
||
cs.execute("truncate table wind_type_detail")
|
||
cs.executemany("insert into wind_type_detail values(%s,%s,%s,%s,%s,%s,%s)", windTypeDetail)
|
||
conn.commit()
|
||
cs.close()
|
||
conn.close()
|
||
print('OK')
|
||
pass
|
||
|
||
def insert_location(conn: Connection, if_clear=True):
|
||
locationInfo = createLocationInfo()
|
||
cs = conn.cursor() # 获取光标
|
||
if if_clear:
|
||
cs.execute("truncate table wind_location_info")
|
||
cs.executemany("insert into wind_location_info values(%s,%s,%s,%s,%s,%s)", locationInfo)
|
||
conn.commit()
|
||
cs.close()
|
||
conn.close()
|
||
print('OK')
|
||
pass
|
||
|
||
def insert_type_info(conn: Connection, if_clear=True):
|
||
|
||
windType_info = createWindTypeInfo()
|
||
cs = conn.cursor() # 获取光标
|
||
if if_clear:
|
||
cs.execute("truncate table wind_type_info")
|
||
|
||
# 注意这里使用的是executemany而不是execute,下边有对executemany的详细说明
|
||
cs.executemany("insert into wind_type_info values(%s,%s,%s)", windType_info)
|
||
|
||
conn.commit()
|
||
cs.close()
|
||
conn.close()
|
||
print('OK')
|
||
|
||
|
||
def insert_data():
|
||
conn = getConnection()
|
||
insert_type_info(conn)
|
||
insert_location(conn)
|
||
insert_type_detail(conn)
|
||
pass
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# nameChange()
|
||
# loadData()
|
||
# insert_data()
|
||
conn = getConnection()
|
||
insert_location(conn)
|