日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

python操作MySQL(增刪改查)

 靜幻堂 2018-09-05
小菜鳥學(xué)前端 2018-07-11 09:30:24

大家早上好啊,今天我們繼續(xù)學(xué)習(xí)python操作MySQL(增刪改查)。

python操作MySQL(增刪改查)

大數(shù)據(jù)機(jī)器學(xué)習(xí)數(shù)據(jù)分析爬蟲Python開發(fā)愛好者,?MySQL 是我們經(jīng)常接觸的數(shù)據(jù)庫,它的優(yōu)點好處我就不說了,(其實本人也說不出什么,反正是常用的數(shù)據(jù)庫)那么我們?nèi)绾问褂胮ython 對mysql數(shù)據(jù)庫進(jìn)行操作呢。下面我來給大家說說。

首先連接mysql

在建立連接前我們的python 要安裝pymsql

pip install pymysql

在命令行里輸入 pip install pymysql

python操作MySQL(增刪改查)

下面打開我們的IDE 我使用的是pycharm

# !/usr/bin/env python

# -*- coding:utf-8 -*-

'''

1.0 python連接mysql 并進(jìn)行增刪改查

'''

import pymysql.cursors

'''

連接數(shù)據(jù)庫

'''

#使用pymysql指令連接數(shù)據(jù)庫

connection = pymysql.connect(host = '127.0.0.1', #要連接的數(shù)據(jù)庫的IP地址

user = 'root', #登錄的賬戶名,如果登錄的是最高權(quán)限賬戶則為root

password = '123456', #對應(yīng)的密碼

db = 'iris', #要連接的數(shù)據(jù)庫

charset = 'utf8mb4', #設(shè)置編碼格式

#返回到Python的結(jié)果,以什么方式存儲,如Dict.Cursor是以字典的方式存儲

#如果不加這行數(shù)據(jù)是以元組方式返回

cursorclass = pymysql.cursors.DictCursor

)

這樣我們就建立了python 同mysql 的連接

創(chuàng)建表

try:

'''

創(chuàng)建表

'''

# 使用cursor()方法獲取操作游標(biāo)

with connection.cursor() as cursor:

# 如果數(shù)據(jù)表已經(jīng)存在使用 execute() 方法刪除表。

cursor.execute("drop table if EXISTS users")

sql = '''create table users (

id int(11) not null auto_increment,

email varchar(255) collate utf8_bin not null,

password varchar(255) collate utf8_bin not null,

primary key (id)

)

engine = InnoDB default charset=utf8 collate=utf8_bin

auto_increment=1;

'''

cursor.execute(sql)

except:

# 發(fā)生錯誤時回滾

connection.rollback()

finally:

# 關(guān)閉連接

connection.close()

插入數(shù)據(jù)

# 從數(shù)據(jù)庫鏈接中得到cursor的數(shù)據(jù)結(jié)構(gòu)

with connection.cursor() as cursor:

sql = " insert into users(email, password) VALUES (%s, %s)"

cursor.execute(sql,('webmaster@python.org','very_secret'))

# 執(zhí)行到這一行指令時才是真正改變了數(shù)據(jù)庫,之前只是緩存在內(nèi)存中

connection.commit()

批量插入數(shù)據(jù)

'''

批量插入數(shù)據(jù)

'''

#讀取文件

with open('iris.csv', 'r', encoding='utf-8', newline='') as f:

i = 0 #用來記錄讀取文件中數(shù)據(jù)的的次數(shù)

z = 0 #用來記錄讀取文件中數(shù)據(jù)的的次數(shù)

list = []

for line in f:

strs = line.split(',')

data = (strs[0], strs[1], strs[2], strs[3], strs[4].replace(" ","")) #數(shù)據(jù)的最后一行會有一個 換行符,用replace替換掉

list.append(data)

i += 1

z += 1

if i >= 10: #用于現(xiàn)在一次插入多少條這里是10條插入一次

with connection.cursor() as cursor:

sql = "insert into iris(sepal_length, sepal_width, petal_length, petal_width, uspecies) "

"VALUES (%s, %s, %s, %s, %s)"

cursor.executemany(sql, list) #批量添加數(shù)據(jù)時 要用executemany

connection.commit()

print("插入{}條數(shù)據(jù),已插入{}條數(shù)據(jù)".format(i, z))

i = 0 #對計數(shù)歸零

list.clear() #清理數(shù)據(jù)列表

if i > 0 : # 循環(huán)文件完畢后,對剩余的數(shù)據(jù)進(jìn)行插入,(例如小于10的剩余數(shù)據(jù),比如你的文件有55行,剩余的5行就要這樣插入)

cursor.executemany(sql, list)

connection.commit()

查詢數(shù)據(jù)

'''

查詢數(shù)據(jù)

'''

#單一數(shù)據(jù)

with connection.cursor() as cursor:

sql = " select id, email, password from users where email = %s"

cursor.execute(sql,('sfs1100@126.com'))

result = cursor.fetchone() #fetchone()只取出第一條結(jié)果

print(result)

print(type(result))

#多條數(shù)據(jù)

with connection.cursor() as cursor:

sql = " select * from iris where sepal_length > %s"

cursor.execute(sql, (3))

result = cursor.fetchall() #fetchone()取出查詢后的所有數(shù)據(jù)

print(result)

print(type(result))

for each in result:

print(each['uspecies'])

更新數(shù)據(jù)

'''

更新數(shù)據(jù)

'''

with connection.cursor() as cursor:

sql = " update users set email = %s where password = %s"

cursor.execute(sql, ('sfs1100@126.com', 'very_secret'))

connection.commit()

刪除數(shù)據(jù)

'''

刪除數(shù)據(jù)

'''

with connection.cursor() as cursor:

sql = "delete from users where email = %s"

cursor.execute(sql, ('sfs1100@126.com'))

connection.commit()

python操作MySQL(增刪改查)

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多