Procházet zdrojové kódy

Merge branch 'dev' of http://git.fast-fun.cn:92/lmstack/data_analyze_platform into dev

qingfeng před 2 roky
rodič
revize
67564e2c03
1 změnil soubory, kde provedl 19 přidání a 9 odebrání
  1. 19 9
      LIB/MIDDLE/AlgoDemo/Demo/V_1_0_0/Main/main.py

+ 19 - 9
LIB/MIDDLE/AlgoDemo/Demo/V_1_0_0/Main/main.py

@@ -54,18 +54,30 @@ if __name__ == '__main__':
     logger.info("{}, 算法开始".format(str(os.getpid())))
     
     try:    
-        # 连接数据库的两种方式
-        # 方式一:新dataframe写入数据库时,采用该方式可以不需要写sql语句;
-        #    该方式无法对数据库进行修改;
+        # 创建数据库连接池
         
         db_engine_1 = create_engine(
             "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
                 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)
+        
+        sql = "update all_fault_info_copy set update_time=:time,end_time=:end_time where product_id=:sn and end_time=:end_time and code=:code"
+        params = {"time":datetime.datetime.now(), "end_time":end_time, "sn":sn, "code":'C493'}
+        session.execute(sql,params=params)
+        session.commit()
+        session.close()
+
+
 
         # 准备算法数据
         
@@ -120,9 +132,7 @@ if __name__ == '__main__':
                     
                 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))
             except Exception as e :
                 logger.error(traceback.format_exc)