Skip to content

Instantly share code, notes, and snippets.

@0xlitf
Created December 6, 2015 10:13
Show Gist options
  • Save 0xlitf/715a796c932fa365f3a2 to your computer and use it in GitHub Desktop.
Save 0xlitf/715a796c932fa365f3a2 to your computer and use it in GitHub Desktop.
fmdb
遍历一下ID和city

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"weathercity" ofType:@"sqlite"];

FMDatabase *db = [FMDatabase databaseWithPath:databasePath];

if (![db open]) {

    NSLog(@"open fail");

}else{

    NSLog(@"open success");

    FMResultSet *result = [db executeQuery:@"SELECT * FROM WeatherCity"];
    //在使用前,一定要实现next方法,无论表里面有几行
    while ([result next]) {

        NSLog(@"result %d %@", [result intForColumn:@"id"],[result stringForColumn:@"city"]);

    }

}

[db close];

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"weathercity" ofType:@"sqlite"];

FMDatabase *db = [FMDatabase databaseWithPath:databasePath];

if (![db open]) {

    NSLog(@"open fail");

}else{

    NSLog(@"open success");

    FMResultSet *result = [db executeQuery:@"SELECT * FROM WeatherCity"];
    //在使用前,一定要实现next方法,无论表里面有几行
    while ([result next]) {

        NSLog(@"result %d %@", [result intForColumn:@"id"],[result stringForColumn:@"city"]);

    }

}

[db close];

打印结果就不写了,就是遍历了一遍WeatherCity这个表然后把所有id和city打印出来

过一遍table的行数

    FMResultSet *result = [db executeQuery:@"SELECT COUNT(*) FROM WeatherCity"];

    while ([result next]) {

        NSLog(@"TOTAL %d",[result intForColumnIndex:0]);

    }

1 2 3 4 5 6 7 FMResultSet result = [db executeQuery:@"SELECT COUNT() FROM WeatherCity"];

    while ([result next]) {

        NSLog(@"TOTAL %d",[result intForColumnIndex:0]);

    }

可变参数,插入数据

    NSString *sqlStr = @"INSERT INTO WeatherCity (Id,city,cityId,province,cityPinyin,provincePinyin) values (?,?,?,?,?,?)";

    BOOL result = [db executeUpdate:sqlStr,[NSNumber numberWithInteger:9999999],@"dreamcity",[NSNumber numberWithInteger:9999999],@"dreamProvince",@"dreamCityPinyin",@"dreamProvincePinyin"];

    if (result){

        NSLog(@"insert success");

        FMResultSet *result = [db executeQuery:@"SELECT * FROM WeatherCity WHERE Id=9999999"];

        if ([result next]) {

            NSLog(@"insert done id %d city %@ cityID %d province %@ cityPinyin %@ provincePinyin %@",[result intForColumn:@"id"],[result stringForColumn:@"city"],[result intForColumn:@"cityid"],[result stringForColumn:@"province"],[result stringForColumn:@"citypinyin"],[result stringForColumn:@"provincePinyin"]);

        }

    }else {

        NSLog(@"insert fail");

    }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 NSString *sqlStr = @"INSERT INTO WeatherCity (Id,city,cityId,province,cityPinyin,provincePinyin) values (?,?,?,?,?,?)";

    BOOL result = [db executeUpdate:sqlStr,[NSNumber numberWithInteger:9999999],@"dreamcity",[NSNumber numberWithInteger:9999999],@"dreamProvince",@"dreamCityPinyin",@"dreamProvincePinyin"];

    if (result){

        NSLog(@"insert success");

        FMResultSet *result = [db executeQuery:@"SELECT * FROM WeatherCity WHERE Id=9999999"];

        if ([result next]) {

            NSLog(@"insert done id %d city %@ cityID %d province %@ cityPinyin %@ provincePinyin %@",[result intForColumn:@"id"],[result stringForColumn:@"city"],[result intForColumn:@"cityid"],[result stringForColumn:@"province"],[result stringForColumn:@"citypinyin"],[result stringForColumn:@"provincePinyin"]);

        }

    }else {

        NSLog(@"insert fail");

    }

上面代码插入了一条数据,然后又打印了一下看插入成功没有

FMDatabaseQueue

在不同的线程如果操作一个数据库会很危险,所以要用到线程安全的FMDatabaseQueue,举个例子

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"weathercity" ofType:@"sqlite"];

FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:databasePath];

[queue inDatabase:^(FMDatabase *db){

    NSString *sqlStr = @"INSERT INTO WeatherCity (Id,city,cityId,province,cityPinyin,provincePinyin) values (?,?,?,?,?,?)";

    [db executeUpdate:sqlStr,[NSNumber numberWithInteger:7777777],@"dreamcity",[NSNumber numberWithInteger:7777777],@"dreamProvince",@"dreamCityPinyin",@"dreamProvincePinyin"];

    [db executeUpdate:sqlStr,[NSNumber numberWithInteger:8888888],@"dreamcity",[NSNumber numberWithInteger:8888888],@"dreamProvince",@"dreamCityPinyin",@"dreamProvincePinyin"];

    FMResultSet *result = [db executeQuery:@"select * from weathercity"];

    while ([result next]) {

        NSLog(@"result %d %@", [result intForColumn:@"id"],[result stringForColumn:@"city"]);

    }

}];

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"weathercity" ofType:@"sqlite"];

FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:databasePath];

[queue inDatabase:^(FMDatabase *db){

    NSString *sqlStr = @"INSERT INTO WeatherCity (Id,city,cityId,province,cityPinyin,provincePinyin) values (?,?,?,?,?,?)";

    [db executeUpdate:sqlStr,[NSNumber numberWithInteger:7777777],@"dreamcity",[NSNumber numberWithInteger:7777777],@"dreamProvince",@"dreamCityPinyin",@"dreamProvincePinyin"];

    [db executeUpdate:sqlStr,[NSNumber numberWithInteger:8888888],@"dreamcity",[NSNumber numberWithInteger:8888888],@"dreamProvince",@"dreamCityPinyin",@"dreamProvincePinyin"];

    FMResultSet *result = [db executeQuery:@"select * from weathercity"];

    while ([result next]) {

        NSLog(@"result %d %@", [result intForColumn:@"id"],[result stringForColumn:@"city"]);

    }

}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment