{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b82fd271", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "import snowflake\n", "from faker import Faker\n", "fake = Faker('zh_CN')\n", "import random\n", "# 创建数据库连接\n", "connection = pymysql.connect(\n", "host='192.168.0.244',\n", "port=3300,\n", "user='root',\n", "password='hxyd1109',\n", "database='school_server',\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": 6, "id": "977b832d", "metadata": {}, "outputs": [], "source": [ "sql='select * from class_attend limit 0,10'" ] }, { "cell_type": "code", "execution_count": 7, "id": "0318e673", "metadata": {}, "outputs": [], "source": [ "cursor.execute(sql)\n", "result = cursor.fetchall()" ] }, { "cell_type": "code", "execution_count": 8, "id": "7af746d4", "metadata": {}, "outputs": [], "source": [ "result = create_table(cursor,result)" ] }, { "cell_type": "code", "execution_count": 11, "id": "5bb61d61", "metadata": {}, "outputs": [], "source": [ "sql='''\n", "UPDATE class_attend \n", "SET \n", " class_s_time = DATE_ADD(class_s_time, INTERVAL 28 DAY),\n", " class_e_time = DATE_ADD(class_e_time, INTERVAL 28 DAY),\n", " start_time = DATE_ADD(start_time, INTERVAL 28 DAY),\n", " end_time = DATE_ADD(end_time, INTERVAL 28 DAY),\n", " attend_date = DATE_ADD(attend_date, INTERVAL 28 DAY),\n", " class_s_times = DATE_ADD(class_s_times, INTERVAL 28 DAY),\n", " class_e_times = DATE_ADD(class_e_times, INTERVAL 28 DAY);\n", "'''\n", "cursor.execute(sql)\n", "connection.commit()" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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 }