|
@@ -54,18 +54,26 @@ if __name__ == '__main__':
|
|
logger.info("{}, 算法开始".format(str(os.getpid())))
|
|
logger.info("{}, 算法开始".format(str(os.getpid())))
|
|
|
|
|
|
try:
|
|
try:
|
|
- # 连接数据库的两种方式
|
|
|
|
- # 方式一:新dataframe写入数据库时,采用该方式可以不需要写sql语句;
|
|
|
|
- # 该方式无法对数据库进行修改;
|
|
|
|
|
|
+ # 创建数据库连接池
|
|
|
|
|
|
db_engine_1 = create_engine(
|
|
db_engine_1 = create_engine(
|
|
"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
|
|
"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
|
|
user1, password1, host1, port1, db1
|
|
user1, password1, host1, port1, db1
|
|
))
|
|
))
|
|
|
|
+ Session = sessionmaker(bind=db_engine)
|
|
|
|
+ # 取数示例
|
|
|
|
+ sql = "select * from table"
|
|
|
|
+ df_diag_ram = pd.read_sql(sql, db_engine)
|
|
|
|
|
|
- # 方式二:该方式可以通过写update SQL语句,对数据库中的数据进行修改
|
|
|
|
- conn = pymysql.connect(host=host2, port=port2, user=user2, password=password2, database=db2)
|
|
|
|
- cursor = conn.cursor()
|
|
|
|
|
|
+ # 修改数据库示例
|
|
|
|
+ session = Session()
|
|
|
|
+ sql = 'insert into table (data) values (:data)'
|
|
|
|
+ params = {'data':1}
|
|
|
|
+ session.execute(sql,params=params)
|
|
|
|
+ session.commit()
|
|
|
|
+ session.close()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
# 准备算法数据
|
|
# 准备算法数据
|
|
|
|
|
|
@@ -120,9 +128,7 @@ if __name__ == '__main__':
|
|
|
|
|
|
else:
|
|
else:
|
|
# 修改
|
|
# 修改
|
|
- sql = '''update test_tb set data={} where sn='{}' '''.format(df_res['data'].values[0], (df_res['sn'].values[0]))
|
|
|
|
- cursor.execute(sql)
|
|
|
|
- conn.commit()
|
|
|
|
|
|
+ pass
|
|
logger.info("{} done".format(sn))
|
|
logger.info("{} done".format(sn))
|
|
except Exception as e :
|
|
except Exception as e :
|
|
logger.error(traceback.format_exc)
|
|
logger.error(traceback.format_exc)
|