{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b82fd271",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql\n",
    "import snowflake\n",
    "from faker import Faker\n",
    "import random\n",
    "import datetime\n",
    "fake = Faker('zh_CN')\n",
    "import random\n",
    "# 创建数据库连接\n",
    "connection = pymysql.connect(\n",
    "host='192.168.0.244',\n",
    "port=3306,\n",
    "user='root',\n",
    "password='Abcd@123456',\n",
    "database='hongxx',\n",
    "charset='utf8'\n",
    ")\n",
    "# 创建游标对象\n",
    "cursor = connection.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "759c550a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_table(cursor,result):\n",
    "    \n",
    "    data = []\n",
    "    for i in result:\n",
    "        list_list = list(i)\n",
    "        des=cursor.description  # 获取表详情，字段名，长度，属性等\n",
    "        t = \",\".join([item[0] for item in des])\n",
    "        table_head = t.split(',')  # # 查询表列名 用,分割\n",
    "    \n",
    "        dict_result = dict(zip(table_head, list_list))  # 打包为元组的列表 再转换为字典\n",
    "        data.append(dict_result)  # 将字典添加到list_result中\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b717043a",
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = 'select * from sys_user where dept_id=100'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b4b373b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute(sql)\n",
    "rows = cursor.fetchall()\n",
    "result = create_table(cursor,rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "394495d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 3,\n",
       " 4,\n",
       " 6,\n",
       " 24,\n",
       " 31,\n",
       " 308,\n",
       " 909,\n",
       " 917,\n",
       " 918,\n",
       " 1145,\n",
       " 3690,\n",
       " 3698,\n",
       " 3828,\n",
       " 3842,\n",
       " 3843,\n",
       " 3844,\n",
       " 3895,\n",
       " 3899,\n",
       " 3912,\n",
       " 3913,\n",
       " 3935,\n",
       " 3936,\n",
       " 3988,\n",
       " 5440,\n",
       " 5492,\n",
       " 7696,\n",
       " 7713,\n",
       " 7714,\n",
       " 7723,\n",
       " 7728,\n",
       " 2059454179031199746]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i[\"user_id\"] for i in result]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "325e4664",
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in result:\n",
    "    userid = item[\"user_id\"]\n",
    "  \n",
    "    sql2 = f\"update sys_user set dept_id=100, team_user_type ='BACKEND' where user_id={userid}\"\n",
    "    cursor.execute(sql2)\n",
    "connection.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f62b4e7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in result:\n",
    "    userid = item[\"user_id\"]\n",
    "    sql2 = f\"update sys_user_dept set dept_id=100, person_type='hx_sys' where user_id={userid}\"\n",
    "    cursor.execute(sql2)\n",
    "connection.commit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dcf47b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in result:\n",
    "    userid = item[\"user_id\"]\n",
    "    sql2 = f\"update sys_user_role set dept_id=100 where user_id={userid}\"\n",
    "    cursor.execute(sql2)\n",
    "connection.commit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
