欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中SQLite數(shù)據(jù)庫(kù)的使用

 更新時(shí)間:2023年04月28日 08:42:58   作者:HkSwaggyD  
SQLite是一種輕型關(guān)系型數(shù)據(jù)庫(kù),常用于嵌入式設(shè)備和移動(dòng)應(yīng)用中。Python中內(nèi)置了SQLite模塊,可用于連接和操作SQLite數(shù)據(jù)庫(kù)。通過Python SQLite模塊,可以方便地創(chuàng)建、查詢和修改數(shù)據(jù)庫(kù)中的數(shù)據(jù),支持事務(wù)處理和數(shù)據(jù)庫(kù)操作的原子性保證

SQL(結(jié)構(gòu)化查詢語(yǔ)言)是一種通用數(shù)據(jù)庫(kù)查詢語(yǔ)言。SQL具有數(shù)據(jù)定義、數(shù)據(jù)操作和數(shù)據(jù)控制功能,可以完成數(shù)據(jù)庫(kù)的全部工作。SQL語(yǔ)言使用時(shí)只需要用告訴計(jì)算機(jī)“做什么”,而不需要告訴它“怎么做”。

SQL語(yǔ)言有兩種使用方式,一是直接以命令方式交互使用;二是嵌入到C/C++、Python等主語(yǔ)言中使用。

本章我們通過學(xué)習(xí)Python中的第三方庫(kù)SQLite來掌握一些SQL的基本語(yǔ)句,以及運(yùn)用Python來操作數(shù)據(jù)庫(kù)的方法

預(yù)備知識(shí)

sqlite數(shù)據(jù)庫(kù)的創(chuàng)建與連接

sqlite數(shù)據(jù)庫(kù)的創(chuàng)建與連接分三步走:

(1)導(dǎo)入模塊

import sqlite3
#或者:
from sqlite3 import dbapi2       #導(dǎo)入sqlite3模塊的dbapi2接口模塊 

(2)利用connect方法創(chuàng)建數(shù)據(jù)庫(kù)

connection=sqlite3.connect(filename) 
#filename為數(shù)據(jù)庫(kù)文件名,如果該文件存在則打開該數(shù)據(jù)庫(kù),如果不存在則創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù)文件。 
#該方法返回一個(gè)數(shù)據(jù)庫(kù)連接對(duì)象

(3)關(guān)閉連接對(duì)象

connection.close() 
#關(guān)閉連接,更新數(shù)據(jù)庫(kù)文件

SQL語(yǔ)句創(chuàng)建數(shù)據(jù)表

表是數(shù)據(jù)庫(kù)中存放關(guān)系數(shù)據(jù)的集合,一個(gè)數(shù)據(jù)庫(kù)里通常包含了多個(gè)表,如學(xué)生表、班級(jí)表、教師表等,表和表之間通過外鍵關(guān)聯(lián)。

在SQL中,利用create語(yǔ)句創(chuàng)建表語(yǔ)法結(jié)構(gòu)如下:

create table 表名(字段1,…,字段n)

如,創(chuàng)建mytb表:

create table if not exists mytb( xm char, cj real, kc text )

表名是mytb;IF NOT EXISTS表示如果數(shù)據(jù)庫(kù)中不存在mytb數(shù)據(jù)表,就創(chuàng)建該表;如果該數(shù)據(jù)表已經(jīng)存在,則什么也不做;

xm char, cj real, kc text表示該數(shù)據(jù)表有3個(gè)字段,xm(姓名)是字符串類型,cj(成績(jī))是浮點(diǎn)數(shù)類型,kc(課程)是文本字符串。

SQLite3支持的數(shù)據(jù)類型有:

null(值=空)、integer(整數(shù))、real(浮點(diǎn)數(shù))、text(字符串文本)、blob(二進(jìn)制數(shù)據(jù)塊)。

execute()方法

在Python中我們可以使用 execute 方法執(zhí)行一條SQL語(yǔ)句

conn.execute('create table if not exists mytb( xm char, cj real, kc text )')

conn為連接對(duì)象 execute()方法中參數(shù)為一條SQL語(yǔ)句,類型為字符串

插入記錄

(1)插入記錄的SQL語(yǔ)句

語(yǔ)法格式如下 :

insert into 表名 [字段名] values [常量] 

如:

insert into Persons values ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing')

(2)利用execute()執(zhí)行SQL語(yǔ)句

cur.exceute(sql語(yǔ)句)

(3)提交事務(wù)

conn.commit() #提交事務(wù),將數(shù)據(jù)寫入文件,保存到磁盤中。

查詢SQL語(yǔ)句

從“表”中查詢滿足條件表達(dá)式的“目標(biāo)列”

SELECT 目標(biāo)列 FROM 表 [WHERE 條件表達(dá)式]

如,查詢年齡20歲以下的學(xué)生姓名及其年齡:

select sname age from student where age<20

如,查詢表中所有記錄:

select * from student

fetchall()

返回多條記錄(rows),如果沒有結(jié)果,則返回空()

sqlite_master表

每一個(gè) SQLite 數(shù)據(jù)庫(kù)都有一個(gè)叫 sqlite_master 的表,該表會(huì)自動(dòng)創(chuàng)建。

sqlite_master是一個(gè)特殊表, 存儲(chǔ)數(shù)據(jù)庫(kù)的元信息, 如表(table), 索引(index), 視圖(view), 觸發(fā)器(trigger), 可通過select查詢相關(guān)信息。

select name,sql from sqlite_master where type='table'

該語(yǔ)句用于查詢數(shù)據(jù)庫(kù)中數(shù)據(jù)表的名稱name,以及創(chuàng)建表的SQL語(yǔ)句

更新記錄

更新記錄的SQL語(yǔ)句:

UPDATE 表名 SET 列名=表達(dá)式… [WHERE 條件]

當(dāng)“條件”成立時(shí),將某列的值改為“表達(dá)式” 如:

update student set cj=90 where xh="001" 

可將001號(hào)同學(xué)的成績(jī)改為90

刪除記錄

DROP TABLE和DELETE語(yǔ)句:

(1)刪除數(shù)據(jù)表所有記錄

DELETE FROM <表名>

如,刪除student表

delete from student

(2)刪除記錄

DELETE FROM <表名> WHERE <條件>

如,刪除表中學(xué)號(hào)xh為'001'記錄

delete from student where xh='001'

(3)刪除整個(gè)數(shù)據(jù)表

DROP TABLE 表名

如,刪除student表結(jié)構(gòu)

drop table student

例題練習(xí)

第1關(guān):創(chuàng)建和連接數(shù)據(jù)庫(kù)文件

本關(guān)任務(wù):在當(dāng)前目錄下,創(chuàng)建和連接mytest.db數(shù)據(jù)庫(kù)。

代碼解析

def return_values():
#***********Begin**********#
    #(1)導(dǎo)入內(nèi)置sqlite3模塊
    import sqlite3
    #(2)創(chuàng)建conn連接對(duì)象(在當(dāng)前路徑下建立mytest.db數(shù)據(jù)庫(kù))
    conn = sqlite3.connect("mytest.db")
    #(3)關(guān)閉連接
    conn.close()
#***********End**********#

第2關(guān):創(chuàng)建數(shù)據(jù)表

本關(guān)任務(wù):創(chuàng)建或打開數(shù)據(jù)表示例。

代碼解析

#(1)導(dǎo)入sqlite3模塊
import sqlite3
#(2)創(chuàng)建conn連接對(duì)象,建立mytest.db數(shù)據(jù)庫(kù)
conn = sqlite3.connect("mytest.db")
#(3)定義sql語(yǔ)句,創(chuàng)建mytb數(shù)據(jù)表,表中有三個(gè)字段xm、cj、kc,其數(shù)據(jù)類型分別為char、real、text
sql_demo = "create table if not exists mytb( xm char , cj real , kc text )"
#(4)執(zhí)行sql語(yǔ)句
conn.execute(sql_demo)
#(5)關(guān)閉連接
conn.close()

第3關(guān):插入記錄

本關(guān)任務(wù):創(chuàng)建一個(gè)sqlite3數(shù)據(jù)庫(kù)文件mytest.db,再創(chuàng)建一個(gè)數(shù)據(jù)表mytb.db,往表里插入三行記錄。

代碼解析

#(1)導(dǎo)入sqlite3模塊
import sqlite3
#(2)創(chuàng)建數(shù)據(jù)庫(kù)文件mytest.db
conn = sqlite3.connect("mytest.db")
#(3)定義一個(gè)游標(biāo)對(duì)象
cur = conn.cursor()
#(4)定義創(chuàng)建數(shù)據(jù)表SQL語(yǔ)句
sql_create = "create table if not exists mytb(xm char,cj real,kc text)"
sql_insert_by = "insert into mytb values ('寶玉',85,'計(jì)算機(jī)')"
sql_insert_dy = "insert into mytb values ('黛玉',90,'計(jì)算機(jī)')"
sql_insert_bc = "insert into mytb values ('寶釵',80,'數(shù)據(jù)庫(kù)')"
#(5)執(zhí)行SQL語(yǔ)句,創(chuàng)建數(shù)據(jù)表mytb
conn.execute(sql_create)
#(6)依次插入3條記錄,內(nèi)容分別為:('寶玉',85,'計(jì)算機(jī)')、('黛玉',92,'計(jì)算機(jī)')、('寶釵',80,'數(shù)據(jù)庫(kù)')
cur.execute(sql_insert_by)
cur.execute(sql_insert_dy)
cur.execute(sql_insert_bc)
#(7)提交事務(wù)
conn.commit()
#(8)關(guān)閉連接
cur.close()
conn.close()

第4關(guān):查詢記錄

本關(guān)任務(wù):設(shè)計(jì)一個(gè)程序,查詢已有數(shù)據(jù)庫(kù)文件myfile.db中數(shù)據(jù)表mytb中所有記錄,及查詢數(shù)據(jù)表結(jié)構(gòu)。

代碼解析

#(1)導(dǎo)入sqlite3模塊
import sqlite3
#(2)打開數(shù)據(jù)庫(kù)文件myfile.db
conn = sqlite3.connect("myfile.db")
#(3)定義一個(gè)游標(biāo)對(duì)象
cur = conn.cursor()
sql_select = "select * from mytb"
#(4)查詢數(shù)據(jù)表mytb中所有記錄,并賦值給列表
cur.execute(sql_select)
lst = cur.fetchall()
#(5)輸出記錄數(shù)
print(f"共{len(lst)}條記錄")
#(6)輸出所有記錄列表
print(lst)
#(7)從sqlite_master表中查詢數(shù)據(jù)表的名稱和創(chuàng)建時(shí)的sql語(yǔ)句,查詢結(jié)果賦值給列表,并輸出列表內(nèi)容
cur.execute("select name,sql from sqlite_master ")
print(cur.fetchall())

第5關(guān):更新和刪除記錄

本關(guān)任務(wù):在sqlite數(shù)據(jù)庫(kù)中更新和刪除記錄

代碼解析

#(1)導(dǎo)入sqlite3模塊
import sqlite3
#(2)打開數(shù)據(jù)庫(kù)my.db
conn = sqlite3.connect("my.db")
#(3)定義游標(biāo)
cur = conn.cursor()
#(4)更新記錄,將xm“寶玉”的成績(jī)cj改為0
cur.execute("update mytb set cj = 0 where xm = '寶玉'")
#(5)刪除成績(jī)cj>90的記錄
cur.execute("delete from mytb where cj > 90")
#(6)刪除數(shù)據(jù)表mytb中所有記錄
cur.execute("delete from mytb")
#(7)刪除整個(gè)數(shù)據(jù)表mytb
cur.execute("drop table mytb")
#(8)提交事務(wù)
conn.commit()
#(9)關(guān)閉游標(biāo)
cur.close()
#(10)關(guān)閉數(shù)據(jù)庫(kù)連接
conn.close()

第6關(guān):圖書數(shù)據(jù)庫(kù)的綜合操作

本關(guān)任務(wù):在SQLite中創(chuàng)建數(shù)據(jù)庫(kù)mybook.db;在數(shù)據(jù)庫(kù)中創(chuàng)建數(shù)據(jù)表mytb;在表中定義:isbn(text)、書名(text)、價(jià)格(real)等字段,并插入記錄。

代碼解析

#(1)導(dǎo)入sqlite3模塊
import sqlite3
#(2)創(chuàng)建數(shù)據(jù)庫(kù)mybook.db
conn = sqlite3.connect("mybook.db")
#(3)定義游標(biāo)對(duì)象
cur = conn.cursor()
#(4)創(chuàng)建表mytb
cur.execute("create table if not exists mytb ( isbn text , 書名 text , 價(jià)格 real)")
#(5)插入記錄('9787302518358','歐美戲劇選讀',88.00)
cur.execute("insert into mytb values ('9787302518358','歐美戲劇選讀',88.00)")
#(6)插入記錄('9787302454038','組織理論與設(shè)計(jì) 第12版',75.00)
cur.execute("insert into mytb values ('9787302454038','組織理論與設(shè)計(jì) 第12版',75.00)")
#(7)插入記錄('9787302496878','中國(guó)文化經(jīng)典讀本',45.00)
cur.execute("insert into mytb values ('9787302496878','中國(guó)文化經(jīng)典讀本',45.00)")
#(8)提交事務(wù),保存數(shù)據(jù)
conn.commit()
#(9)讀入所有記錄,將查詢記錄賦值給列表,遍歷輸出列表內(nèi)容
cur.execute("select * from mytb")
lst = cur.fetchall()
for x in lst:
    print(x)
#(10)關(guān)閉游標(biāo)
cur.close()
#(11)關(guān)閉數(shù)據(jù)庫(kù)
conn.close()

到此這篇關(guān)于Python中SQLite數(shù)據(jù)庫(kù)的使用的文章就介紹到這了,更多相關(guān)Python SQLite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論