pymysql语法_pymysql的用法
一、首先要安裝MySQL,我安裝的mysq5.7的;
具體安裝步驟可以自行百度,或者參考這個(gè)win10安裝MYSQL5.7
二、啟動(dòng)MySQL,
啟動(dòng):net start MySQL
停止:net stop MySQL
卸載:net delete MySQL
其他數(shù)據(jù)庫(kù)操作自行百度;
三、安裝pymysql模塊:
pip直接安裝即可;
四、基本的增刪改查的操作:
#coding:utf-8
# import MySQLdb python3不支持MySQLdb
import pymysql
#打開(kāi)數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)可自己創(chuàng)建,create database test;
con = pymysql.connect(host='localhost', user='root', passwd='登錄密碼', db='test', port=3306, charset='utf8')
#通過(guò)cursor()創(chuàng)建一個(gè)游標(biāo)對(duì)象
cur = con.cursor()
#建表
cur.execute(' CREATE TABLE student (id int not null auto_increment primary key,name varchar(20),age int, sex varchar(2))')
#插入數(shù)據(jù)
data="'Alice',16,女"
cur.execute(' INSERT INTO person (name,age) VALUES (%s)'%data)
cur.execute(' INSERT INTO person (name,age) VALUES (%s,%s)',('Alex',20,'男'))
cur.executemany(' INSERT INTO person (name,age) VALUES (%s,%s)',[('sara',24,'女'),('開(kāi)心麻花',30,'男')])
#提交操作
con.commit()
#查詢表中的數(shù)據(jù)
cur.execute('SELECT * FROM person')
#fetchall()獲取所有數(shù)據(jù),返回一個(gè)二維的列表
res = cur.fetchall()
for line in res:
print(line)
# fetchone()獲取其中的一個(gè)結(jié)果,返回一個(gè)元組
cur.execute('SELECT * FROM person')
res = cur.fetchone()
print(res)
#修改數(shù)據(jù)
cur.execute('UPDATE person SET name=%s WHERE id=%s', ('小明',12,'男'))
#刪除數(shù)據(jù)
cur.execute('DELETE FROM person WHERE id=%s', (0,))
con.commit()
con.close()
五、用pymysql跑了一下之前寫的天氣的爬取:
import requests
import json
import pymysql
#打開(kāi)數(shù)據(jù)庫(kù)
con = pymysql.connect(host='localhost', user='root', passwd='wjx411527', db='test', port=3306, charset='utf8')
#通過(guò)cursor方法創(chuàng)建一個(gè)游標(biāo)對(duì)象
cur = con.cursor()
#建表
cur.execute(' CREATE TABLE tianqi (id int not null auto_increment primary key,date int ,date2 varchar(20),history_rain varchar(20),history_max int,history_min int,tmax varchar(10),tmin varchar(10),time varchar(20),w1 varchar(20),wd1 varchar(20),alins varchar(20),als varchar(20))')
class Weather():
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer': 'http://www.weather.com.cn/weather40d/101280601.shtml'
}
# 新建列表,用來(lái)存儲(chǔ)獲取的數(shù)據(jù)
all_info = []
def __init__(self):
pass
# 獲取一年的天氣數(shù)據(jù)
def get_data(self):
global year
month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
for i in month:
url = 'http://d1.weather.com.cn/calendar_new/' + str(year) + '/101280601_' + str(year) + str(
i) + '.html?_=1496558858156'
html = requests.get(url, headers=self.headers).content
global datas
datas = json.loads(html[11:])
self.get_info()
# 獲取天氣的具體數(shù)據(jù)
def get_info(self):
for data in datas:
if data['date'][:4] == year:
date = data['date']
date2 = data['nlyf'] + data['nl']
history_rain = data['hgl']
history_max = data['hmax']
history_min = data['hmin']
tmax = data['max']
tmin = data['min']
time = data['time']
w1 = data['w1']
wd1 = data['wd1']
alins = data['alins']
als = data['als']
#先把去重的數(shù)據(jù)存儲(chǔ)到列表all_info
info = (date,date2,history_rain,history_max,history_min,tmax,tmin,time,w1,wd1,alins,als )
if info not in self.all_info:
self.all_info.append(info)
#把數(shù)據(jù)存到sqlite
def store_pymysql(self):
self.get_data()
for one_info in self.all_info:
cur.execute(' INSERT INTO tianqi (date,date2,history_rain,history_max,history_min,tmax,tmin,time,w1,wd1,alins,als) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',one_info)
con.commit()
if __name__ == '__main__':
year = '2017'
tianqi = Weather()
tianqi.store_pymysql()
# 關(guān)閉數(shù)據(jù)庫(kù)連接
con.close()
運(yùn)行結(jié)果:
天氣
寫法和sqlite的基本類似,可以和上一篇sqlite的比較一下;
幾個(gè)小點(diǎn)注意一下:
1、mysql的數(shù)據(jù)庫(kù)要提前創(chuàng)建好;
2、mysql在創(chuàng)建表時(shí)必須要表明每列的數(shù)據(jù)類型;
mysql這里用的可視化工具是navicat,可以自行百度下載;
總結(jié)
以上是生活随笔為你收集整理的pymysql语法_pymysql的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: fan4801开关电源原理图_开关电源原
- 下一篇: innodb和my查询速度_mysql存