要使用FMDatabase进行数据库的操作,我们先得导入FMDatabase这个封装好的包(下载地址:https://github.com/ccgus/fmdb)
然后在要执行数据库操作的文件中引入 头文件:#import "FMDatabase.h"
再在 Frameworks中导入libsqlite3.0.dylib
准备工作就绪,话不多说看代码:①
因为数据库的创建是在应用程序的沙盒路径下,所以先得获得应用程序的沙盒路径
//应用程序的沙盒路径
- (NSString *) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *document = [path objectAtIndex:0];
NSLog(@"%@ .............. ",document);
return [documentstringByAppendingPathComponent:@"StudentData.sqlite"];
}
②
创建数据库,以及一个studentList表
- (void)createTable
{
NSFileManager *fileManager = [NSFileManagerdefaultManager];
FMDatabase *db = [FMDatabasedatabaseWithPath:[selfdataFilePath]];
if (![fileManager fileExistsAtPath:[self dataFilePath]]) {
//"还未创建数据库,现在正在创建数据库"
if ([db open]) {
//数据库创建完成,创建studentList表(包括 id,age,name,address这几个属性)
NSString *sqlCreateTable = [NSStringstringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' TEXT, '%@' TEXT)",@"studentList",@"id",@"age",@"name",@"address"];
BOOL res = [db executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close];
}
}
NSLog(@"FMDatabase:---------%@",db);
}
③
//对studentList进行“增”操作
-(void)AddStudent:(NSString *)name age:(NSString *)age address:(NSString *)address
{
FMDatabase *db = [FMDatabasedatabaseWithPath:[selfdataFilePath]];
if ([db open]) {
NSString *insertSql1= [NSStringstringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
@"studentList",@"age", @"name",@"address", age, name, address];
BOOL res = [db executeUpdate:insertSql1];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
}
}
④
//对studentList进行“改”操作
-(void)updateFromStudentList:(NSString *)oldName newName:(NSString *)newName
{
FMDatabase *db = [FMDatabasedatabaseWithPath:[selfdataFilePath]];
if ([db open]) {
NSString *updateSql = [NSStringstringWithFormat:
@"update %@ set %@ = '%@' where %@ = '%@'",
@"studentList", @"address", newName ,@"name", oldName];
BOOL res = [db executeUpdate:updateSql];
if (!res) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
[db close];
}
}
⑤
//对studentList进行“删”操作
-(void)deleteStudent:(NSString *)name
{
FMDatabase *db = [FMDatabasedatabaseWithPath:[selfdataFilePath]];
if ([db open]) {
NSString *deleteSql = [NSStringstringWithFormat:
@"delete from %@ where %@ = '%@'",
@"studentList",@"name", name];
BOOL res = [db executeUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
}
⑥
//对studentList进行“查”操作
-(NSMutableArray *)selectStudents
{
NSMutableArray * array = [[NSMutableArrayalloc] init];
FMDatabase *db = [FMDatabasedatabaseWithPath:[selfdataFilePath]];
if ([db open]) {
NSString * sql = [NSStringstringWithFormat:
@"SELECT * FROM %@ order by age desc",@"studentList"];
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
NSString * name = [rs stringForColumn:@"name"];
NSString * age = [rs stringForColumn:@"age"];
NSString * address = [rs stringForColumn:@"address"];
NSDictionary * dic = [[NSDictionaryalloc] initWithObjectsAndKeys:name,@"name",age,@"age",address,@"address",nil];
[array addObject:dic];
}
[db close];
}
return array;
}