詳見 iOS開發(fā) 文件存儲方式
Core Date:Core Data是iOS5之后才出現(xiàn)的一個框架,它提供了對象-關(guān)系映射(ORM)的功能,即能夠?qū)C對象轉(zhuǎn)化成數(shù)據(jù),保存在SQLite數(shù)據(jù)庫文件中,也能夠?qū)⒈4嬖跀?shù)據(jù)庫中的數(shù)據(jù)還原成OC對象。在此數(shù)據(jù)操作期間,我們不需要編寫任何SQL語句。由于功能太過強(qiáng)大,所以帶來的性能損耗也比較大,在此先不介紹Core Data的處理方式,有關(guān)Core Data與SQLite 的選擇,詳見談?wù)動?SQLite 和 FMDB 而不用 Core Data。
SQLite:
什么是SQLite
什么是數(shù)據(jù)庫
常用關(guān)系數(shù)據(jù)庫
數(shù)據(jù)庫是如何存儲數(shù)據(jù)的 : 數(shù)據(jù)庫的存儲結(jié)構(gòu)和Excel很像,以表(table)為單位.
數(shù)據(jù)庫存儲數(shù)據(jù)的步驟
1.SQL語句的特點(diǎn):
不區(qū)分大小寫
每條語句必須以分號結(jié)尾
2.SQL中的常用關(guān)鍵詞
3.數(shù)據(jù)庫中不可以使用關(guān)鍵字來命名表、字段
數(shù)據(jù)定義語句 (DDL:Data Definition Language)
數(shù)據(jù)操作語句(DML:Data Manipulation Language)
數(shù)據(jù)查詢語句(DQL:Data Query Language)
創(chuàng)表
格式(一般表名以t_作為前綴)
字段類型
示例
create table t_student (id integer, name text, age inetger, score real) ;
刪表
格式
示例
插入數(shù)據(jù)(insert)
格式 : 數(shù)據(jù)庫中的字符串內(nèi)容應(yīng)該用單引號 ’ 括住
示例
insert into t_student (name, age) values ('kingsly', 20) ;
更新數(shù)據(jù)(update)
示例
update t_student set name = ‘Roger’, age = 34 ;
刪除數(shù)據(jù) (delete)
示例
delete from t_student;
格式
select 字段1,字段2,...from 表名;
select * from 表名; // 查詢所有字段
示例
select name,age frome t_student; select * frome t_student;
重命名字段名
格式
示例
給name起個叫做myname的別名,給age起個叫做myage的別名
select name myname, age myage from t_student ;
給t_student表起個別名叫做s,利用s來引用表中的字段
select s.name, s.age from t_student s ;
條件語句常見的格式
示例
將t_student表中年齡大于10 并且 姓名不等于jack的記錄,年齡都改為 5
update t_student set age = 5 where age > 10 and name != ‘jack’ ;
select * from t_student order by age ;
默認(rèn)是按照升序排序(由小到大),也可以變?yōu)?strong>降序(由大到小)
select * from t_student order by age desc ; //降序 select * from t_student order by age desc ; //降序
也可以用多個字段進(jìn)行排序
先按照年齡排序(升序),年齡相等就按照身高排序(降序)
select * from t_student order by age asc, height desc ;
使用limit可以精確地控制查詢結(jié)果的數(shù)量
格式
示例
select * from t_student limit 5, 10 ;
示例
name字段不能為null,并且唯一,age字段不能為null,并且默認(rèn)為1
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
主鍵
Primary Key ,用來唯一地標(biāo)識某一條記錄
t_student 表中,若沒有設(shè)置一個字段為主鍵,則難免會出現(xiàn)幾個記錄中name和age完全相等的情況,因此需要一個標(biāo)識來區(qū)分,比如人的身份證id,來區(qū)分同名和相同年齡的人
主鍵可以是一個或者多個字段
主鍵應(yīng)當(dāng)不影響用戶記錄的數(shù)據(jù),最好是由電腦自動生成主鍵
主鍵的聲明
在創(chuàng)表的時候用primary key聲明一個主鍵,eg:用一個integer類型的id字段作為t_student表的主鍵
create table t_student (id integer primary key, name text, age integer) ;
主鍵字段默認(rèn)就包含了not null 和 unique 兩個約束
讓integer類型的主鍵自動增長,需要添加 autoincrement,一般情況下讓主鍵自動增加便于管理
create table t_student (id integer primary key autoincrement, name text, age integer) ;
外鍵約束
學(xué)生表的班級字段,來自班級表中的id字段
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_t_student_class_id_t_class_id foreign key (class_id) (id)) ; references t_class
表連接查詢
表連接的類型
內(nèi)連接:inner join 或者 join (顯示的是左右表都有完整字段值的記錄)
左外連接:left outer join (保證左表數(shù)據(jù)的完整性)
示例:查詢網(wǎng)絡(luò)工程2班的所有學(xué)生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.
新聞熱點(diǎn)
疑難解答
圖片精選