需求做用: 若是須要保存大量的結構較爲複雜的數據時候, 使用數據庫, 例如交規考試項目sql
經常使用的數據庫:數據庫
(1)Microsoft SQL Server 2000/2008, 中小企業使用較多設計模式
(2)Oracle 比較複雜, 大企業使用較多安全
(3)Mysql數據庫, 網站使用較多網站
(4)sqlite: 本地數據庫, 訪問數據足夠快, 直接訪問文件spa
足夠簡單, 功能相對其餘數據庫軟件不是特別齊全, 足夠用了設計
足夠小, 系統不超過1M, 適合在移動端上使用code
實例: 使用數據存儲存儲一個班上學生的信息orm
學號sid 用戶名username 密碼password 成績scoresqlite
1501 zhangsan 123 100
1502 lilei 321 90
1503 wangwu 222 80
(1)建立數據庫
(2)建立數據表
(3)設計數據表(添加多個字段/列)
(4)數據庫經常使用操做
增,刪,改,查
SQL, Structure Query Language, 結構化查詢語言, 做用就是操做數據庫(建立表, 數據增刪改查)
(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'
(1)配置
導入文件,
添加二進制庫 libsqlite3.dylib,
包含頭文件#import "FMDatabase.h"
#import "FMDatabase.h" @interface ViewController () { //數據庫對象 FMDatabase *_database; }
//實例: 存儲學生信息 //(1)建立數據庫 [self createAndInitDatabase]; //(2)建立數據表 [self createTable]; //(3)插入數據 //[self insertData]; //(4)查詢數據 [self queryData]; //(5)修改和刪除數據 //參考: 插入數據 } -(void)queryData { //顯示全部人的信息 NSString *sql = @"select * from StudentInfo"; //FMResultSet 表示查詢後結果集 FMResultSet *resultSet = [_database executeQuery:sql]; //next方法每次獲取一條記錄, 獲取不到返回nil while ([resultSet next]) { NSLog(@"sid = %@,name=%@,pa=%@,score=%@", [resultSet stringForColumn:@"sid"], [resultSet stringForColumn:@"username"], [resultSet stringForColumn:@"password"], [resultSet stringForColumn:@"score"]); } } -(void)insertData { int sid = 1502; NSString *username = @"lilei"; NSString *password = @"321"; NSString *score = @"80"; NSString *sql = @"insert into StudentInfo(sid,username,password,score) values(?,?,?,?)"; //注意: ?對應的每個參數都應該是字符串, 其餘類型轉化爲字符串 BOOL b = [_database executeUpdate:sql,[NSString stringWithFormat:@"%d",sid], username, password, score]; NSLog(@"insert b = %d",b); } -(void)createTable { NSString *sql = @"create table if not exists " "StudentInfo(sid integer," "username varchar(20)," "password varchar(20)," "score varchar(20));"; //executeQuery用來執行select語句, //其餘語句使用executeUpdate BOOL b = [_database executeUpdate:sql]; NSLog(@"create b = %d",b); } -(void)testSandbox { //iOS安全機制 - 沙盒 //(1). 每一個應用內容都放在一個沙盒目錄下面 //(2). 每一個應用只能修改本身沙盒目錄下的文件, 其餘應用文件沒法修改 //(3). 默認文件夾 Documents,Library,tmp //開發: 本身建立的文件放在Documents下面 //肯定文件位置 //當前應用文件夾: NSHomeDirectory() //NSLog(@"home = %@",NSHomeDirectory()); //NSLog(@"%@",[[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSHomeDirectory() error:nil]); } -(void)createAndInitDatabase { //設置路徑 NSString *path = [NSString stringWithFormat:@"%@/Documents/stuInfo.sqlite",NSHomeDirectory()]; //建立數據庫(若是不存在則建立打開, 若是存在則直接打開) _database = [[FMDatabase alloc] initWithPath:path]; if(!_database.open) { NSLog(@"打開失敗"); return; } NSLog(@"打開成功"); }