前言:
上回我們解決了遇到的第一個問題,心里有點(diǎn)小竊喜。但是,別高興的太早,未來還有更多的問題等待著我們呢。一起努力吧!
此時,我們開始使用FMDB來管理數(shù)據(jù)庫。
首先,為大家推薦一款可視化sqlite數(shù)據(jù)庫管理工具:SQLite Manager,我認(rèn)為挺好用的,畢竟我們只是測試用,只要能夠看見數(shù)據(jù)的增刪就OK了。
注:mac終端有一個專門的sqlite3數(shù)據(jù)庫,進(jìn)入終端->sqlite3 test.db,直接進(jìn)入,但是對我而言,不太好用,不直觀,個人觀點(diǎn)。
1.用上面的工具先創(chuàng)建一個簡單的數(shù)據(jù)庫
2.將數(shù)據(jù)庫和我們的應(yīng)用連接起來,將數(shù)據(jù)庫導(dǎo)入工程里面
在工程里面獲取該數(shù)據(jù)庫的時候,我們會使用如下代碼:
//獲取工程里面的數(shù)據(jù)庫路徑
NSString *studentDBPath = [[NSBundle mainBundle] pathForResource:@"studentSqlite" ofType:@"db"];
但是,有時你會發(fā)現(xiàn)根本找不到下面的文件,獲取的 studentDBPath 為空。明明工程里面有數(shù)據(jù)庫文件,代碼卻找不到,很是奇怪???
解決的辦法是:
照上圖,在Build Phases里面,使用上面的方法找到的所有文件只能是在Copy Bundle Resources里面包含的文件,否則,查找不到。
CopyBundle Resources 主要是一些資源文件會被打包的時候會被放入app中
3. 替換數(shù)據(jù)庫
3.1 創(chuàng)建空數(shù)據(jù)庫
剛剛導(dǎo)入的數(shù)據(jù)庫只是在工程里面,我們編譯運(yùn)行后,手機(jī)軟件里面的文件并沒有導(dǎo)入的數(shù)據(jù)庫,所以需要先創(chuàng)建一個空的數(shù)據(jù)庫到軟件Document
//獲取文檔路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//創(chuàng)建一個空文件(數(shù)據(jù)庫)
NSString *fileName = [path stringByAppendingPathComponent:@"test.db"]
上面創(chuàng)建了一個空的數(shù)據(jù)庫,如果接下來我們就直接打開數(shù)據(jù)
//創(chuàng)建數(shù)據(jù)庫實(shí)例
FMDatabase *database = [[FMDatabase alloc]initWithPath:fileName];
if ([database open]) {
NSLog(@"打開數(shù)據(jù)庫成功");
}
你會看到下面的錯誤消息,提示你沒有找到某某表, table不存在
2016-12-30 21:58:47.460 1_數(shù)據(jù)庫fmdb簡單測試[839:53506] 打開數(shù)據(jù)庫成功
2016-12-30 21:58:47.462 1_數(shù)據(jù)庫fmdb簡單測試[839:53506] DB Error: 1 "no such table: student"
2016-12-30 21:58:47.462 1_數(shù)據(jù)庫fmdb簡單測試[839:53506] DB Query: select * from student;
想著也是,我們創(chuàng)建的是空數(shù)據(jù)庫,還沒有創(chuàng)建表,所以會報這個錯誤
3.2 替換空數(shù)據(jù)庫
上面我們已經(jīng)在軟件內(nèi)部(NSDocumentDirectory)創(chuàng)建了一個新的空數(shù)據(jù)庫,然后把我們工程里面的數(shù)據(jù)庫替換掉這個空數(shù)據(jù)庫
//1.先在Document里面查找看有沒有對應(yīng)的數(shù)據(jù)庫,有的話就不用替換了
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [searchPaths objectAtIndex:0];
NSString *dbPath = [docPath stringByAppendingPathComponent:@"studentDB.db"];
NSLog(@"%@",dbPath);
//2.使用NSFileManager才能直接管理應(yīng)用里面的文件,進(jìn)行替換或者刪除
NSFileManager *fileManager = [[NSFileManager alloc]init];
BOOL isExit = [fileManager fileExistsAtPath:dbPath];
//如果不存在,將工程里面數(shù)據(jù)庫的復(fù)制到Document里面
if (!isExit) {
NSLog(@"原來不存在數(shù)據(jù)庫");s
//獲取工程里面的數(shù)據(jù)庫路徑,如果獲取不到路徑,往上翻翻,有解決辦法
NSString *studentDBPath = [[NSBundle mainBundle] pathForResource:@"studentSqlite" ofType:@"db"];
NSLog(@"%@",studentDBPath);
//替換數(shù)據(jù)庫
BOOL success = [fileManager copyItemAtPath:studentDBPath toPath:dbPath error:nil];
if (success) {
NSLog(@"數(shù)據(jù)庫替換成功");
}
到目前為止,存在應(yīng)用里面的就是我們自己創(chuàng)建的有數(shù)據(jù)的數(shù)據(jù)庫了。
4.FMDB數(shù)據(jù)庫查詢、增加、刪除
4.0 打開數(shù)據(jù)庫
//1.獲得數(shù)據(jù)庫文件的路徑
FMDatabase *database = [[FMDatabase alloc]initWithPath:dbPath];
if ([database open]) {
NSLog(@"打開數(shù)據(jù)庫成功");
}
4.1 查詢
//使用SQL語句,從表中獲取一個集合
FMResultSet *resultSet = [database executeQuery:@"select * from student;"];
//[resultSet next],會一條一條的遍歷,直到為空值結(jié)束循環(huán)
while ([resultSet next]) {
Student *stu = [[Student alloc]init];
stu.number = [resultSet intForColumn:@"number"];
stu.name = [resultSet objectForColumnName:@"name"];
stu.age = [resultSet intForColumn:@"age"];
sssss
/**一開始,數(shù)組students沒有數(shù)據(jù),而且也添加不進(jìn)去數(shù)據(jù),是因?yàn)椋簺]有初始化,即沒有內(nèi)存空間放數(shù)據(jù)*/
[self.students addObject:stu];
}
4.2 增加
對于增加,我是直接設(shè)置一個按鈕,點(diǎn)擊一下就往數(shù)據(jù)庫里面填寫一條數(shù)據(jù),使用SQLite Manager,可以方便看到增加進(jìn)了數(shù)據(jù)庫,主動調(diào)用SQLiteManager,非常方便
數(shù)據(jù)庫除了查詢,其他都是更新
#pragma mark - 添加數(shù)據(jù)
- (IBAction)updateData:(id)sender {
NSString *name = @"新添加的";sw
NSInteger age = arc4random();
BOOL success = [database executeUpdate:@"insert into student (name,age) values(?,?)",name,@(age)];
if (!success) {
NSLog(@"%@",[database lastErrorMessage]);
}
}
4.3 刪除數(shù)據(jù)
#pragma mark - 刪除數(shù)據(jù)
- (IBAction)deleteData:(id)sender {
//delete from student where number = 1 ,這是SQL語句,有空的朋友可以去了解SQL語句
NSString *deleteData = [NSString stringWithFormat:@"delete from student where number = 1 "];
//開始一直沒有刪除成功, 原因是 把數(shù)據(jù)庫的 名字寫成了表的名字了
BOOL success = [self.db executeUpdate:deleteData];
if (success) {
NSLog(@"刪除成功");
}
}
5. 總結(jié)
到此為止,一般簡單的FMDB操作都已經(jīng)寫完了,還有多線程操作,目前還沒有學(xué)習(xí)到,到學(xué)習(xí)了以后再次添加,還要抽個時間大概了解一下SQL語言。
由于這是第一次在簡書上寫文章,有什么不妥的地方,歡迎評論,一起探討交流。
2017年到了,祝大家元旦快樂。。。
github示例庫:https://github.com/Brucelin2015/FMDBSimpleOperation