了解數(shù)據(jù)庫
https://github.com/JikerSun/-FMDB-.git
存儲數(shù)據(jù)的倉儲,數(shù)據(jù)的庫表,一條條數(shù)據(jù)(數(shù)據(jù)模型)key-value
具有唯一的key? 主鍵(primary key),? 鍵test
庫表? student? ? 姓名? 學(xué)號? 年齡? 性別score
創(chuàng)建庫表、刪除庫表、插入數(shù)據(jù)、刪除數(shù)據(jù)、修改數(shù)據(jù)、查找數(shù)據(jù)
常用的數(shù)據(jù)庫:SQL Server、Oracle、MySQL、DB2
我們要學(xué)習(xí)的數(shù)據(jù)庫:SQLite3
輕量化、嵌入式設(shè)備
數(shù)據(jù)庫的基本操作語句
SQL? ? (Structured Query Language)數(shù)據(jù)庫操作語言
DB? (DataBase)
GUI工具? MesaSQLite
鍵? 字段? 屬性
使用GUI工具創(chuàng)建數(shù)據(jù)庫? .rdb
iOS 創(chuàng)建數(shù)據(jù)庫? .db
SQLite? 常用的數(shù)據(jù)類型:integer? float? varchar(容量)可自定調(diào)整大小? ? text(文本)? Boolean? ? blob (二進(jìn)制)
primary key? 主鍵? ? autoincrement? 自增長
SQLite? 常用的數(shù)據(jù)類型:interger? float? varchar(容量)可以自動調(diào)整大小(text (一般存儲文本))? ? Boolean(BOOL)? blob(二進(jìn)制)
建表的語句:1
表名? ? ? ? ? ? 主鍵 類型? ? ? 說明是主鍵? ,名字類型(預(yù)留多少個(gè)),? 年齡 類型,? 性別 類型
create table student(number integer primary key,name varchar(20),age integer,gendar integer)
主鍵自增長
create table score(number integer primary key autoincrement,name varchar(20),age integer,gendar integer)
語句2:如果表存在就不創(chuàng)建 如果表不存在就創(chuàng)建
create table if not exists student(number integer primary key autoincrement,name varchar(20),age integer,gendar integer
刪表語句
drop table score
插入語句
insert into 表名(字段1,字段2) values (字段值1,字段值2)
insert into student(number,name,age,gendar) values(1,'趙四',18,1)
查找語句
全部查找? ? ? ? ? 表名
select * from student
根據(jù)學(xué)號查找
表名? ? ? ? ? ? ? ? ? ? 表達(dá)式(精確查找用”=“,模糊查找用like)
select * from student where number=1
(更新)修改數(shù)據(jù)語句
表名? ? ? ? ? ? 要改的字段? ? ? ? ? 主鍵的值
update student set age=65 where number=1
刪除數(shù)據(jù)語句
全部刪除表單
表名
delete from student
單個(gè)元素的刪除
表名? ? ? ? ? ? ? ? ? ? ? 主鍵值
delete from student where number=1
FMDB(開源的第三方庫)
1. 非ARC模式的第三方庫 -fno-objc-arc
2.系統(tǒng)依賴庫? libsqlite3.dylib:
添加依賴庫的方法: target—>Bulid Phases——>按添加”+”——>打入libsqlite3.dulib——>按enter確定
FMDatabase? 是一個(gè)FMDB的數(shù)據(jù)庫的對象
FMStatement 查詢到的數(shù)據(jù)集合
next? FMStatement中的方法? 指向下一個(gè)元素 ( 首次使用不指向任何人元素)
使用事務(wù)? ? 事務(wù) ——> 原子性(不可分割性)? 隱式的操作 (高效)
開始事務(wù)[database beginTransaction];
提交事務(wù) [database commit];
例子:
用程序進(jìn)行數(shù)據(jù)庫數(shù)據(jù)的添加刪除修改等;
用事務(wù)(未顯示事務(wù),顯示事務(wù))堆數(shù)據(jù)庫數(shù)據(jù)進(jìn)行批量的添加
#import "ViewController.h"
//導(dǎo)入FMDB頭文件
#import "FMDatabase.h"
// 導(dǎo)入模型類
#import "StudentModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark 一.? 指定數(shù)據(jù)庫文件的路徑
//? ? 1.如果路徑存在? 就直接讀取數(shù)據(jù)庫文件
//? ? 2.如果路徑不存在? 就創(chuàng)建響應(yīng)路徑的數(shù)據(jù)庫文件,同時(shí)讀取文件
//? ? 取到沙盒的路徑? 然后拼接到想存入的文件夾中
//? ? Documents :一般把應(yīng)用程序的數(shù)據(jù)存儲到此文件夾下,定期向云端備份
NSString *path=[NSString stringWithFormat:@"%@/Documents/class.db",NSHomeDirectory()];
NSLog(@"%@",path);
#pragma? mark? ? 二.普通的FMDB第三方庫的使用
//? 2.1? 創(chuàng)建FMDataBase對象
//? ? databaseWithPath 生成FMDataBase對象的同時(shí)傳入數(shù)據(jù)庫文件的路徑(可能有該文件,可能沒有..)
FMDatabase *database=[FMDatabase databaseWithPath:path];
//? ? 2.2? 打開數(shù)據(jù)庫
//? ? 如果存在數(shù)據(jù)庫的文件? 直接讀取文件? ? 如果不存在數(shù)據(jù)庫文件? 創(chuàng)建并讀取數(shù)據(jù)庫文件
[database open];
//? 2.3? 建表
//? ? student 學(xué)生信息表
//? 2.3. 1.定義一個(gè)字符串 寫一個(gè)創(chuàng)建表的語句? 如果表不存在就創(chuàng)建 如果存在就不創(chuàng)建了
NSString * creatSQL=@"create table if not exists student(number integer primary key autoincrement,name varchar(20),age integer,gendar integer)";
//? 2.3. 2.執(zhí)行建表的語句? executeUpdate? 該方法執(zhí)行語句
BOOL? createSuccess=[database executeUpdate:creatSQL];
//? ? 如果是1 說明創(chuàng)建成功? 如果是0 就說明創(chuàng)建失敗 打印最后的錯誤信息
NSLog(@"創(chuàng)建表:%d? %@",createSuccess,database.lastErrorMessage);
/*
#pragma? mark? 創(chuàng)建一個(gè)模型
StudentModel *stu=[[StudentModel alloc]init];
stu.number=@"2";
stu.name=@"張三";
stu.age=@"25";
stu.gendar=@"1";
//2.4? 插入數(shù)據(jù) (步驟同建表)
//? ? NSString *inserSQL=@"insert into student(number,name,age,gendar) values(1,'李四',36,1)";
//? ? 如果是插入數(shù)據(jù)模型 需要使用 ? (問號) 占位(?相當(dāng)于格式控制符)? 代表oc的基本對象相當(dāng)于%@
NSString *inserSQL=@"insert into student(number,name,age,gendar) values(?,?,?,?)";
//? ? 插入數(shù)據(jù)
//? ? BOOL insertSucess=[database executeUpdate:inserSQL];
//? ? 插入數(shù)據(jù)模型的變量的插入數(shù)據(jù)方法
BOOL insertSucess=[database executeUpdate:inserSQL,stu.number,stu.name,stu.age,stu.gendar];
NSLog(@"插入語句是:%d? ? %@",insertSucess,database.lastErrorMessage);
//? ? 2,5 刪除數(shù)據(jù)
NSString *deleteSQL=@"delete from student where number=2";
//? ? 執(zhí)行刪除語句
BOOL deleteSuccess=[database executeUpdate:deleteSQL];
NSLog(@"刪除數(shù)據(jù):%d? %@",deleteSuccess,database.lastErrorMessage);
//? ? 2.6 更新語句
NSString *updataSQL=@"update student set name='caoyu' where number=1";
BOOL updataSuccess=[database executeUpdate:updataSQL];
NSLog(@"更新(修改):%d? %@",updataSuccess,database.lastErrorMessage);
//? ? 2.7 查詢
NSString *selectSQL=@"select * from student";
//? ? executeQuery? 用此方法運(yùn)行查詢語句 返回值是FMResultSet類型
FMResultSet *selectSet=[database executeQuery:selectSQL];
NSLog(@"%@",selectSet);
//? ? 指向下一個(gè)元素? 方法next返回值是bool? yes是有值? no是無值
[selectSet next];
while ([selectSet next])
{
//? ? 根據(jù)字段的名字返回字段的值(取出的類型是 nsstring)
//? ? ? ? NSString *name=[selectSet stringForColumn:@"name"];
//? //? ? 根據(jù)字段的名字返回字段的值(取出的類型是? int)? 索引從0開始
int age =[selectSet intForColumnIndex:0];
NSLog(@"%d",age);
}*/
/**********************使用事務(wù)進(jìn)行數(shù)據(jù)庫操作*****************/
/****************************未顯示的使用事務(wù)*******************/
NSDate *date=[NSDate date];
//? ? 未顯示的使用事務(wù)
for(int i=0;i<500;i++)
{
BOOL bo= [database executeUpdate:[NSString stringWithFormat:@"insert into student (name,age,gendar) values(%d,18,1)",i]];
NSLog(@"%d",bo);
}
//? ? timeIntervalSinceDate? 從某個(gè)時(shí)間到某個(gè)時(shí)間的時(shí)間間隔? 單位為秒
NSLog(@"未顯示的使用事務(wù):%f",[[NSDate date] timeIntervalSinceDate:date]);
/*****************顯示的使用事務(wù)******************/
NSDate *date2=[NSDate date];
//? ? 顯示的使用事務(wù)? 所有的操作提交一次
//? ? 開始使用事務(wù)
//? ? beginDeferredTransaction
[database? beginDeferredTransaction];
for (int i=0; i<500; i++){
BOOL bo= [database executeUpdate:[NSString stringWithFormat:@"insert into student (name,age,gendar) values(%d,18,1)",i]];
NSLog(@"%d",bo);
}
//? ? 提交事務(wù)
BOOL commitSuccess= [database commit];
NSLog(@"顯示的使用事務(wù):%f",[[NSDate date] timeIntervalSinceDate:date2]);
NSLog(@"提交事務(wù):%d",commitSuccess);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}@end