接车宝派发更新
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-12-17T08:57:05.953495Z",
|
||||
"start_time": "2025-12-17T08:57:05.580269Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"import mysql.connector\n",
|
||||
"from mysql.connector import Error\n",
|
||||
"\n",
|
||||
"# 连接信息\n",
|
||||
"HS_DB_Config = {\n",
|
||||
" 'host': \"f6-public.rwlb.rds.aliyuncs.com\",\n",
|
||||
" 'user': \"rw_operation_data_relay\",\n",
|
||||
" 'password': \"m+q5Z4%IVuF9bf\",\n",
|
||||
" 'database': \"f6operation_data_relay\"\n",
|
||||
" } # 衡时数据库链接配置-mysql\n",
|
||||
"table_name = \"saas_period_product_fenmu\" # 请替换为实际的表名\n",
|
||||
"# table_name = \"yida_process_time_statistics\"\n",
|
||||
"\n",
|
||||
"# 连接\n",
|
||||
"connection = mysql.connector.connect(\n",
|
||||
" host=HS_DB_Config[\"host\"],\n",
|
||||
" user=HS_DB_Config[\"user\"],\n",
|
||||
" password=HS_DB_Config[\"password\"],\n",
|
||||
" database=HS_DB_Config[\"database\"]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(f\"成功连接 {HS_DB_Config['database']}\")\n",
|
||||
"cursor = connection.cursor()\n",
|
||||
"\n",
|
||||
"# 读取Excel文件\n",
|
||||
"df = pd.read_excel(\n",
|
||||
" r\"C:\\Users\\zy187\\Desktop\\应续约信息-商户与商品-数据表格.xlsx\",\n",
|
||||
" sheet_name=\"Sheet1\")\n",
|
||||
"\n",
|
||||
"# 处理空值 - 将NaN/NaT/空字符串统一转为None\n",
|
||||
"df = df.map(lambda x: None if pd.isna(x) or str(x).strip() == '' else x)\n",
|
||||
"\n",
|
||||
"# 生成插入语句\n",
|
||||
"columns = ', '.join(df.columns)\n",
|
||||
"placeholders = ', '.join(['%s'] * len(df.columns))\n",
|
||||
"insert_query = f\"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})\"\n",
|
||||
"\n",
|
||||
"# 批量插入数据,每次1000条\n",
|
||||
"records = [tuple(row) for row in df.values]\n",
|
||||
"batch_size = 1000\n",
|
||||
"total_records = len(records)\n",
|
||||
"inserted_count = 0\n",
|
||||
"\n",
|
||||
"for i in range(0, total_records, batch_size):\n",
|
||||
" batch = records[i:i+batch_size]\n",
|
||||
" cursor.executemany(insert_query, batch)\n",
|
||||
" connection.commit()\n",
|
||||
" inserted_count += len(batch)\n",
|
||||
" print(f\"已成功导入 {inserted_count}/{total_records} 条记录\")\n",
|
||||
"\n",
|
||||
"print(f\"总共成功导入 {inserted_count} 条记录到 {table_name} 表\")\n",
|
||||
"\n",
|
||||
"cursor.close()\n",
|
||||
"connection.close()"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"成功连接 f6operation_data_relay\n",
|
||||
"已成功导入 6/6 条记录\n",
|
||||
"总共成功导入 6 条记录到 saas_period_product_fenmu 表\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 3
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user