{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-27T08:11:08.133652Z",
     "start_time": "2024-12-27T08:11:08.099503Z"
    }
   },
   "source": [
    "import pymysql\n",
    "\n",
    "# db = pymysql.connect(host=\"frp-air.top\", port=43458, user=\"stock\", password=\"c&J#USr9~J5hrUiP\", database=\"stock\")\n",
    "db = pymysql.connect(host=\"localhost\", port=3307, user=\"test\", password=\"test\", database=\"main\")"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "7fdf7baf7e494f5d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-27T08:11:43.530387Z",
     "start_time": "2024-12-27T08:11:43.527288Z"
    }
   },
   "source": "cursor = db.cursor()",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-27T08:29:21.672366Z",
     "start_time": "2024-12-27T08:29:13.248490Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('../document/Tushare股票信息.csv',\n",
    "                 names=['ts_code', 'symbol', 'name', 'area', 'industry', 'fullname', 'enname', 'cnspell', 'market',\n",
    "                        'exchange', 'curr_type', 'list_status', 'list_date', 'act_name', 'act_ent_type'])\n",
    "# ts_code列按英文句号分割，将分割后的两个字符串交换位置\n",
    "df['ts_code'] = df['ts_code'].str.split('.').str[::-1].str.join('.')\n",
    "# 只留下ts_code、name、market，fullname列\n",
    "df = df[['ts_code', 'name', 'market', 'fullname']]\n",
    "# 遍历所有行，生成插入sql语句\n",
    "for i in range(len(df)):\n",
    "    sql = \"insert into target(code, name, description, market, type) values ('%s','%s','%s','%s','%s') on duplicate key update name = values(name), description = values(description), market = values(market), type = values(type)\" % (\n",
    "        df.iloc[i]['ts_code'], df.iloc[i]['name'], df.iloc[i]['fullname'], df.iloc[i]['market'], 'stock'\n",
    "    )\n",
    "    cursor.execute(sql)\n",
    "db.commit()"
   ],
   "id": "bff56fafd1219dfa",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-27T08:29:45.830427Z",
     "start_time": "2024-12-27T08:29:44.310167Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_csv('../document/ETF信息.csv', usecols=[0, 1, 2], names=['index', 'code', 'name'], index_col='index')\n",
    "for i in range(len(df)):\n",
    "    sql = \"insert into target(code, name, type) values ('%s','%s','%s') on duplicate key update name = values(name), type = values(type)\" % (\n",
    "        df.iloc[i]['code'], df.iloc[i]['name'], 'etf'\n",
    "    )\n",
    "    cursor.execute(sql)\n",
    "db.commit()"
   ],
   "id": "577d97871a0acbe7",
   "outputs": [],
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "id": "9cc56d42dac5ea9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-27T08:49:40.805003Z",
     "start_time": "2024-12-27T08:49:40.762113Z"
    }
   },
   "source": [
    "db.close()"
   ],
   "outputs": [],
   "execution_count": 26
  }
 ],
 "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
}
