熟悉基本語(yǔ)法別看這篇文章了
我這里就是單純記錄下使用泽本,備忘
1、導(dǎo)入包
2啊终、github或者pod框架:FMDB
3、在相應(yīng)界面傲须,導(dǎo)入頭文件開(kāi)始使用
4蓝牲、創(chuàng)建表
// 1.獲得數(shù)據(jù)庫(kù)文件的路徑
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接文件名
NSString *filename = [doc stringByAppendingPathComponent:@"wangyi.sqlite"];
// 3.得到數(shù)據(jù)庫(kù)
FMDatabase *db = [FMDatabase databaseWithPath:filename];
NSLog(@"filename === %@",filename);
// 4.打開(kāi)數(shù)據(jù)庫(kù)
if ([db open]) {
// 5.創(chuàng)表
BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS test_tab (id integer PRIMARY KEY AUTOINCREMENT, name Text NOT NULL, age Text NOT NULL);"];
if (result) {
NSLog(@"成功創(chuàng)表");
} else {
NSLog(@"創(chuàng)表失敗");
}
}else{
NSLog(@"打開(kāi)表失敗");
}
5、插入數(shù)據(jù)
// 1.獲得數(shù)據(jù)庫(kù)文件的路徑
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接文件名
NSString *filename = [doc stringByAppendingPathComponent:@"wangyi.sqlite"];
// 3.得到數(shù)據(jù)庫(kù)
FMDatabase *db = [FMDatabase databaseWithPath:filename];
NSLog(@"filename === %@",filename);
// 4.打開(kāi)數(shù)據(jù)庫(kù)
if ([db open]) {
// 2.執(zhí)行更新
for (int i = 0; i<50; i++) {
[db executeUpdate:@"insert into test_tab (name,age) values(?,?)",@"jack",@"21"];
}
NSLog(@"插入完成");
}else{
NSLog(@"打開(kāi)表失敗");
}
6泰讽、查詢(xún)數(shù)據(jù)表例衍,顯示在tableView
[self.dataArray removeAllObjects];
// 1.獲得數(shù)據(jù)庫(kù)文件的路徑
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接文件名
NSString *filename = [doc stringByAppendingPathComponent:@"wangyi.sqlite"];
// 3.得到數(shù)據(jù)庫(kù)
FMDatabase *db = [FMDatabase databaseWithPath:filename];
NSLog(@"filename === %@",filename);
// 4.打開(kāi)數(shù)據(jù)庫(kù)
if ([db open]) {
NSLog(@"打開(kāi)表成功");
// 2.執(zhí)行更新
FMResultSet *data = [db executeQuery:@"SELECT * FROM WYWork_tab ORDER BY id DESC"];
NSMutableArray *tempArray = [NSMutableArray array];
while ([data next]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 每條記錄的檢索值
NSString *name = [data stringForColumn:@"name"];
NSString *time = [data stringForColumn:@"time"];
NSInteger ID = [data intForColumn:@"id"];
[dict setValue:name forKey:@"name"];
[dict setValue:time forKey:@"time"];
[dict setValue:@(ID) forKey:@"id"];
[tempArray addObject:dict];
}
NSLog(@"tempArray == %@",tempArray);
self.dataArray = [WYShowModel mj_objectArrayWithKeyValuesArray:tempArray];
}else{
NSLog(@"打開(kāi)表失敗");
}
//插入數(shù)據(jù)后昔期,關(guān)閉數(shù)據(jù)庫(kù)
[db close];
[self.tableView reloadData];
7、刪除數(shù)據(jù)庫(kù)條目
// 1.獲得數(shù)據(jù)庫(kù)文件的路徑
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接文件名
NSString *filename = [doc stringByAppendingPathComponent:@"wangyi.sqlite"];
// 3.得到數(shù)據(jù)庫(kù)
FMDatabase *db = [FMDatabase databaseWithPath:filename];
NSLog(@"filename === %@",filename);
// 4.打開(kāi)數(shù)據(jù)庫(kù)
if ([db open]) {
NSLog(@"打開(kāi)表成功");
//刪除某條數(shù)據(jù)
NSString *deleteSql = [NSString stringWithFormat:@"delete from WYWork_tab where id = %ld",(long)index];
[db executeUpdate:deleteSql];
}else{
NSLog(@"打開(kāi)表失敗");
}
//插入數(shù)據(jù)后佛玄,關(guān)閉數(shù)據(jù)庫(kù)
[db close];