App下載

PyMySQL增刪查改的簡單使用如何操作

少女一米八 2021-08-13 10:15:00 瀏覽數(shù) (2263)
反饋

我們在使用MySQL的時候,可以在MySQL的客戶終端來操作數(shù)據(jù)庫中的表,同時,也可以使用navicat等可視化的工具來操作數(shù)據(jù)表。

但是,這只是操作個別數(shù)據(jù),如果我們想要插入10萬條數(shù)據(jù),那肯定就不能這么做了。我們可以通過程序?qū)懸粋€循環(huán)來自動插入,因此,PyMySQL就是使用python語言來直接操作數(shù)據(jù)庫的一個接口。今天我們就來介紹一下pymysql使用步驟,簡單了解一下pymysql增刪改查的功能吧。
明確了這一點,我們再開始介紹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,或者寫真實的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)

運行結(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,或者寫真實的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é)束!")

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

增加結(jié)果

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,或者寫真實的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é)束!")

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

修改結(jié)果

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,或者寫真實的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é)束!")

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

刪除結(jié)果

到此這篇PyMySQL增刪查改的簡單使用介紹就介紹到這了,更多PyMySQL學(xué)習(xí)內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章。

0 人點贊