{ "cells": [ { "cell_type": "code", "execution_count": 8, "id": "4ba86c3e", "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": 9, "id": "5db9d7fd", "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": 10, "id": "c62fc147", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Open-architected analyzing portal'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fake.catch_phrase()" ] }, { "cell_type": "code", "execution_count": 16, "id": "8d091b58", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (3, 'Phas', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (3, 'Bala', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (4, 'Syne', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (2, 'Cust', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (2, 'Stre', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (2, 'Orga', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (2, 'Ergo', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (2, 'Clon', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (2, 'Pola', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES (1, 'Opti', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\n", "10条数据插入成功!\n" ] } ], "source": [ "\n", "\n", "# 3. 循环生成10条数据并插入\n", "for _ in range(10):\n", " # 生成随机parent_id(1-4)\n", " random_parent_id = random.randint(1, 4)\n", " # 生成随机名称(faker的word/name方法,这里用catch_phrase生成更贴合“页面名称”的随机词)\n", " random_name = fake.catch_phrase()[:4] # 限制长度避免超出字段长度\n", " sql = insert_sql.format(random_parent_id, random_name)\n", " insert_sql = f\"\"\"\n", "INSERT INTO `sys_custom_open_page` \n", "(`parent_id`, `name`, `description`, `page_level`, `biz_type`, `page_type`, \n", "`section_code`, `page_path`, `page_url`, `dept_id`, `del_flag`, `create_by`, \n", "`create_time`, `create_person_id`, `create_person_name`, `update_by`, \n", "`update_time`, `update_person_name`, `update_person_id`) \n", "VALUES ({random_parent_id}, '{random_name}', NULL, '0', 'video', '1', '1245', 'http://www.baidu.com', \n", "NULL, 0, '0', 'xtl', '2026-01-14 17:04:49', 0, 'xtl', 'xtl', \n", "'2026-01-14 17:05:10', 'xtl', 0);\n", "\"\"\"\n", " print(insert_sql)\n", " # 执行插入\n", " # cursor.execute(insert_sql, (random_parent_id, random_name))\n", "\n", "# 4. 提交事务(必须提交才会写入数据库)\n", "# connection.commit()\n", "print(\"10条数据插入成功!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "5cb7f98e", "metadata": {}, "outputs": [], "source": [] } ], "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 }