2012年7月23日星期一

Little Trick for Multiple Sql Commands by FMDB

Just consumed an hour for debugging a "fault" caused by putting a NSString contain multiple sql commands into a FMDB executeUpdate: function.

A question on StackOverFlow stated that FMDB doesn't support multiple commands in a single executeUpdate: call and I testified it by reading the code of FMDB.


Here is a little trick to do so:


    FMDatabase *db_ = [[FMDatabase alloc] initWithPath:yourPath];

    [db_ open];

    [db_ beginTransaction];
    
    NSArray *sqlCommands = [DATABASE_TEMPLATE componentsSeparatedByString:@";"];
    for (NSString *sql in sqlCommands)
    {
        [db_ executeUpdate:sql];
    }
    
    return [db_ commit];


It is particularly useful when creating many tables, and the string may come from some data files.

沒有留言:

發佈留言