最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyMySQL實(shí)現(xiàn)增刪查改的簡單使用

 更新時(shí)間:2021年05月13日 10:46:08   作者:Ma Sizhou  
這篇文章主要介紹了PyMySQL實(shí)現(xiàn)增刪查改的簡單使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

我們在使用MySQL的時(shí)候,可以在MySQL的客戶終端來操作數(shù)據(jù)庫中的表,同時(shí),也可以使用navicat等可視化的工具來操作數(shù)據(jù)表。但是,這只是操作個(gè)別數(shù)據(jù),如果我們想要插入10萬條數(shù)據(jù),那肯定就不能這么做了。
我們可以通過程序?qū)懸粋€(gè)循環(huán)來自動(dòng)插入,因此,PyMySQL就是使用python語言來直接操作數(shù)據(jù)庫的一個(gè)接口。
明確了這一點(diǎn),我們再開始介紹PyMySQL包:

1、PyMySQL的使用步驟:

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

2、案例:

2.1 查詢數(shù)據(jù)庫中的表的信息:

 # 需求:查詢數(shù)據(jù)庫person中info表的信息

 # 1.導(dǎo)包
import pymysql

try:
     # 2.連接MySQL數(shù)據(jù)庫的服務(wù)
    connc = pymysql.Connect(
                    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
                    password="4412",
                    host='127.0.0.1',  # mysql服務(wù)端的IP,默認(rèn)是127.0.0.1/localhost,或者寫真實(shí)的ip
                    database='person',
                    port=3306,
                    charset="utf8")
     # 3.創(chuàng)建游標(biāo)對象
    cur = connc.cursor()
     # 4.編寫SQL語句
    sql = 'select * from info;'
     # 5.使用游標(biāo)對象調(diào)用SQL
    cur.execute(sql)
     # 6.獲取查詢的結(jié)果
    result= cur.fetchall()
    print(result)
    # 7.關(guān)閉游標(biāo)對象
    cur.close()
    # 8.關(guān)閉連接
    connc.close()

except Exception as e:
    print(e)

運(yùn)行結(jié)果:

在這里插入圖片描述

2.2 增加數(shù)據(jù):

大部分的步驟都和前面一樣,直接在程序中注釋看:

# 需求:
# 增加數(shù)據(jù) 劉德華56 男 數(shù)據(jù) 到 數(shù)據(jù)庫person--的info表中
# 修改數(shù)據(jù) 小王 的名字為 小王吧 到 數(shù)據(jù)庫person--的info表中
# 刪除數(shù)據(jù) 張三      數(shù)據(jù)庫person--的info表中

# 1.導(dǎo)包
import pymysql

# 2.連接MySQL服務(wù)
connc = pymysql.Connect(
    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
    password="4412",
    host='127.0.0.1',  # mysql服務(wù)端的IP,默認(rèn)是127.0.0.1/localhost,或者寫真實(shí)的ip
    database='person',
    port=3306,
    charset="utf8")

# 3.創(chuàng)建游標(biāo)對象
cur = connc.cursor()

try:
    # 4.編寫、增加、刪除的SQL語句    
    # 增加數(shù)據(jù) 劉德華 56 男
    sql = 'insert into info values(%s, %s, %s, %s)'
    add_data = [0,"劉德華", 56, "男"]
    
    # 5.使用游標(biāo)對象執(zhí)行SQL語句
    cur.execute(sql, add_data)
    
    # 6.提交操作
    connc.commit()
    
except Exception as e:
    print(e)
    # 操作失敗,數(shù)據(jù)回滾
    connc.rollback()
    
finally:
    # 7.關(guān)閉游標(biāo)對象
    cur.close()
    
    # 8.關(guān)閉連接
    connc.close()

print("結(jié)束!")

運(yùn)行之后,看看person數(shù)據(jù)庫中 表info 的數(shù)據(jù),確實(shí)增加成功了:

在這里插入圖片描述

2.3 修改數(shù)據(jù):

# 需求:
# 增加數(shù)據(jù) 劉德華56 男 數(shù)據(jù) 到 數(shù)據(jù)庫person--的info表中
# 修改數(shù)據(jù) 小王 的名字為 小王吧 到 數(shù)據(jù)庫person--的info表中
# 刪除數(shù)據(jù) 張三      數(shù)據(jù)庫person--的info表中

# 1.導(dǎo)包
import pymysql

# 2.連接MySQL服務(wù)
connc = pymysql.Connect(
    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
    password="4412",
    host='127.0.0.1',  # mysql服務(wù)端的IP,默認(rèn)是127.0.0.1/localhost,或者寫真實(shí)的ip
    database='person',
    port=3306,
    charset="utf8")

# 3.創(chuàng)建游標(biāo)對象
cur = connc.cursor()

try:
    # 4.編寫、增加、刪除的SQL語句
    # 修改數(shù)據(jù) 李四 的名字為 李四的爸爸
    sql = 'update info set name=%s where name="李四"'
    update_data = ["李四的爸爸"]
        
    # 5.使用游標(biāo)對象執(zhí)行SQL語句
    cur.execute(sql, update_data)
    
    # 6.提交操作
    connc.commit()
    
except Exception as e:
    print(e)
    #  操作失敗,數(shù)據(jù)回滾
    connc.rollback()
    
finally:
    # 7.關(guān)閉游標(biāo)對象
    cur.close()
    
    # 8.關(guān)閉連接
    connc.close()

print("結(jié)束!")

運(yùn)行之后,看看person數(shù)據(jù)庫中 表info 的數(shù)據(jù),確實(shí)修改成功了:

在這里插入圖片描述

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

# 需求:
# 增加數(shù)據(jù) 劉德華56 男 數(shù)據(jù) 到 數(shù)據(jù)庫person--的info表中
# 修改數(shù)據(jù) 小王 的名字為 小王吧 到 數(shù)據(jù)庫person--的info表中
# 刪除數(shù)據(jù) 張三      數(shù)據(jù)庫person--的info表中

# 1.導(dǎo)包
import pymysql

# 2.連接MySQL服務(wù)
connc = pymysql.Connect(
    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
    password="4412",
    host='127.0.0.1',  # mysql服務(wù)端的IP,默認(rèn)是127.0.0.1/localhost,或者寫真實(shí)的ip
    database='person',
    port=3306,
    charset="utf8")

# 3.創(chuàng)建游標(biāo)對象
cur = connc.cursor()

try:
    # 4.編寫、增加、刪除的SQL語句
    # 修改數(shù)據(jù) 李四 的名字為 李四的爸爸
    sql = 'update info set name=%s where name="李四"'
    update_data = ["李四的爸爸"]
        
    # 5.使用游標(biāo)對象執(zhí)行SQL語句
    cur.execute(sql, update_data)
    
    # 6.提交操作
    connc.commit()
    
except Exception as e:
    print(e)
    #  操作失敗,數(shù)據(jù)回滾
    connc.rollback()
    
finally:
    # 7.關(guān)閉游標(biāo)對象
    cur.close()
    
    # 8.關(guān)閉連接
    connc.close()

print("結(jié)束!")

運(yùn)行之后,看看person數(shù)據(jù)庫中 表info 的數(shù)據(jù),確實(shí)刪除成功了:

在這里插入圖片描述

到此這篇關(guān)于PyMySQL實(shí)現(xiàn)增刪查改的簡單使用的文章就介紹到這了,更多相關(guān)PyMySQL 增刪查改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

清水河县| 昭苏县| 吉安市| 连山| 浏阳市| 桓台县| 马山县| 彰化县| 东源县| 库伦旗| 宜兴市| 定西市| 平阴县| 衢州市| 香港 | 清镇市| 漯河市| 义乌市| 虹口区| 南川市| 六枝特区| 孟州市| 外汇| 彭阳县| 庆云县| 元阳县| 遂平县| 陵水| 陆川县| 永清县| 理塘县| 宜兰市| 和平区| 贵溪市| 什邡市| 泰顺县| 怀集县| 内黄县| 红桥区| 民县| 哈密市|