iOS 新建本地数据库FMDB

114 阅读1分钟

1.在appdelegate.m中添加

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    myDelegate.dbPath = [self dataFilePath];

    [self createTable];

    return YES;

}

  • (NSString *) dataFilePath//应用程序的沙盒路径

{

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *document = [path objectAtIndex:0];

    return [document stringByAppendingPathComponent:@"NameData.sqlite"];

}

  • (void)createTable

{

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];

    if (![fileManager fileExistsAtPath:myDelegate.dbPath]) {

        NSLog(@"还未创建数据库,现在正在创建数据库");

        if ([db open]) {

             

    [db executeUpdate:@"create table if not exists StudentList (name text, address text, id text)"];             

            [db close];

        }else{

            NSLog(@"database open error");

        }

    }

    NSLog(@"FMDatabase:---------%@",db);

}

2.新增插入,更新,查询,删除操作

-(void)btnClick{

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];

    [db open];

    NSString * name =@"梨子";

    NSString *address = @"砀山";

    int i = 1;

    NSString *id = [NSString stringWithFormat:@"%d",i];

    BOOL  res = [db executeUpdate:@"INSERT INTO StudentList (name, address, id) VALUES (?, ?, ?)", name, address, id];

    if (res == NO) {

        NSLog(@"数据插入失败");

        }else{

         NSLog(@"数据插入成功");

        }

    [db close];

}

-(void)btn1Click{

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];

    [db open];

    int i = 1;

    NSString *id = [NSString stringWithFormat:@"%d",i];

    BOOL  res = [db executeUpdate:@"UPDATE StudentList SET name = ?, address = ? WHERE id = ?",@"葡萄",@"新疆",id];

    if (res == NO) {

        NSLog(@"数据修改失败");

        }else{

         NSLog(@"数据修改成功");

        }

    [db close];

}

-(void)btn2Click{

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];

    [db open];

    int i = 1;

    NSString *id = [NSString stringWithFormat:@"%d",i];

    NSString *nameOut = [db stringForQuery:@"SELECT name FROM StudentList WHERE id = ?",id];

    NSString *addressOut=  [db stringForQuery:@"SELECT address FROM StudentList WHERE id = ?",id];

    NSLog(@"查询到的名字===%@----地址===%@",nameOut,addressOut);

    [db close];

}

-(void)btn3Click{

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];

    [db open];

    int i = 1;

    NSString *id = [NSString stringWithFormat:@"%d",i];

    BOOL  res = [db executeUpdate:@"DELETE FROM StudentList WHERE id = ?",id];

    if (res == NO) {

        NSLog(@"删除数据失败");

        }else{

         NSLog(@"删除数据成功");

        }

    [db close];

}