一、為什么要使用SQLite3,什么是SQLite3,常見的數據庫可以用於移動端嘛?
1、持久化存儲的方式有以下幾種:
1、plist 只能存儲NSDictionary 和NSArray的數據。 2、Preference 偏好設置NSUserDefaults,存取方便,但只能存儲小數據。 3、NSCoding (NSKeyedArchiver\NSkeyedUnarchiver)歸檔可以存儲大數據,但是不方便存取,每次存儲都會覆蓋上一次的內容。 4、SQLite3 重點,存儲速度快,而且可以存儲大數據且取數據比較方便,可以取出單條數據。 5、Core Data 是對SQLite3的封裝。
2、什么是SQLite3,有什么優點?
1) SQLite的優點 SQLite是一款輕型的嵌入式數據庫 它占用資源非常的低,在嵌入式設備中,可能只需要幾百K的內存就夠了 它的處理速度比Mysql、PostgreSQL這兩款著名的數據庫都還快 2)什么是數據庫?
數據庫(Database)是按照數據結構來組織、存儲和管理數據的倉庫 數據庫可以分為2大種類 關系型數據庫(主流) 對象型數據庫 3)常用的數據庫有哪些? 常用關系型數據庫 PC端:Oracle、MySQL、SQL Server、Access、DB2、Sybase 嵌入式\移動客戶端:SQLite 4)如何存儲數據 數據庫是如何存儲數據的 數據庫的存儲結構和excel很像,以表(table)為單位 數據庫存儲數據的步驟 1.新建一張表(table) 2. 添加多個字段(column,列,屬性) 3.添加多行記錄(row,每行存放多個字段對應的值)
二、SQLite3能做什么?
1、SQLite3能做什么?
1、可以存儲大量的數據,且通過多線程來保證存取數據的安全,如(用新建一個事務,來解決存取錢中間出出現差錯的解決)。 2、可以做離線緩存,方便用戶在沒有網絡且上次已經瀏覽過的信息,不用再請求網絡。 3、可以做本地模糊查詢,快速實現用戶查詢的內容。
2、常用的圖形化工具是什么?
Navicat Premium圖形化軟件是mac系統上數據庫操作軟件
1)打開SQLite3數據庫
2)創建一個新的數據庫
3)圖形化創建表
4)手動寫sql的語句
三、如何使用SQLite3?
1、sql語句
使用SQL語言編寫出來的句子代碼,就是SQL語句 在程序運行過程中,要想操作(增刪改查,CRUD)數據庫中的數據,必須使用SQL語句 SQL語句的特點 不區分大小寫(比如數據庫認為user和UsEr是一樣的) 每條語句都必須以分號 ; 結尾 SQL中的常用關鍵字有 select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等 數據庫中不可以使用關鍵字來命名表、字段
1)數據定義DDL
數據定義語句(DDL:Data Definition Language) 包括create和drop等操作 在數據庫中創建新表或刪除表(create table或 drop table) 操作注意:只有 crete 和drop 后面需要加上table,然后再跟上表名,其它關鍵字后面只需要加表名。
2) 數據操作DML
數據操作語句(DML:Data Manipulation Language) 包括insert、update、delete等操作 上面的3種操作分別用於添加、修改、刪除表中的數據
3) 數據查詢DQL
數據查詢語句(DQL:Data Query Language) 可以用於查詢獲得表中的數據 關鍵字select是DQL(也是所有SQL)用得最多的操作 其他DQL常用的關鍵字有where,order by,group by和having
4)sql常用的數據類型,和注意事項
注意:字符型數據必須開單引號包起來如,‘%@’,‘The’ SQLite將數據划分為以下幾種存儲類型: integer : 整型值 real : 浮點值 text : 文本字符串 blob : 二進制數據(比如文件) 實際上SQLite是無類型的 就算聲明為integer類型,還是能存儲字符串文本(主鍵除外) 建表時聲明啥類型或者不聲明類型都可以,也就意味着創表語句可以這么寫: create table t_student(name, age); 為了保持良好的編程規范、方便程序員之間的交流,編寫建表語句的時候最好加上每個字段的具體類型
5)條件語句
如果只想更新或者刪除某些固定的記錄,那就必須在DML語句后加上一些條件 條件語句的常見格式 where 字段 = 某個值 ; // 不能用兩個 = where 字段 is 某個值 ; // is 相當於 = where 字段 != 某個值 ; where 字段 is not 某個值 ; // is not 相當於 != where 字段 > 某個值 ; where 字段1 = 某個值 and 字段2 > 某個值 ; // and相當於C語言中的 && where 字段1 = 某個值 or 字段2 = 某個值 ; // or 相當於C語言中的 || where 字段1 like 某個值 ;表示近似匹配,如 where name like '%a%' ,表示name中只要含有a就匹配 % 一般於like搭配表示包含,近似匹配,多用於模糊查詢中 示例: 將t_student表中年齡大於10 並且 姓名不等於jack的記錄,年齡都改為 5 update t_student set age = 5 where age > 10 and name != ‘jack’ ; 刪除t_student表中年齡小於等於10 或者 年齡大於30的記錄 delete from t_student where age <= 10 or age > 30 ;
6)常用sql語句
1.DDL 數據定義語句
1、創建表 create table 表名(字段名1 類型,字段名2 類型....) create table if not exists 表名 (字段名1 類型,字段名2 類型...) if not exists 表示如果不存在才創建表 示例: 注意事項創建表時必須要有一個主鍵且是integer類型的格式如下: create table if not exists t_student(id integer primary key autoincrement,name text unique,age integer,height real not null), 其中primary key表示主鍵,autoincrement 是自動增長,unique表示是唯一的,not null不能為空 2、刪除表 drop table 表名 drop table if exists 表名 ->if exists 表示存在
2.DML 數據操作語句
1.插入數據 insert into 表名(字段名1,字段名2...)values(字段名1的值,字段名的值) 示例: insert into t_student (name,age,height)values('TheYouth',22,1.70) 注意: 屬於text類型的(也就是字符型)數據必須用‘’單引號括起來 2.刪除數據 delete from t_student 表名 delete from t_student 表名 條件 示例: delete from t_class ,清空t_class所有數據 delete from t_student where name = 'TheYouth',刪除t_student表中姓名等於TheYouth的數據 3.更新數據(修改數據) update 表名 set 字段名1=字段1的值,字段名2=字段2的值 示例: update t_student set name ='TheYouth',age=29,更新t_student表中所有數據姓名都改為TheYouth,年齡都改為29 update t_student set name ='Youth' where name = 'TheYouth'
3.DQL 數據查詢語句
1、查詢語句 select * from 表名 select 字段名1,字段名2... from 表名 示例: select name ,age from t_student ,查詢t_student表中所有name和age字段 select *from t_student where height > 1.3,查詢t_student表中所有height大於1.3的 select *from t_student order by height desc,查詢t_student表並按height降序排列,desc是降序,asc是升序,排序默認的是升序 2、起別名 select 字段1 別名 , 字段2 別名 , … from 表名 別名 ; select 字段1 別名, 字段2 as 別名, … from 表名 as 別名 ; select 別名.字段1, 別名.字段2, … from 表名 別名 ; 示例 : select name myname, age myage from t_student ; 給name起個叫做myname的別名,給age起個叫做myage的別名 3、計算總數 select count (字段) from 表名 ; select count ( * ) from 表名 ; 示例 select count (name)from t_student select count(*)from t_student 4、按照某個字段的值,進行排序搜索 select * from t_student order by 字段 ; select * from t_student order by age ; 默認是按照升序排序(由小到大),也可以變為降序(由大到小) select * from t_student order by age desc ; //降序 select * from t_student order by age asc ; // 升序(默認) 也可以用多個字段進行排序 select * from t_student order by age asc, height desc ; 先按照年齡排序(升序),年齡相等就按照身高排序(降序) 5、限制加載數據條數 使用limit可以精確地控制查詢結果的數量,比如每次只查詢10條數據 格式 select * from 表名 limit 數值1, 數值2 ; 示例 select * from t_student limit 4, 8 ; 可以理解為:跳過最前面4條語句,然后取8條記錄 limit常用來做分頁查詢,比如每頁固定顯示5條數據,那么應該這樣取數據 第1頁:limit 0, 5 第2頁:limit 5, 5 第3頁:limit 10, 5 … 第n頁:limit 5*(n-1), 5 6. 模糊查詢 select * from 表名 where name like '%查詢的字符%' or phone like '%查詢的字符% 7、約束 建表時可以給特定的字段設置一些約束條件,常見的約束有 not null :規定字段的值不能為null unique :規定字段的值必須唯一 default :指定字段的默認值 (建議:盡量給字段設定嚴格的約束,以保證數據的規范性) 主鍵約束 如果t_student表中就name和age兩個字段,而且有些記錄的name和age字段的值都一樣時,那么就沒法區分這些數據,造成數據庫的記錄不唯一,這樣就不方便管理數據 良好的數據庫編程規范應該要保證每條記錄的唯一性,為此,增加了主鍵約束 也就是說,每張表都必須有一個主鍵,用來標識記錄的唯一性 什么是主鍵 主鍵(Primary Key,簡稱PK)用來唯一地標識某一條記錄 例如t_student可以增加一個id字段作為主鍵,相當於人的身份證 主鍵可以是一個字段或多個字段 主鍵設計原則 主鍵應當是對用戶沒有意義的 永遠也不要更新主鍵 主鍵不應包含動態變化的數據 主鍵應當由計算機自動生成 主鍵的聲明 在創表的時候用primary key聲明一個主鍵 create table t_student (id integer primary key, name text, age integer) ; integer類型的id作為t_student表的主鍵 主鍵字段 只要聲明為primary key,就說明是一個主鍵字段 主鍵字段默認就包含了not null 和 unique 兩個約束 如果想要讓主鍵自動增長(必須是integer類型),應該增加autoincrement create table t_student (id integer primary key autoincrement, name text, age integer) ; 利用外鍵約束可以用來建立表與表之間的聯系 外鍵的一般情況是:一張表的某個字段,引用着另一張表的主鍵字段 新建一個外鍵 外鍵名 foreign key (外鍵字段) references 關聯表名(關聯表字段) 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) references t_class (id); t_student表中有一個叫做fk_t_student_class_id_t_class_id的外鍵 這個外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段 什么是表連接查詢 需要聯合多張表才能查到想要的數據 表連接的類型 內連接:inner join 或者 join (顯示的是左右表都有完整字段值的記錄) 左外連接:left outer join (保證左表數據的完整性)
7)數據庫增、刪、改、查操作
//一.打開數據庫,創建表 // 1)創建路徑 NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject; NSString *filePath = [cache stringByAppendingPathComponent:@"students.sqlite"]; // 2)打開數據庫 if (sqlite3_open(filePath.UTF8String, &_sqlite3) == SQLITE_OK){ NSLog(@"數據庫打開成功"); }else{ NSLog(@"數據庫打開失敗"); } NSLog(@"%@",filePath); // 3)創建表 char *error = nil; NSString *sql = @"create table if not exists t_students(id integer primary key autoincrement,name text not null unique,age integer no null,height real no null);"; sqlite3_exec(_sqlite3, sql.UTF8String, NULL, NULL, &error); if (error) { NSLog(@"表創建失敗%s",error); }else{ NSLog(@"表創建成功"); //二、增 NSString *sql = @"insert into t_students(name,age,height)values('LiJ',29,1.80);"; char *error = nil; //插入sql語句 sqlite3_exec(_sqlite3, sql.UTF8String, NULL, NULL, &error); if (error) { NSLog(@"接入表數據失敗%s",error); }else{ NSLog(@"接入表數據成功"); } //注意字符串必須用‘’單引號包起來 for(NSInteger i = 0; i< 20; i++) { NSString *name = [NSString stringWithFormat:@"TheYouth%d",arc4random_uniform(100000)]; NSInteger age = [[NSString stringWithFormat:@"%ld",i+18] integerValue]; CGFloat height = [[NSString stringWithFormat:@"%.1f",0.1+i] floatValue]; NSString *insert = [NSString stringWithFormat:@"insert into t_student (name,age,height)values('%@',%ld,%f)",name,age,height]; //執行插入命令 } //三、刪 NSString *sql = @"delete from t_students where name = 'xian'"; char *error = nil; sqlite3_exec(_sqlite3, sql.UTF8String, NULL, NULL, &error); if (error) { NSLog(@"刪除表數據失敗%s",error); }else{ NSLog(@"刪除表數據成功"); } //四、改 NSString *sql = @"update t_students set name = 'TheYouth' where name = 'xingZai';"; char *error = nil; sqlite3_exec(_sqlite3, sql.UTF8String, NULL, NULL, &error); if (error) { NSLog(@"更新表數據失敗%s",error); }else{ NSLog(@"更新表數據成功"); } //五、查 NSString *sql = @"select * from t_students order by name Asc;"; //創建句柄 sqlite3_stmt *stmt; if (sqlite3_prepare_v2(_sqlite3, sql.UTF8String, -1, &stmt, NULL) == SQLITE_OK){//是否准備成功 while(sqlite3_step(stmt) == SQLITE_ROW){//執行每一行數據 int ID = sqlite3_column_int(stmt, 0); unsigned const char *name = sqlite3_column_text(stmt, 1); int age = sqlite3_column_int(stmt, 2); int height = sqlite3_column_int(stmt, 3); NSLog(@"%d--%s--%d--%d",ID,name,age,height); } } 模糊查詢 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSString *sql = [NSString stringWithFormat:@"select * from t_contact where name like '%%%@%%' or phone like '%%%@%%'",searchText,searchText]; //加載查詢命令,結果返回到加載數據的數組中 //重新刷新表格 [self.tableView reloadData]; }