1. 程式人生 > 實用技巧 >python操作MySQL---PyMySql模組

python操作MySQL---PyMySql模組

第一part:pymysql模組的安裝:


PyMySQL 是在 Python3.x 版本中用於連線 MySQL 伺服器的一個第三方庫,Python2中則使用mysqldb,其安裝方式有兩種:

方式一:在命令列L:pip installpymysql

方式二:在pycharm --settings--project---project Interpreter中搜索PyMySql進行新增


第二part:pymysql對資料庫的連線--新增資料



#1:匯入pymysql模組

import pymysql
#2:連線mysql資料庫,使用connect函式,其中包含資料庫伺服器地址、埠號,使用者名稱、密碼、資料庫名

get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:建立遊標,用於獲取結果集
get_cursor=get_connect.cursor()
#4:定義一條插入的sql語句
insert_sql="insert into student values(7,'huangsasa','廣州白雲')"

#5:使用遊標進行執行sql
get_cursor.execute(insert_sql)
#6:進行事務提交(只有新增,修改和刪除需要)
get_connect.commit()
#7:關閉遊標連線
get_cursor.close()
#8:關閉資料庫連線
get_connect.close()

執行之後,在表中新增一條資料:



第三part:pymysql對資料庫的連線--修改資料


#1:匯入pymysql模組

import pymysql
#2,連線mysql資料庫,使用connect函式,其中包含資料庫伺服器地址、埠號,使用者名稱、密碼、資料庫名
get_connect=pymysql.connect(host="localhost",

port=3306,
user="root",
passwd="123456",
db="school")
#3:建立遊標,用於獲取結果集
get_cursor=get_connect.cursor()
#4:定義一條更新的sql語句
update_sql="update student set student_address='深圳市羅湖區' where student_id=7"
#5:使用遊標進行執行sql
get_cursor.execute(update_sql)
#6:進行事務提交
get_connect.commit()
#7:關閉遊標連線
get_cursor.close()
#8:關閉資料庫連線
get_connect.close()

執行之後,在表中對該條資料進行修改,如下:


第四part:查詢資料


#1:匯入pymysql模組

import pymysql
#2,連線mysql資料庫,使用connect函式,其中包含資料庫伺服器地址、埠號,使用者名稱、密碼、資料庫名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:建立遊標,用於獲取結果集
get_cursor=get_connect.cursor()
#4:定義一條查詢的sql語句
select_sql="select * from student where student_id=7"
#5:使用遊標進行執行sql
get_cursor.execute(select_sql)
#6:獲取執行的結果,fetchall為查詢所有的記錄
results=get_cursor.fetchall()
#7:列印查詢結果,結果的型別為元組,可以通過迴圈進行遍歷列印
print(results)
#8:關閉遊標
get_cursor.close()
#9:關閉資料庫連線
get_connect.close()

執行之後,結果如下:


第五part:刪除資料


#1:匯入pymysql模組

import pymysql
#2,連線mysql資料庫,使用connect函式,其中包含資料庫伺服器地址、埠號,使用者名稱、密碼、資料庫名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:建立遊標,用於獲取結果集
get_cursor=get_connect.cursor()
#4:定義一條刪除的sql語句
delete_sql="delete from student where student_id=7"
#5:使用遊標進行執行sql
get_cursor.execute(delete_sql)
#6:進行事務提交
get_connect.commit()
#7:關閉遊標連線
get_cursor.close()
#8:關閉資料庫連線
get_connect.close()

執行之後,結果如下:


第六part:對新增,修改和刪除失敗時進行回滾資料


#1:匯入pymysql模組

import pymysql
#2,連線mysql資料庫,使用connect函式,其中包含資料庫伺服器地址、埠號,使用者名稱、密碼、資料庫名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:建立遊標,用於獲取結果集
get_cursor=get_connect.cursor()
#4:定義一條sql語句
str_sql="insert into student values (7,'huigun','廣州海珠區')"
error_sql="insert into student values ('huigun','失敗時進行回滾操作')"
# 5:使用遊標進行執行sql
try:
#執行一個正確的sql語句
get_cursor.execute(str_sql)
#執行一個錯誤的sql語句
get_cursor.execute(error_sql)
#提交事務
get_connect.commit()
except Exception as e:
#出現異常,事務進行回滾操作
get_connect.rollback()
print("丟擲異常,事務進行回滾操作")
#6:進行事務提交
get_connect.commit()
#7:關閉遊標連線
get_cursor.close()
#8:關閉資料庫連線
get_connect.close()

執行之後,結果如下: