日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

pymysql语法_pymysql的用法

發布時間:2023/12/19 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pymysql语法_pymysql的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、首先要安裝MySQL,我安裝的mysq5.7的;

具體安裝步驟可以自行百度,或者參考這個win10安裝MYSQL5.7

二、啟動MySQL,

啟動:net start MySQL

停止:net stop MySQL

卸載:net delete MySQL

其他數據庫操作自行百度;

三、安裝pymysql模塊:

pip直接安裝即可;

四、基本的增刪改查的操作:

#coding:utf-8

# import MySQLdb python3不支持MySQLdb

import pymysql

#打開數據庫,數據庫可自己創建,create database test;

con = pymysql.connect(host='localhost', user='root', passwd='登錄密碼', db='test', port=3306, charset='utf8')

#通過cursor()創建一個游標對象

cur = con.cursor()

#建表

cur.execute(' CREATE TABLE student (id int not null auto_increment primary key,name varchar(20),age int, sex varchar(2))')

#插入數據

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,'女'),('開心麻花',30,'男')])

#提交操作

con.commit()

#查詢表中的數據

cur.execute('SELECT * FROM person')

#fetchall()獲取所有數據,返回一個二維的列表

res = cur.fetchall()

for line in res:

print(line)

# fetchone()獲取其中的一個結果,返回一個元組

cur.execute('SELECT * FROM person')

res = cur.fetchone()

print(res)

#修改數據

cur.execute('UPDATE person SET name=%s WHERE id=%s', ('小明',12,'男'))

#刪除數據

cur.execute('DELETE FROM person WHERE id=%s', (0,))

con.commit()

con.close()

五、用pymysql跑了一下之前寫的天氣的爬取:

import requests

import json

import pymysql

#打開數據庫

con = pymysql.connect(host='localhost', user='root', passwd='wjx411527', db='test', port=3306, charset='utf8')

#通過cursor方法創建一個游標對象

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'

}

# 新建列表,用來存儲獲取的數據

all_info = []

def __init__(self):

pass

# 獲取一年的天氣數據

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()

# 獲取天氣的具體數據

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']

#先把去重的數據存儲到列表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)

#把數據存到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()

# 關閉數據庫連接

con.close()

運行結果:

天氣

寫法和sqlite的基本類似,可以和上一篇sqlite的比較一下;

幾個小點注意一下:

1、mysql的數據庫要提前創建好;

2、mysql在創建表時必須要表明每列的數據類型;

mysql這里用的可視化工具是navicat,可以自行百度下載;

總結

以上是生活随笔為你收集整理的pymysql语法_pymysql的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。