標籤:
iOS開發之資料庫FMDB 1.簡介
需求作用: 如果需要儲存大量的結構較為複雜的資料時候, 使用資料庫, 例如交規考試項目
常用的資料庫:
(1)Microsoft SQL Server 2000/2008, 中小企業使用較多
(2)Oracle 比較複雜, 大企業使用較多
(3)Mysql資料庫, 網站使用較多
(4)sqlite: 本機資料庫, 訪問資料足夠快, 直接存取檔案
足夠簡單, 功能相對其他資料庫軟體不是特別齊全, 足夠用了
足夠小, 系統不超過1M, 適合在移動端上使用
2. MesaSQlite使用
執行個體: 使用資料存放區儲存一個班上學生的資訊
學號sid 使用者名稱username 密碼password 成績score
1501 zhangsan 123 100
1502 lilei 321 90
1503 wangwu 222 80
(1)建立資料庫
(2)建立資料表
(3)設計資料表(添加多個欄位/列)
(4)資料庫常用操作
增,刪,改,查
3. SQL結構化查詢語句
SQL, Structure Query Language, 結構化查詢語言 (SQL), 作用就是操作資料庫(建立表, 資料增刪改查)
(1)建立資料表
create table StudentInfo(sid integer, username varchar(20), password varchar(20),score varchar(20))
create table if not exists StudentInfo(sid integer, username varchar(20), password varchar(20),score varchar(20))
(2)插入資料
insert into StudentInfo(sid,username,password,score) values(1503,‘wangwu‘,‘222‘,‘80‘)
(3)查詢資料
<1>查詢表格中所有資料
select * from StudentInfo;
<2>查詢指定的欄位
執行個體: 查詢所有名字username
select username from StudentInfo
<3>根據指定的條件進行查詢
執行個體: 尋找name為zhansan的所有資訊
select * from StudentInfo where username=‘zhangsan‘
<4>根據多個條件進行查詢
執行個體: 尋找uname為zhansan, 並且性別為boy的所有資訊
select * from StudentInfo where username=‘zhangsan‘ and password=‘123‘
<5>查詢後需要排序
//根據age升序排列
select * from StudentInfo order by score
select * from StudentInfo order by score desc
<6>擷取資料行數
select count(*) from StudentInfo
(4)修改資料
update StudentInfo set score=‘100‘ where username=‘zhangsan‘;
(5)刪除資料
delete from StudentInfo where sid=‘1503‘
4. FMDB操作資料庫
(1)配置
匯入檔案,
添加二進位庫 libsqlite3.dylib,
包含標頭檔#import "FMDatabase.h"
5. 資料庫在項目中使用-單例設計模式
iOS開發之資料庫FMDB