SQLite數(shù)據(jù)庫

SQLite數(shù)據(jù)庫

1.數(shù)據(jù)庫簡介

1.1數(shù)據(jù)庫的作用:"離線緩存數(shù)據(jù)"

1.2數(shù)據(jù)緩存策略

plist

歸檔

偏好設(shè)置

沙盒文件

"SQLite數(shù)據(jù)庫"

1.3系統(tǒng)提供的數(shù)據(jù)存儲方式的弊端:

不方便操作大量的數(shù)據(jù)

-系統(tǒng)提供的數(shù)據(jù)存儲方式都是覆蓋存儲的,新的數(shù)據(jù)會覆蓋舊的數(shù)據(jù).而且當數(shù)據(jù)量非常大時,如果要添加新的數(shù)據(jù),必須先把舊數(shù)據(jù)全部加載到內(nèi)存中

不方便查找大量的數(shù)據(jù)

-當數(shù)據(jù)量非常龐大時,要查詢其中某些數(shù)據(jù),就非常困難

代碼演示plist文件保存數(shù)據(jù)

1.4SQLite簡介

SQLite是一個輕量級,體積小,嵌入式,功能強大,跨平臺的數(shù)據(jù)存儲方式

客戶端的數(shù)據(jù)庫: SQLite

服務(wù)器端的數(shù)據(jù)庫: MySQL --> SQLServer --> Oracle

1.5學習方式

先用Navicat客戶端演示SQLite的使用(僅僅是上課演示而已,不需要掌握)

再用代碼演示SQLite的使用(需要熟練使用)

2.Excel存儲數(shù)據(jù)

2.1提示: SQLite數(shù)據(jù)庫存儲數(shù)據(jù)的結(jié)構(gòu)和Excel很像

"Excel""SQLite"

-創(chuàng)建Excel文件-創(chuàng)建一個數(shù)據(jù)庫文件

-創(chuàng)建表-創(chuàng)建表

-確定表頭-創(chuàng)建數(shù)據(jù)庫的字段名(Excel表頭),并指定數(shù)據(jù)類型

-對Excel表進行數(shù)據(jù)操作-對數(shù)據(jù)庫進行增刪改查

2.2一個數(shù)據(jù)庫中,可以有多張表,每個表中可以存儲不同的數(shù)據(jù)

3.Navicat存儲數(shù)據(jù)-創(chuàng)建數(shù)據(jù)庫和表

3.1提示:數(shù)據(jù)庫本身就是一個文件,可以用終端和客戶端來創(chuàng)建和操作

3.2Navicat創(chuàng)建數(shù)據(jù)庫和表的步驟:

1.創(chuàng)建一個數(shù)據(jù)庫文件,以".sqlite"結(jié)尾

2.建立Navicat和數(shù)據(jù)庫文件的連接,只有建立好連接才能操作數(shù)據(jù)庫文件

3.創(chuàng)建數(shù)據(jù)庫表,創(chuàng)建表頭并指定數(shù)據(jù)類型

4.保存數(shù)據(jù)庫文件,以"t_xxx"命名

4.Navicat操作數(shù)據(jù)-增刪改查

4.1在數(shù)據(jù)庫中

行:記錄/數(shù)據(jù)(一條記錄/一條數(shù)據(jù))

列:字段名/列名

4.2增刪改查操作

新增:'+'

刪除:'-'

修改:雙擊字段直接修改

查詢:'filter'(過濾)

4.3以上操作都是在Navicat客戶端操作的,實際開發(fā)中是在Xcode中寫代碼.

5.SQL語句之數(shù)據(jù)定義語句-創(chuàng)建和刪除表

iOS ??????????? OC

Android ??????? JAVA

SQLite數(shù)據(jù)庫"SQL語句"

5.1SQL語句:一個通用的數(shù)據(jù)庫語言,適用于客戶端數(shù)據(jù)庫(SQLite)和服務(wù)器端數(shù)據(jù)庫(MySQL,SQLServer,Oracle)

5.2客戶端的SQL語句需要掌握:"數(shù)據(jù)定義語句","數(shù)據(jù)操作語句","條件語句","數(shù)據(jù)查詢語句"

5.3數(shù)據(jù)定義語句:創(chuàng)建(create)和刪除(drop)數(shù)據(jù)庫表

-創(chuàng)建數(shù)據(jù)庫表:

create tableifnot exists表名(字段名1類型,字段名2類型,...);

"create table if not exists t_product(productID integer,productName text,productPrice integer);"

-刪除數(shù)據(jù)庫表

drop tableifexists表名;

"drop table if exists t_product;"

6.SQL語句之數(shù)據(jù)操作語句-增刪改

-提示:到目前為止數(shù)據(jù)庫和數(shù)據(jù)庫表都已經(jīng)創(chuàng)建好了,現(xiàn)在開始就是操作數(shù)據(jù)庫

6.1新增語句

insert into表名(字段名1,字段名2,...) values(字段1值,字段2值,...);

"insert into t_product(productID,productName,productPrice) values(1005,'iPhone103',2000);"

注意:字符串用'單引號'引起來

6.2刪除語句

delete from表名where條件語句;

"delete from t_product where productPrice = 1000;"

注意:如果執(zhí)行刪除語句時不加條件語句,就會刪除表中所有的記錄

6.3修改語句

不加條件時,所有的productPrice字段的值都會修改成103

"update t_product set productPrice = 103;"

按照條件修改一個字段的值

"update t_product set productPrice = 333 where productID = 1005;"

按照條件,修改一條記錄的多個字段

"update t_product set productName = 'iPHone1000',productPrice = 1000 where productID = 1002;"

7.SQL語句之查詢語句(相當重要)

7.1準備工作:執(zhí)行提前準備好的新增語句,準備要查詢的數(shù)據(jù)

7.2查詢語句體驗

查詢價格大于5500的商品信息

"select * from t_product where productPrice > 5500;"

查詢價格小于4000或者價格大于5500的商品信息

"select * from t_product where productPrice < 4000 or productPrice > 5500;"

查詢商品編號小于100并且價格小于4000的商品信息

"select * from t_product where productID < 100 and productPrice < 4000;"

注意:"select *"是查找表中的所有字段

8.排序和分頁查找

8.1排序

提示:分頁和排序都是在某個查詢結(jié)果后面執(zhí)行進行的

使用場景:電商類APP中的商品按照價格的從高到低篩選展示

查詢出來的數(shù)據(jù),按照價格從低到高排序

"select * from t_product where productID < 100 and productPrice < 4000 order by productPrice;"

查詢出來的數(shù)據(jù),按照價格從高到低排序

"select * from t_product where productID < 100 and productPrice < 4000 order by productPrice desc;"

注意:"order by"是默認從低到高;"desc"是從高到低

8.2分頁查詢

從第0條開始查詢,查詢五條記錄

"select * from t_product where productID < 100 and productPrice < 4000 order by productPrice desc limit 0,5;"

"limit 0,5":分頁語句;索引,每頁條數(shù).從第0條開始查詢,每頁查詢五條記錄

規(guī)律:

第1頁: limit0,5

第2頁: limit5,5

第3頁: limit10,5

...

第n頁: limit (n-1)*每頁條數(shù),每頁條數(shù)

注意:

當取到最后,記錄不足每頁條數(shù)時,就取實際剩下的記錄

分頁語句放在查詢語句的后面

9.模糊查詢

9.1使用場景:關(guān)鍵字搜索

查找商品價格中帶99的商品

"select * from t_product where productPrice like '%99%';"

查找商品價格中帶99的商品,按照價格從高到低排序

"select * from t_product where productPrice like '%99%' order by productPrice desc;"

查找商品價格中帶99的商品,按照價格從高到低排序,取前面五條

"select * from t_product where productPrice like '%99%' order by productPrice desc limit 0,5;"

10.主鍵字段

10.1提示:

當有兩條記錄是一樣的時候,可以使用主鍵字段來區(qū)別

主鍵可以自增長,數(shù)據(jù)庫建議我們每個表中都要有主鍵字段

10.2主鍵字段演練

增加字段(了解)

"alter table t_studentInfo add id_replace integer;"

新建表,并設(shè)置主鍵字段

"create table if not exists t_class(id integer primary key,className text not null,classNO integer);"

10.3注意:

"primary key":某某字段為主鍵

"not null":某某字段不能為空

11.Xcode之原生SQLite -創(chuàng)建數(shù)據(jù)庫和表

11.1提示:導(dǎo)入頭文件"#import ";新增類庫"libsqlite3.0.tbd"

11.2使用步驟

1.創(chuàng)建數(shù)據(jù)庫

2.打開數(shù)據(jù)庫

3.建表(t_person)

4.操作表

11.3注意:

- sqlite3中的函數(shù)都是以sqlite3開頭的

-創(chuàng)建并打開數(shù)據(jù)庫函數(shù): sqlite3_open

-建表和數(shù)據(jù)操作函數(shù)(查詢函數(shù)除外) : sqlite3_exec

12.Xcode之原生SQLite -數(shù)據(jù)增刪改查

-建表和數(shù)據(jù)操作函數(shù)(查詢函數(shù)除外) : sqlite3_exec

-查詢函數(shù): sqlite3_prepare_v2

-查詢數(shù)據(jù)時會得到結(jié)果集,要查詢的數(shù)據(jù)都在結(jié)果集中,while循環(huán)遍歷結(jié)果集一條一條記錄取出來

13.FMDB -創(chuàng)建數(shù)據(jù)庫和表

13.1FMDB簡介

- FMDB是一個操作數(shù)據(jù)庫的第三方框架,并且支持多線程環(huán)境下的操作

-也是需要手動導(dǎo)入'libsqlite3.0.tbd'庫

-"FMDatabase":單線程;"FMDatabaseQueue":多線程;"FMResultSet":查詢的結(jié)果集

13.2演練保存Heros模型數(shù)據(jù)

準備模型類和保存模型數(shù)據(jù)的工具類

13.3FMDB使用步驟:

1.創(chuàng)建數(shù)據(jù)庫

2.打開數(shù)據(jù)庫

3.創(chuàng)建表

4.增刪改查

13.4注意:

-創(chuàng)建數(shù)據(jù)庫和建表只需要執(zhí)行一次,可以在"+ (void)initialize"方法中實現(xiàn)

-建表,增刪改操作都是同一個方法"[_db executeUpdate]"

-查詢是另外的方法"[_db executeQuery]"

15.FMDB增刪改查

15.1新增操作

[_db executeUpdateWithFormat:@"insert into t_heros(name,age) values(%@,%@)",hero.name,hero.age];

15.2刪除操作

[_db executeUpdateWithFormat:@"delete from t_heros where name = %@",hero.name];

15.3修改操作

[_db executeUpdateWithFormat:@"update t_heros set name = %@ where id = 3",hero.name];

15.4查詢操作

-需求:把查詢出來的數(shù)據(jù)展示在列表上

- UI準備:準備好列表

-數(shù)據(jù)準別

獲取結(jié)果集: FMResultSet *resultSet = [_db executeQuery:@"select * from t_heros"];

while循環(huán)遍歷結(jié)果集,把遍歷出來的數(shù)據(jù)轉(zhuǎn)成模型數(shù)據(jù),并添加到模型數(shù)組

取結(jié)果集中的數(shù)據(jù): NSString *name = [resultSet stringForColumn:@"name"];

16.FMDB模糊查找-關(guān)鍵字搜索

16.1注意:

1.大坑提示:模糊查詢時,需要自己拼接查詢語句,不要使用框架提供的

2.'%'是特殊字符,此時需要使用%轉(zhuǎn)義.即'%%'

3.自己拼接查詢語句時,就需要自己添加單引號把字符串引起來

16.2自己拼接模糊查詢語句

-錯誤的拼接和執(zhí)行模糊查詢的方式

FMResultSet *resultSet = [_db executeQueryWithFormat:@"select * from t_heros where name like '%%%@%%'",keyWord];

-正確的拼接和執(zhí)行模糊查詢的方式

NSString *selectSQL = [NSString stringWithFormat:@"select * from t_heros where name like '%%%@%%'",keyWord];

FMResultSet *resultSet = [_db executeQuery:selectSQL];

17.多線程操作數(shù)據(jù)庫的問題及解決辦法

-問題:當有多個線程同時操作一個數(shù)據(jù)庫時,只有一個數(shù)據(jù)會寫入.

-解決辦法:讓FMDatabaseQueue(串行隊列)來管理數(shù)據(jù)庫對象.多個操作按順序來進行

18.封裝FMDataBaseQueue單例

新建單例類,繼承自FMDataBaseQueue,保證只創(chuàng)建一次

把FMDatabaseQueue定義成單例的目的是為了保證隊列在內(nèi)存中是唯一的一個,當有多個數(shù)據(jù)庫操作任務(wù)時,都可以放在同一個隊列中

最后編輯于
?著作權(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)容