|
第一節(jié):數(shù)據(jù)庫(kù)API與全局變量及核心類基本流程 - 數(shù)據(jù)庫(kù)API
- 通過(guò)全局變量查看 DB API特性
- 全局變量用于判斷該數(shù)據(jù)庫(kù)模塊所支持的功能,通常有以下3個(gè)全局變量
- apilevel:顯示數(shù)據(jù)庫(kù)模塊的API版本號(hào)
- threadsafety:指定該數(shù)據(jù)庫(kù)模塊的線程安全等級(jí)
- paramstyle:指定當(dāng)SQL語(yǔ)句需要參數(shù)時(shí),可以使用哪種風(fēng)格(qmark、numeric、named)的參數(shù)
- 核心API
- connect()函數(shù):鏈接數(shù)據(jù)庫(kù),返回?cái)?shù)據(jù)庫(kù)鏈接
- 數(shù)據(jù)庫(kù)鏈接:用于打開(kāi)游標(biāo),開(kāi)啟或提交事務(wù)
- 游標(biāo):用于執(zhí)行SQL語(yǔ)句,獲取執(zhí)行結(jié)果
- 操作數(shù)據(jù)庫(kù)的流程
第二節(jié):案例實(shí)操-動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表 - 導(dǎo)入sqlite3模塊
- python自帶了sqlite數(shù)據(jù)庫(kù)和sqlite數(shù)據(jù)庫(kù)的API模塊,無(wú)需再安裝,如果是導(dǎo)入其他sqlite自身未帶有的模塊,就需要大家手動(dòng)去安裝了
- 導(dǎo)入sqlite3模塊,通過(guò)全局變量可了解該模塊支持的特性
- 執(zhí)行DDL創(chuàng)建數(shù)據(jù)庫(kù)
- 按照前面的步驟操作SQLite數(shù)據(jù)庫(kù),只要用游標(biāo)執(zhí)行DDL語(yǔ)句即可
import sqlite3# 1 打開(kāi)數(shù)據(jù)庫(kù)鏈接# SQLite是一個(gè)沒(méi)有后臺(tái)進(jìn)程的數(shù)據(jù)庫(kù),磁盤上的一個(gè)文件就可以對(duì)應(yīng)SQLite數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 2 打開(kāi)游標(biāo)c = conn.cursor()# 3 使用游標(biāo)的execute方法執(zhí)行任意的SQL語(yǔ)句(DDL)c.execute(''' craete table user_tb( _id integer primary key autoincrement, name text, pass text, age interger)''')c.execute(''' craete table order_tb( _id integer primary key autoincrement, item_name text, item_price real, item_number integer, user_id integer, foreign key(user_id) references user_tb(_id))''')# 4 關(guān)閉游標(biāo)c.close()# 5 關(guān)閉數(shù)據(jù)庫(kù)鏈接conn.colse()
- SQLite數(shù)據(jù)庫(kù)特性
- SQLite 內(nèi)部只支持NULL 、INTEGER 、REAL(浮點(diǎn)型)、TEXT(文本)和BLOB(大二進(jìn)制對(duì)象)這五種數(shù)據(jù)類型
- SQLite允許輸入數(shù)據(jù)時(shí)忽略底層數(shù)據(jù)列實(shí)際的數(shù)據(jù)類型,因此在編寫(xiě)建表語(yǔ)句時(shí)可以省略數(shù)據(jù)列后面的類型聲明
import sqlite3# 1 打開(kāi)數(shù)據(jù)庫(kù)鏈接# SQLite是一個(gè)沒(méi)有后臺(tái)進(jìn)程的數(shù)據(jù)庫(kù),磁盤上的一個(gè)文件就可以對(duì)應(yīng)SQLite數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 2 打開(kāi)游標(biāo)c = conn.cursor()# 3 使用游標(biāo)的execute方法執(zhí)行任意的SQL語(yǔ)句(DDL)# 省略數(shù)據(jù)列后面的類型聲明c.execute(''' craete table user_tb( _id integer primary key autoincrement, name, pass, age)''')# 4 關(guān)閉游標(biāo)c.close()# 5 關(guān)閉數(shù)據(jù)庫(kù)鏈接conn.colse()
第三節(jié):使用SQLite Expert - 下載安裝SQLite EXpert
- 登錄http://www./download.html下載軟件
- 使用SQLite EXpert創(chuàng)建數(shù)據(jù)庫(kù)
- 主界面左上角的第一個(gè)和第二個(gè)按鈕(創(chuàng)建內(nèi)存中的數(shù)據(jù)庫(kù))都可以創(chuàng)建數(shù)據(jù)庫(kù)
- 創(chuàng)建數(shù)據(jù)庫(kù)之后就可以創(chuàng)建數(shù)據(jù)表:選擇工具欄中的SQL->New SQL Tab
- 使用SQLite EXpert打開(kāi)數(shù)據(jù)庫(kù)
- 單擊主界面工具條上第三個(gè)按鈕即可打開(kāi)數(shù)據(jù)庫(kù),打開(kāi)數(shù)據(jù)庫(kù)之后 ,可查看該數(shù)據(jù)庫(kù)包含的數(shù)據(jù)表以及表中包含的數(shù)據(jù)
第四節(jié):執(zhí)行DML語(yǔ)句 - 執(zhí)行DML語(yǔ)句
- 使用游標(biāo)的execute()方法也可以執(zhí)行語(yǔ)句的insert、update、delete語(yǔ)句
- SQLite數(shù)據(jù) API默認(rèn)就是開(kāi)啟事務(wù),因此必須提交事務(wù),否則程序?qū)?shù)據(jù)所做的修改(包括插入數(shù)據(jù)、刪除數(shù)據(jù),整理數(shù)據(jù))不會(huì)生效
import sqlite3# 創(chuàng)建數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 執(zhí)行SQL語(yǔ)句# 插入insert into tabnamec.execute('insert into user_tb values(null, ?, ?, ?)', ('fkjava', '33445', 23))c.execute('insert into user_tb values(null, ?, ?, ?)', ('crazyit', '35555', 25))c.execute('insert into order_tb values(null, ?, ?, ?, ?)', ('鼠標(biāo)', 33, 3, 1))# 更新 update tabnamec.execute('update user_tb set pass=?', ('98765',))# 執(zhí)行完DML語(yǔ)句之后,如果程序獲取被DML語(yǔ)句修改的記錄條數(shù),可通過(guò)游標(biāo)的rowcount來(lái)獲取print('受影響的記錄條數(shù):' , c.rowcount)# 提交事務(wù),使修改生效conn.commit()# 關(guān)閉資源c.close()conn.close()
- 重復(fù)執(zhí)行多次DML語(yǔ)句
- 使用executemany()方法則可以將同一條DML語(yǔ)句重復(fù)執(zhí)行多次
- 該方法的第二個(gè)參數(shù)包含幾個(gè)元組,該DML語(yǔ)句就會(huì)被執(zhí)行幾次
import sqlite3# 創(chuàng)建數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 執(zhí)行SQL語(yǔ)句# 此處每個(gè)元組就代表一行數(shù)據(jù)c.executemany('insert into user_tb values(null, ?, ?, ?)', (('悟空', '4444', 20), ('八戒', '5555', 30), ('沙僧', '6666', 40), ('唐僧', '7777', 50)))# 提交事務(wù),使修改生效conn.commit()# 關(guān)閉資源c.close()conn.close()
第五節(jié):執(zhí)行查詢 - 使用execute執(zhí)行查詢語(yǔ)句
- 此時(shí)改為執(zhí)行select語(yǔ)句,由于select語(yǔ)句執(zhí)行完成后可以得到查詢結(jié)果,因此程序可通過(guò)游標(biāo)的fetchone()、fetchmany(n)、fetchall()來(lái)獲取查詢結(jié)果,也可以直接將游標(biāo)當(dāng)成可迭代對(duì)象來(lái)獲取查詢結(jié)果
- fetchone():返回一個(gè)元組,該元組代表一行數(shù)據(jù)
- fetchmany(n):返回一個(gè)長(zhǎng)度小于等于n的列表,列表每個(gè)元素都是一個(gè)元組(每個(gè)元組代表一行數(shù)據(jù))
- fetchall():盡量避免使用fetchall()來(lái)獲取查詢返回的全部記錄,原因是可能導(dǎo)致內(nèi)存開(kāi)銷過(guò)大,嚴(yán)重時(shí)可能導(dǎo)致系統(tǒng)崩潰
import sqlite3# 創(chuàng)建數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 執(zhí)行SQL語(yǔ)句c.execute('select * from user_td where _id > ?', (2,))# 所有查詢結(jié)果都通過(guò)游標(biāo)來(lái)獲取# description屬性(元組)返回列信息# 如果要獲取查詢數(shù)據(jù),fetchxxx或者直接迭代游標(biāo)for col in c.description: print(col[0], end='\t')print() #--------------------fetchone方法------------------ while True: # 用fetchone每次獲取一條記錄 row = c.fetchone() # 如果row為空,說(shuō)明沒(méi)有數(shù)據(jù) if not row: break else: # 輸出該行內(nèi)各個(gè)單元格的數(shù)據(jù) for d in row: print(col[0], end='\t') print()#--------------------游標(biāo)當(dāng)成可迭代對(duì)象------------------ for row in c: # 輸出該行內(nèi)各個(gè)單元格的數(shù)據(jù) for d in row: print(col[0], end='\t') print()# 關(guān)閉資源c.close()conn.close()
第六節(jié):案例實(shí)操-使用事務(wù)控制數(shù)據(jù)庫(kù)操作 - 事務(wù):事務(wù)由一步或者幾步數(shù)據(jù)庫(kù)操作序列組成的邏輯執(zhí)行單元
- 事務(wù)具備的4個(gè)特性:原子性(Atomicity)、一致性(Consistency)、隔離性(Isolation)、持續(xù)性(Durability),簡(jiǎn)稱ACID
- 事務(wù)回滾兩種方式:顯示回滾和自動(dòng)回滾
- 顯示回滾:調(diào)用數(shù)據(jù)庫(kù)連接對(duì)象的rollback
- 自動(dòng)回滾:系統(tǒng)錯(cuò)誤或者強(qiáng)行退出(退出之前沒(méi)有提交)
import sqlite3# 創(chuàng)建數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 如果游標(biāo)只是執(zhí)行DDL語(yǔ)句,程序不需要顯示提交事務(wù),程序所做的修改會(huì)自動(dòng)生效# 如果程序先執(zhí)行DML語(yǔ)句# 執(zhí)行DML語(yǔ)句,事務(wù)開(kāi)啟了,該游標(biāo)后面所執(zhí)行ddl語(yǔ)句也不會(huì)自動(dòng)生效c.execute('insert into user_tb values(null, ?, ?, ?)', ('aaa', 'bbb', 23))# 因此這條DDL語(yǔ)句也不會(huì)自動(dòng)生效c.execute('create table haha(_id integer primary key)')# 提交事務(wù),上面的語(yǔ)句才能生效# conn.commit()# 顯示回滾:回滾事務(wù),如果程序不提交事務(wù),默認(rèn)就會(huì)回滾,上面的語(yǔ)句不會(huì)生效# 自動(dòng)回滾:沒(méi)有提交事務(wù)conn.rollback()# 關(guān)閉資源c.close()conn.close()
第七節(jié):案例實(shí)操-用程序執(zhí)行SQL腳本 insert into user_tb values(null, '張三', '11111', 23)insert into user_tb values(null, '李四', '22222', 24)insert into user_tb values(null, '小吳', '33333', 25)creat table test_td(_id integer primary key autoincrement,name text,pass text,description);creat table emp_td(_id integer primary key autoincrement,emp_name,emp_pass,emp_title);
- 執(zhí)行SQL腳本
- 游標(biāo)對(duì)象還包含一個(gè)executescript()方法,可執(zhí)行一段SQL腳本,它并不是一個(gè)標(biāo)準(zhǔn)的API,但是大部分的數(shù)據(jù)庫(kù)API模塊中都有這個(gè)方法
import sqlite3# 創(chuàng)建數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 打卡SQL腳本所在的文件with open('a.sql', 'r', True, 'UTF-8') as f: # 讀取文件中的SQL語(yǔ)句 sql = f.read() # 使用游標(biāo)來(lái)執(zhí)行SQL腳本,用executescript方法 # SQL腳本中的所有語(yǔ)句都會(huì)被執(zhí)行 c.executescript(sql) # 提交事務(wù)conn.commit() # 關(guān)閉資源c.close()conn.close()
- 便捷方法:
- execute(sql[, parameters]):執(zhí)行一條SQL語(yǔ)句
- executemany(sql[, parameters]):根據(jù)序列重復(fù)執(zhí)行SQL語(yǔ)句
- executescript(sql_script):執(zhí)行SQL腳本
import sqlite3# 創(chuàng)建數(shù)據(jù)庫(kù)conn = sqlite3.connect('test.db')# 打卡SQL腳本所在的文件with open('a.sql', 'r', True, 'UTF-8') as f: # 讀取文件中的SQL語(yǔ)句 sql = f.read() # 直接用數(shù)據(jù)庫(kù)連接對(duì)象來(lái)執(zhí)行SQL腳本 c.executescript(sql) # 提交事務(wù)conn.commit() # 關(guān)閉資源conn.close()
|