第一節(jié):數(shù)據(jù)庫API與全局變量及核心類基本流程
第二節(jié):案例實(shí)操-動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表
import sqlite3# 1 打開數(shù)據(jù)庫鏈接# SQLite是一個(gè)沒有后臺(tái)進(jìn)程的數(shù)據(jù)庫,磁盤上的一個(gè)文件就可以對(duì)應(yīng)SQLite數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 2 打開游標(biāo)c = conn.cursor()# 3 使用游標(biāo)的execute方法執(zhí)行任意的SQL語句(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ù)庫鏈接conn.colse()
import sqlite3# 1 打開數(shù)據(jù)庫鏈接# SQLite是一個(gè)沒有后臺(tái)進(jìn)程的數(shù)據(jù)庫,磁盤上的一個(gè)文件就可以對(duì)應(yīng)SQLite數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 2 打開游標(biāo)c = conn.cursor()# 3 使用游標(biāo)的execute方法執(zhí)行任意的SQL語句(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ù)庫鏈接conn.colse()
第三節(jié):使用SQLite Expert
第四節(jié):執(zhí)行DML語句
import sqlite3# 創(chuàng)建數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 執(zhí)行SQL語句# 插入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語句之后,如果程序獲取被DML語句修改的記錄條數(shù),可通過游標(biāo)的rowcount來獲取print('受影響的記錄條數(shù):' , c.rowcount)# 提交事務(wù),使修改生效conn.commit()# 關(guān)閉資源c.close()conn.close()
import sqlite3# 創(chuàng)建數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 執(zhí)行SQL語句# 此處每個(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í)行查詢
import sqlite3# 創(chuàng)建數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 執(zhí)行SQL語句c.execute('select * from user_td where _id > ?', (2,))# 所有查詢結(jié)果都通過游標(biāo)來獲取# 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為空,說明沒有數(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ù)庫操作
import sqlite3# 創(chuàng)建數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 如果游標(biāo)只是執(zhí)行DDL語句,程序不需要顯示提交事務(wù),程序所做的修改會(huì)自動(dòng)生效# 如果程序先執(zhí)行DML語句# 執(zhí)行DML語句,事務(wù)開啟了,該游標(biāo)后面所執(zhí)行ddl語句也不會(huì)自動(dòng)生效c.execute('insert into user_tb values(null, ?, ?, ?)', ('aaa', 'bbb', 23))# 因此這條DDL語句也不會(huì)自動(dòng)生效c.execute('create table haha(_id integer primary key)')# 提交事務(wù),上面的語句才能生效# conn.commit()# 顯示回滾:回滾事務(wù),如果程序不提交事務(wù),默認(rèn)就會(huì)回滾,上面的語句不會(huì)生效# 自動(dòng)回滾:沒有提交事務(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);
import sqlite3# 創(chuàng)建數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 獲取游標(biāo)c = conn.cursor()# 打卡SQL腳本所在的文件with open('a.sql', 'r', True, 'UTF-8') as f: # 讀取文件中的SQL語句 sql = f.read() # 使用游標(biāo)來執(zhí)行SQL腳本,用executescript方法 # SQL腳本中的所有語句都會(huì)被執(zhí)行 c.executescript(sql) # 提交事務(wù)conn.commit() # 關(guān)閉資源c.close()conn.close()
import sqlite3# 創(chuàng)建數(shù)據(jù)庫conn = sqlite3.connect('test.db')# 打卡SQL腳本所在的文件with open('a.sql', 'r', True, 'UTF-8') as f: # 讀取文件中的SQL語句 sql = f.read() # 直接用數(shù)據(jù)庫連接對(duì)象來執(zhí)行SQL腳本 c.executescript(sql) # 提交事務(wù)conn.commit() # 關(guān)閉資源conn.close()
聯(lián)系客服