{ "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_video',\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": 4, "id": "b717043a", "metadata": {}, "outputs": [], "source": [ "sql = 'select * from video_info'" ] }, { "cell_type": "code", "execution_count": 5, "id": "b4b373b8", "metadata": {}, "outputs": [], "source": [ "cursor.execute(sql)\n", "rows = cursor.fetchall()\n", "result = create_table(cursor,rows)" ] }, { "cell_type": "code", "execution_count": 8, "id": "84652b4b", "metadata": {}, "outputs": [], "source": [ "result\n", "# http://192.168.0.244:88/record/original-files/video/522e4c55fc804decafdd0a62e5a1bf74.mp4\n", "for item in result:\n", " if item[\"video_url\"].startswith(\"http://192.168.0.244:88\"):\n", " video_url = item[\"video_url\"].replace(\"http://192.168.0.244:88\",\"/mp4\")\n", " sql = f\"update video_info set video_url='{video_url}' where id={item['id']}\"\n", " cursor.execute(sql)" ] }, { "cell_type": "code", "execution_count": null, "id": "0b4381f4", "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n", "\u001b[1;31m请查看单元格中的代码,以确定故障的可能原因。\n", "\u001b[1;31m单击此处了解详细信息。\n", "\u001b[1;31m有关更多详细信息,请查看 Jupyter log。" ] } ], "source": [ "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 }