Unable to update database

前端 未结 2 1882
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 03:03

I\'m having trouble updating my database. I believe it\'s due to it possibly being read only, however I am unsure. I created my database in my console, and the code below adds t

相关标签:
2条回答
  • 2021-01-28 03:33
    -(BOOL) someUpdateFunction {
        BOOL updatedSuccessfully = NO;
    
        // Open the database.
        if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
            NSString *updateQuery = [[NSString alloc] initWithFormat:@"UPDATE people SET user = 'terror' WHERE user = 'greg'"];
            const char *query = [updateQuery UTF8String];
            [updateQuery release];
    
            sqlite3_stmt *statement; 
            int res = (sqlite3_prepare_v2( database, query, -1, &statement, NULL));
    
            if(res == SQLITE_OK) {
                int result = sqlite3_step(statement);
                if(result == SQLITE_DONE) {
                    updatedSuccessfully = YES;
                    sqlite3_finalize(statement);
                }else {
                    NSLog(@"Failed with error code: %d", result);
                }
            }else {
                NSLog(@"Failed to prepare the update query with error code: %d", res);
            }
        }else {
            NSLog(@"Failed to open the database");
        }
    
        sqlite3_close(database);
        //NSLog(updatedSuccessfully);
        //NSLog(@"The value of the bool is %@\n", updatedSuccessfully);
        return updatedSuccessfully;
    }
    

    I was supplied this in the chat room. Thanks lads.

    Remember to copy the DB over to the device else. You can find the DB path with this code:

    - (NSString *) dataFilePath:(NSString *) dbName { 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:dbName];
        return dbPath;
    }
    
    -(BOOL)buildtheDB{
        BOOL updatedSuccessfully = NO;
    
        NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0]; 
        NSString *sqlFile =[documentDirectory stringByAppendingPathComponent:kDatabaseFileName];
    
        if ([[NSFileManager defaultManager] fileExistsAtPath:sqlFile] == NO) {
            NSString *bundleSql = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"sqlite"];
    
            NSError *error = nil;
    
            BOOL databaseCopied = [[NSFileManager defaultManager] copyItemAtPath:bundleSql toPath:sqlFile error:&error];
            if (databaseCopied == NO) {
                if (error) {
                    NSLog(@"Could not copy database file with error: %@", [error localizedDescription]);
                }
            }else {
                NSLog(@"Database copied successfully");
            }
        }
        else{
            NSLog(@"Database already copied");
        }
    
        return updatedSuccessfully;
    }
    
    0 讨论(0)
  • 2021-01-28 03:35

    You are forgeting some code for update statement

    //the binding part is missing in your code , you need to write after Prepare statement.
    
    sqlite3_bind_text(compiledStatement, 1, [string UTF8String], -1, SQLITE_TRANSIENT);
    
    if(SQLITE_DONE != sqlite3_step(compiledStatement))
    NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));
    
    sqlite3_reset(compiledStatement);
    

    Via iphonesdkarticles.com.

    This will resolve your error.

    0 讨论(0)
提交回复
热议问题