python Oracle SQL

pyhton Oracle 程序示例

import cx_Oracle

def real_save_to_oracle( lst_file_name, stpf_result_data_dir, dict_area_no ):
    conn = cx_Oracle.connect( "SYSDBA/[email protected]/orcl" )
    cursor = conn.cursor()

    for file_name in lst_file_name:
        str_suffix = file_name[-4:]
        if str_suffix != ".csv":
            continue
        file_path = os.path.join( stpf_result_data_dir, file_name )
        df_data = pd.read_csv( file_path )

        file_name = file_name.split("_")[1]
        for _, row_data in df_data.iterrows():
            area_no = dict_area_no[ file_name ]
            print( area_no, file_name )
            str_time = row_data["Time"]
            d_P_real = row_data["P_use"]
            d_Q_real = row_data["Q_use"]

            str_sql = """select * from WF_STAT_PERIOD_DATA where WINDPLANT_NO=%d and
                        DATA_TIME=to_date( '%s', 'yyyy-MM-dd HH24:mi:ss')""" % ( area_no, str_time )
            cursor.execute( str_sql )
            lst_result = cursor.fetchall()
            print( type( lst_result ), lst_result )
            if len( lst_result ) > 0:
                str_sql = """update WF_STAT_PERIOD_DATA set AVG_TEMP=%f, AVG_WIND_SPEED=%f where WINDPLANT_NO=%d and
                        DATA_TIME=to_date( '%s', 'yyyy-MM-dd HH24:mi:ss')""" % ( d_P_real, d_Q_real, area_no, str_time )
                cursor.execute( str_sql )
            else:
                str_sql = """insert into WF_STAT_PERIOD_DATA values
                            ( %d, to_date( '%s', 'yyyy-MM-dd HH24:mi:ss'), 0, 0, 0, 0, 0, %f,
                            %f, 0, 1 )""" % ( area_no, str_time, d_Q_real, d_P_real )
                cursor.execute( str_sql )
            cursor.execute( "COMMIT" )
            time.sleep( 0.01 )
    # select * from PV_AREA_PF_PERIOD_DATA where AREA_NO=37 and MODEL_NO = 0 and TIME_SCALE=-1;
    cursor.close()
    conn.close()
    return None

猜你喜欢

转载自blog.51cto.com/weiyuqingcheng/2517341