在iOS中实现WAL?

时间:2014-07-03 01:30:08

标签: ios objective-c sqlite uitableview

我下面的代码有效。所以我的场景是我在课堂上有下面的代码。我使用这个类只是为了合并表和更新一个表。例如,当我调用此代码存在于app委托中的对象时,它的效果非常好!但是当我在tableview控制器中单击一个按钮后调用同一个对象时,我收到数据库锁定错误。所以这就是我想知道的。在阅读sqlite文档之后WAL:http://www.sqlite.org/wal.html我在想我无法同时读取和更新sqlite数据库,对吧?我不确定解决这个问题,所以围绕我的问题提出一个很好的建议。请记住,请记住,此代码在app委托中工作得很好,当我在单击按钮时使用操作调用它时,它在我的tableview控制器中不起作用。注意:我从SQLITE数据库中选择数据以显示表格单元格名称等。提前感谢!

  //Allocates a filemanager object. Ideally, this object is used for searching through the applications context
    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL dbErr = NO;
    //Boolean variable to tell if the database exists
    BOOL error,mainDbError;

    //Looks through all the databases. If there is a database that does not exist, the following error message will appear to the user. If all the databases exist on the system, the database opens respectively
    error = [self checkAndOpenSyncDB];
    mainDbError = [self checkAndOpenMainDB];
    if (!error&&mainDbError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                                            message:@"We didn't mean for this to happen. Looks like there was a problem loading the sync database. Contact technical support for further assistance."
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
    else{

        //This is I think to check when the last time the sync occured on the system.. not quite sure though1
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSLog(@"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(syncOpenHandle));
        char *errorMessage;
        int errorNum = 0;

        //Atataching the sync db to the master db

        NSString *attachSQL = [NSString stringWithFormat:@"ATTACH DATABASE \'%@\' AS sync_db", self->pathForSync];

        NSLog(@"PATH FOR SYNC !!!!!!! %@", pathForSync);

        NSLog(@"Here's the arratch string: %@", attachSQL);

        //
        if (sqlite3_exec(syncOpenHandle, [attachSQL UTF8String], NULL, NULL, &errorMessage) == SQLITE_OK) {

            NSString *masterQuery = [NSString stringWithFormat:@"SELECT name FROM sqlite_master WHERE type='table';"];
            const char *masterStmt = [masterQuery UTF8String];

            sqlite3_stmt *statement;

            BOOL loopErr;
            loopErr = sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL);
            if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)==SQLITE_OK) {

                while (sqlite3_step(statement) == SQLITE_ROW) {

                    NSString * currentTable = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];

                    NSLog(@"Here's the current table: %@",currentTable);

                    //This is where the magic happens. If there are any keys matching the database, it will update them. If there are no current keys in the database, the query will insert them.
                    if ([currentTable isEqualToString:@"USER_DATA"] == NO && [currentTable isEqualToString:@"USER_ACTIVITY"]== NO && [currentTable isEqualToString:@"USER_ITINERARY"] == NO) {
                        NSString *tblUpdate = [NSString stringWithFormat:@"INSERT or REPLACE INTO main.%@ SELECT * FROM sync_db.%@;",currentTable, currentTable];
                        const char *updateStmt = [tblUpdate UTF8String];

                        //sqlite3_busy_timeout (mainOpenHandle, 60000);


                        bool update;
                        update = sqlite3_exec(syncOpenHandle, updateStmt, NULL, NULL, &errorMessage)== SQLITE_OK;
                        NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
                        if (sqlite3_exec(syncOpenHandle, updateStmt, NULL, NULL, &errorMessage)== SQLITE_OK) {

                            NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
                            if (errorNum == 1) {
                                //A database reset is needded

                                //self->isResetDataBase = YES;
                            }
                            dbErr = YES;
                        }
                    }
                }
                NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
            }
            NSLog(@"Here's the error num %d", errorNum);
            NSLog(@"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(syncOpenHandle));

            NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
            sqlite3_finalize(statement);
            //Detaching the database from the mainDB
            NSString *detachSQL = [NSString stringWithFormat:@"DETACH DATABASE sync_db"]; // reference sync db
            if ((errorNum = sqlite3_exec(syncOpenHandle, [detachSQL UTF8String], NULL, NULL, &errorMessage))!= SQLITE_OK) {

                NSLog(@"Detatched syncDb Failed. ErrorMessage = %s ", errorMessage);


            }
        }


    }



    NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));

    //Closing the database when finished.

    if (syncOpenHandle!=nil) {



        sqlite3_close(syncOpenHandle);


        NSError *err;

        int success = [fileManager fileExistsAtPath:pathForSync];

        if (success) {
            [[NSFileManager defaultManager]removeItemAtPath:pathForSync error:&err];

        }
    }

0 个答案:

没有答案