添加删除之前数据功能

dev_m
1985682903@qq.com 4 years ago
parent 9549a887f4
commit 8b77b41932

@ -584,7 +584,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
@ -632,10 +632,11 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import pymysql\n",
"class MyDB:\n",
" def __init__(self,host,user,passwd,db):\n",
@ -656,22 +657,48 @@
" \n",
" #保存省份数据\n",
" def save_province_datas(self,all_province):\n",
" \n",
" date1 = datetime.datetime.now().strftime('%Y-%m-%d')\n",
" sql1 = 'delete from province_daily_datas where pub_time like \"%s\"'%(date1 + '%')\n",
" print(sql1)\n",
" \n",
" try:\n",
" self.cursor.execute(sql1)\n",
" self.conn.commit()\n",
" print(\"之前省份删除成功\")\n",
" except Exception as a:\n",
" print(a)\n",
" \n",
" sql = 'insert into province_daily_datas(provinceName,provinceShortName,currentConfirmedCount,confirmedCount,suspectedCount,curedCount,deadCount,pub_time) \\\n",
" values(%s,%s,%s,%s,%s,%s,%s,%s)'\n",
" res = self.get_province_list_tuple(all_province)\n",
" \n",
" print(\"+++++++ save_province_datas, datas len:%d\"%(len(res)))\n",
" \n",
" try:\n",
" try: \n",
" \n",
" self.cursor.executemany(sql,res)\n",
" self.conn.commit()\n",
" except Exception as e:\n",
" print(e)\n",
" print(\"???\")\n",
" print(\"++++++++++++ save_province_datas is over\")\n",
" \n",
" \n",
" #保存城市数据\n",
" def save_city_datas(self,all_city):\n",
" date2 = datetime.datetime.now().strftime('%Y-%m-%d')\n",
" sql2 = 'delete from city_daily_datas where pub_time like \"%s\"'%(date2 + '%')\n",
" try:\n",
" self.cursor.execute(sql2)\n",
" print((date2+\"%\"))\n",
" self.conn.commit()\n",
" print(\"之前城市删除成功\")\n",
" except Exception as a:\n",
" print(a)\n",
" \n",
" \n",
" \n",
" sql = 'insert into city_daily_datas(cityName,currentConfirmedCount,confirmedCount,suspectedCount,curedCount,deadCount,locationId,province,pub_time) \\\n",
" values(%s,%s,%s,%s,%s,%s,%s,%s,%s)'\n",
" res = self.get_city_list_tuple(all_city)\n",
@ -679,10 +706,14 @@
" print(\"+++++++ save_city_daily_datas, datas len:%d\"%(len(res)))\n",
" \n",
" try:\n",
" \n",
" \n",
" \n",
" self.cursor.executemany(sql,res)\n",
" self.conn.commit()\n",
" except Exception as e:\n",
" print(e)\n",
" print(\"???\")\n",
" print(\"++++++++++++ save_city_daily_datas is over\") \n",
" \n",
" def __del__(self):\n",
@ -703,17 +734,20 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"448\n",
"delete from province_daily_datas where pub_time like \"2020-08-14%\"\n",
"之前省份删除成功\n",
"+++++++ save_province_datas, datas len:34\n",
"++++++++++++ save_province_datas is over\n",
"+++++++ save_city_daily_datas, datas len:448\n",
"2020-08-14%\n",
"之前城市删除成功\n",
"+++++++ save_city_daily_datas, datas len:447\n",
"++++++++++++ save_city_daily_datas is over\n"
]
}
@ -837,7 +871,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.8.3"
}
},
"nbformat": 4,

Loading…
Cancel
Save