|
@@ -336,18 +336,74 @@ def _update_shandong_new_yoy_origin(region_name):
|
|
|
log.info(f"{region_name} 新数据更新数: {result.rowcount}")
|
|
|
return result.rowcount
|
|
|
|
|
|
+def generate_sql_file(results, prov_name, year_month):
|
|
|
+ filename = f"export_{prov_name}_{year_month}.sql"
|
|
|
+
|
|
|
+ with open(filename, 'w', encoding='utf-8') as f:
|
|
|
+ for row in results:
|
|
|
+ # 过滤掉不需要的字段(如 id)
|
|
|
+ filtered_row = {k: v for k, v in row.items() if k != 'id'}
|
|
|
+
|
|
|
+ # 构建INSERT语句
|
|
|
+ columns = ', '.join([f"`{k}`" for k in filtered_row.keys()])
|
|
|
+ values = ', '.join([
|
|
|
+ f"'{v}'" if isinstance(v, (str,)) else str(v) if v is not None else 'NULL'
|
|
|
+ for v in filtered_row.values()
|
|
|
+ ])
|
|
|
+
|
|
|
+ sql = f"INSERT INTO `t_yujin_crossborder_prov_commodity_trade` ({columns}) VALUES ({values});\n"
|
|
|
+ f.write(sql)
|
|
|
+
|
|
|
+ log.info(f"成功生成SQL文件:{filename}")
|
|
|
+
|
|
|
+def get_commodity_trade_by_prov_year_month(prov_name, year_month):
|
|
|
+ connection = None
|
|
|
+ try:
|
|
|
+ # 连接数据库
|
|
|
+ db_config = DB_CONFIG.copy()
|
|
|
+ db_config['password'] = get_decrypted_password()
|
|
|
+ connection = pymysql.connect(**db_config)
|
|
|
+
|
|
|
+ # 使用 DictCursor 来获取字典形式的结果
|
|
|
+ with connection.cursor(pymysql.cursors.DictCursor) as cursor:
|
|
|
+ # 执行查询
|
|
|
+ sql = "SELECT * FROM t_yujin_crossborder_prov_commodity_trade WHERE prov_name = %s AND crossborder_year_month = %s"
|
|
|
+ cursor.execute(sql, (prov_name, year_month))
|
|
|
+
|
|
|
+ # 获取结果
|
|
|
+ results = cursor.fetchall()
|
|
|
+
|
|
|
+ # 生成SQL文件
|
|
|
+ if results:
|
|
|
+ generate_sql_file(results, prov_name, year_month)
|
|
|
+
|
|
|
+ return results
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ log.error(f"get_commodity_trade_by_prov_year_month error: {str(e)}")
|
|
|
+ return None
|
|
|
+ finally:
|
|
|
+ if connection:
|
|
|
+ connection.close()
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
- commodity_code, commodity_name_fix = get_commodity_id('农产品')
|
|
|
- print(commodity_code, commodity_name_fix)
|
|
|
+ res = get_commodity_trade_by_prov_year_month('江苏省','2025-04')
|
|
|
+ if res is not None:
|
|
|
+ print(f"查询到 {len(res)} 条记录")
|
|
|
+ else:
|
|
|
+ print("未查询到任何记录或发生错误")
|
|
|
+
|
|
|
+ # commodity_code, commodity_name_fix = get_commodity_id('农产品')
|
|
|
+ # print(commodity_code, commodity_name_fix)
|
|
|
# check_year, check_month = 2024, 4
|
|
|
# count = get_code_exist(f'{check_year}-{check_month:02d}', "340000")
|
|
|
# print(count)
|
|
|
|
|
|
# 新表更新地级市同比
|
|
|
- for province in provinces:
|
|
|
- update_shandong_yoy(province)
|
|
|
+ # for province in provinces:
|
|
|
+ # update_shandong_yoy(province)
|
|
|
|
|
|
# 旧表更新省份同比
|
|
|
# for province in provinces:
|
|
|
# update_shandong_yoy_origin(province)
|
|
|
- log.info("同比sql处理完成")
|
|
|
+ # log.info("同比sql处理完成")
|