iOS中,使用的是SQLite資料庫
但是原生SQLite用起來比較麻煩,是以FMDB對他進行了封裝
通過FMDB,可以使用SQL語句進行資料庫操作
SQLite資料庫
1、資料庫
資料庫(Database/DB)是一種以資料模型組織起來并存放存儲管理資料的資料倉庫
(資料存到磁盤上,仍然保留原有的資料模型結構。可以直接進行選擇,篩選,添加,删除等管理操作)
資料庫是資料管理的進階階段,它是由檔案管理系統發展起來的。
資料庫是個檔案,操作對資料的增、删、改、查。由統一資料庫軟體來進行管理和控制。
2、衆多的資料庫
Access
Oracle
SQLite
DB2
Mysql
3、SQLite資料庫
SQLite官網:http://www.sqlite.org
SQLite是幾乎所有的手機系統(iOS,Android,Symbian,Blackberry,Lumia,Limo等)的标準資料庫
SQLite資料庫中的表單由行和列組成
比如“演員”資料庫中,可以有男演員,女演員兩個表單。表單類似資料模型,是以列,就稱為資料模型的字段。每條記錄就是資料模型的一個執行個體對象。
ID 演員 國籍
———————————————————————
1 舒淇 中國香港
2 李麗珍 中國香港
3 蒼井空 日本
4 松島楓 日本
5 奧巴馬 美國
列(字段/域):ID 演員 國籍
5行資料: 一行資料組合到一起稱為一條記錄
【注】操作資料庫,需要資料庫操作軟體和操作語句。
SQL語句
SQL語句通常寫作(Structured Query Language),是專門操作資料庫的一種語言。
【特征】
<1>所有資料在表單中進行管理
<2>資料沒有順序,至少SQL不區分順序
<3>語句不區分大小寫
<4>表單中必須有主鍵列,每個主鍵唯一
1、SQL中的資料類型
integer: 整型資料,大小為4個位元組。
bigint: 整型資料,大小為8個位元組。
smallint: 整型資料,大小為2個位元組。
tinyint: 正整型資料,大小為1個位元組。
float: 浮點資料,大小為4個位元組。
double: 浮點資料,大小為8個位元組。
real: 浮點資料,大小8個位元組。
char(n): 非變長字元串,不超過n個字元(n < 255) //不夠n,仍然存儲n個位元組
varchar(n): 變長字元串,不超過n個字元(n <= 4000)//最常用 存儲空間,随字元串到小改變text: 變長非unicode字元串,存儲超大資料(n <= 2^31-1)date: 日期資料,年份-月份-日期time: 時間資料,時:分:秒datetime: 日期時間資料,年份-月份-日期 時:分:秒timestamp: 日期時間資料,年份-月份-日期 時:分:秒 毫秒blob: 二進制,BOOL值
null: 空
【注】字元串和日期時間,需要加入單引号,日期必須嚴格按照格式,如14年6月2日,必須寫作2014-06-02,不能寫作14-6-2,時間也一樣需要補零。
2、SQL操作
1)建立表2)插入記錄3)更新記錄4)查詢記錄5)删除記錄
6)删除表
3、where語句的篩選運算符
= 或 == 或 like 等于> 大于< 小于>= 大于等于<= 小于等于<> 不等于!> 不大于!< 不小于%舒% 包含子字元串”舒” (%:一個或多個字元)
4、基本sql語句
1. 建立表 create table [if not exists] 表名 ( 屬性名 類型 ( 字元個數,可以省略 ) primary key autoincrement, 屬性名 類型 ( 字元個數,可以省略 ) null( 不可以省略 ), 屬性名 類型 ( 字元個數,可以省略 ) not null( 可以省略 ), 屬性名 類型 ( 字元個數,可以省略 ) not null( 可以省略 ) ); autoincrement 自動增長 , 確定主鍵不會重複 primary key 主鍵 , 唯一辨別一條記錄 , 每個表都需要一個主鍵 . 注意 類型有 integer,text,blob 等 例如: create table if not exists Book ( id integer primary key autoincrement, name text, url text, des text );
2. 表中插入資料 (屬性要全部寫嗎?不用; 可以插入一條資料嗎?) insert into 表名 ( 屬性名 1, 屬性名 2, 屬性名 3) values ('','',''); 例如 insert into Book (name,url,des) values (' 霍金全集 ',' www.baidu.com ',' 描述 '); insert into Book(name,url,des) values('JK','HarryPotter','magic'); insert into Book(name,url,des) values('Luxun','Nahan','chiren') insert into A select * from B insert into MyCollections select * from Home where id=1; 插入屬性 alter table Book add column price float
3. 查詢
select 字段名( * 代表所有字段,如果有多個字段用英文的 , 分割) from 表名字 [where 字段名 =‘’] order by 字段名 desc | asc; 注意: 1,where 判斷 > , >= ,< , <=,between 字段 and 字段
2,desc 降序 asc 升序 select * from Book order by id desc 例如: ( 1 )查詢表中所有字段資料
select * from Book; ( 2 )查詢判斷的條件 select * from Book where 字段名 =''; (3)select name,url from Book where name = ' 鋼鐵是怎麼煉成的 ' where bool = 1
4. 模糊查詢
% 替代一個或多個字元 _ 僅替代一個字元 select 字段名 from 表名 where 屬性 like '% 關鍵字 %';
例如 select * from Book where name like '% 鋼鐵 %';
5. in查詢 将在括号的内容查詢出來 . not in 将不在括号中的内容查詢出來
select 字段名 from 表名 where 字段名 in (' 關鍵字 ',' 關鍵字 '); 例如: select * from Book where name in (' 我的青春 ',' 鋼鐵是怎麼煉成的 ');
6. 更新
update 表名 set 屬性名 = ' 關鍵字 ' where 屬性名 = 關鍵字
例如: update Book set url=' www.aaa.com ' where id = 2 update Book set price = 200 where id = 2
7. 删除資料
delete from 表名 where 屬性名 = ' 關鍵字 '; 例如: delete from Book where name = ' 霍金全集 ';
8. 删除整張表 drop table 表名 ;
9. 添加屬性 ( 字段 ) (删除屬性?) alter table 表名 add column 屬性名 類型 ; 例如: alter table Book add column price integer;
聚合函數
10. 求和 select sum( 屬性名 ) from 表名 select sum(price) from Book select sum(id), sum(price) from Book 例如: select sum(age) from User NSInteger count = [rs intForColumn : @"sum(buyNum)" ];
11. 求平均值 select avg( 屬性名 ) from 表名 例如: select avg(age) from User select avg(id), avg(price) from Book
12. 求最大值 select max( 屬性名 ) from 表名 例如: select max(age) from User select max(id), max(price) from Book
13. 求最小值 select min( 屬性名 ) from 表名 例如: select min(age) from User
14. 求元組個數 (求共幾條資料?)
select count(*) from 表名 select count(distinct|all 屬性名 ) from 表名 如果指定 DISTINCT 短語,則表示在計算時要取消指定列中的重複值。如果不指定 DISTINCT 短語或指定 ALL 短語( ALL 為預設值),則表示不取消重複值。 消除重複 例如: select count(*) from User select count(distinct name) from User
FMDB使用
因為在程式的很多地方,都需要使用到資料庫存儲.為了避免每次都打開,關閉資料庫,我們把它做成單例
擷取單例的命名規則,一般用,defalut,standard,shareInstance
1.FMDB特性
2.使用FMDB操作資料庫
1)打開資料庫
- (instancetype)databaseWithPath:(NSString *)aPath;
開啟資料庫
open
2)關閉資料庫
- (BOOL) close;
3)表建立
- (BOOL) executeUpdate:(NSString *)sql, ...;
4)增加一條記錄
5)删除一條記錄
6)修改一條記錄
7)查詢記錄
- (FMResultSet *) executeQuery:(NSString *)sql, ...;
①周遊結果
是否有下一條記錄
- (BOOL) next;
②擷取資料
- (int)[資料類型]ForColumn:(NSString *)columnName;
- (int)[資料類型]ForColumnIndex:(int)
-stringForColumn:
8)删除表