python31-python操作數(shù)據(jù)庫

本篇文章主要講的是python操作mysql數(shù)據(jù)庫,包括數(shù)據(jù)庫的連接以及數(shù)據(jù)庫的增刪改查,常用的方法包括connect、cursor、execute、commit、fetchone、fetchall、rowcount、close。希望感興趣的小伙伴可以堅持看下去同時歡迎提出寶貴的意見讓我們一起進步!

01:安裝MySQLdb模塊

1)概述:MySQLdb用于python連接Mysql數(shù)據(jù)庫的接口。

2)命令:pip install mysqlclient

02:python連接數(shù)據(jù)庫前提條件

①已經(jīng)創(chuàng)建了數(shù)據(jù)庫TESTDB

②連接數(shù)據(jù)庫TESTDB使用的用戶名和密碼

③保證主機上已經(jīng)安裝MySQLdb模塊

03:連接數(shù)據(jù)庫
import MySQLdb
#打開數(shù)據(jù)庫連接
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
#使用cursor()方法獲得操作游標:返回一個cursor對象,中文叫游標
cursor=db.cursor()
#使用execute()方法執(zhí)行sql語句
cursor.execute('select version()')
#使用fetchone()方法獲取一條數(shù)據(jù)
data=cursor.fetchone()
print('當前數(shù)據(jù)庫的版本:',data)
db.close()#關(guān)閉數(shù)據(jù)庫連接
04:創(chuàng)建數(shù)據(jù)庫表

sql=create table student(id varchar(50), name varchar(50) not null, age int,score int )

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
#如果數(shù)據(jù)表已經(jīng)存在使用execute()方法刪除表
cursor.execute('drop table if exists student')
#構(gòu)造建表sql語句
sql='''create table student(
        id  varchar(50),
        name varchar(50) not null,
        age int,
        score int )'''
print(cursor.execute(sql))
db.close()
05:數(shù)據(jù)庫插入操作

1)所有對數(shù)據(jù)庫修改的動作,必須要commit才會生效

2)加入try except原因:因為在真實場景當中,執(zhí)行的sql語句可能會是多條,而數(shù)據(jù)之間存在外鍵關(guān)系或其他邏輯關(guān)系。一旦某條sql報錯而其他sql不報錯(報錯的sql沒有插入數(shù)據(jù)進去,其他的 sql 沒報錯,成功插入了數(shù)據(jù)),就會造成大量臟數(shù)據(jù),引發(fā)bug,帶來大量的數(shù)據(jù)修復工作,所以我們用try捕獲異常,一旦任意sql報錯即刻回滾操作。

3)sql=insert into student(id,name,age,score)values('123d45e6','mike',25,60)

import MySQLdb,uuid
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
studentId=uuid.uuid4()
#構(gòu)造插入sql語句
sql='''insert into student(id,name,age,score)
        values('%s','mike',25,60) '''%(studentId)
try:
    #執(zhí)行sql語句
    cursor.execute(sql)
    #提交到數(shù)據(jù)庫執(zhí)行
    db.commit()
    print('執(zhí)行成功')
except:
    #插入數(shù)據(jù)有問題時需要回滾代碼
    db.rollback()
print(sql)
db.close()
06:數(shù)據(jù)庫更新操作

sql=update student set name='lisa' where id='98351c37-ff222cb-43ce-b691

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
sql="update student set name='lisa' where id='98351c37-ff222cb-43ce-b691-a80674354285'"
try:
    #執(zhí)行sql語句
    cursor.execute(sql)
    #提交到數(shù)據(jù)庫執(zhí)行
    db.commit()
    print('執(zhí)行成功')
except:
    #更新數(shù)據(jù)有問題時需要回滾代碼
    db.rollback()
    print('回滾成功')
print(sql)
db.close()
07:數(shù)據(jù)庫查詢操作

1)Python查詢 Mysql使用fetchone()方法獲取單條數(shù)據(jù), 使用 fetchall()方法獲取多條數(shù)據(jù)

①etchone():該方法獲取下一個查詢結(jié)果集。結(jié)果集是一個對象

②fetchall():接收全部返回結(jié)果

rowcount:這是一個只讀屬性,并返回執(zhí)行execute方法后影響的行數(shù)

2)sql=select * from student

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
#構(gòu)建sql查詢語句
sql="select * from student"
try:
    cursor.execute(sql)
    #獲取查詢結(jié)果
    # data=cursor.fetchone()#獲取一個
    data=cursor.rowcount#受影響的行數(shù)
    print(data)
    data=cursor.fetchall()#獲取全部
    for row in data:
        print(row)
except:
    print('查詢失敗')
db.close()
08:數(shù)據(jù)庫刪除操作

sql=delete from student where age=30

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
#構(gòu)建sql刪除語句
sql="delete from student where age=30"
try:
    #執(zhí)行sql語句
    cursor.execute(sql)
    #提交到數(shù)據(jù)庫執(zhí)行
    db.commit()
    print('執(zhí)行成功')
except:
    #數(shù)據(jù)刪除錯誤時需要回滾代碼
    db.rollback()
    print('回滾成功')
print(sql)
db.close()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容