{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "# con = create_engine('mysql+pymysql://root:rootroot@172.21.20.14:3306/model_data_test?charset=utf8')\n",
    "import pymysql\n",
    "\n",
    "# 创建连接\n",
    "conn = pymysql.connect(host='172.21.20.14', port=3306, user='root', passwd='rootroot', db='model_data_test', use_unicode=True, charset=\"utf8\")\n",
    "# conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='model_data_test', use_unicode=True, charset=\"utf8\")\n",
    "cursor = conn.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel('/Users/zhangpengcheng/Desktop/tongdun_to_pengcheng.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def myInsert(newList):\n",
    "    '''\n",
    "    数据库插入\n",
    "    :param newList: 传入的列表数据\n",
    "    :return:\n",
    "    '''\n",
    "    try:\n",
    "        sql = \"insert into user_test(data_set_datail_id,model_name,batch_uuid,data_detail) values(%s,%s,%s,%s)\" # 要插入的数据\n",
    "        print(sql)\n",
    "        cursor.executemany(sql,newList) # 执行插入数据\n",
    "\n",
    "        conn.commit()\n",
    "        cursor.close()\n",
    "        conn.close()\n",
    "        print('insert ok')\n",
    "    except Exception as e:\n",
    "        print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "_json = df.to_dict(orient='records')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert into user_test(data_set_datail_id,model_name,batch_uuid,data_detail) values(%s,%s,%s,%s)\n",
      "insert ok\n"
     ]
    }
   ],
   "source": [
    "params = []\n",
    "for item in _json:\n",
    "    _list = []\n",
    "    for k,v in item.items():\n",
    "        if k == 'data_set_datail_id':\n",
    "            _list.append(int(v))\n",
    "        else:\n",
    "            _list.append(v)\n",
    "    params.append(_list)\n",
    "myInsert(tuple(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'params' is not defined",
     "output_type": "error",
     "traceback": [
      "---------------------------------------------------------------------------",
      "NameError                                 Traceback (most recent call last)",
      "<ipython-input-1-a786dfa0970c> in <module>()\n----> 1 params[0]\n",
      "NameError: name 'params' is not defined"
     ]
    }
   ],
   "source": [
    "_json[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data_set_datail_id</th>\n",
       "      <th>model_name</th>\n",
       "      <th>batch_uuid</th>\n",
       "      <th>data_detail</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14</td>\n",
       "      <td>tongdun_feature</td>\n",
       "      <td>cd46dccf-5697-40ea-ad87-dea8fe1db689</td>\n",
       "      <td>{'state': 200, 'value': [{'conditions': [{'dim...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "output_type": "execute_result",
     "metadata": {}
    }
   ],
   "source": [
    "df.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}