Updating SQLite cell from ios

2019-09-19 18:59发布

问题:

I am reading information from an sqlite file, I am able to read from the database and create an array with objects from the database. I need to update entries on the database when something happens in my code. For example a question has a column called 'complete' which is set as no, when answered I want to change the value to yes. How do I open a database and edit a cell on xcode?

回答1:

Firstly no one answer you from the scratch . First You need to do something and ask here if you stuck somewhere .Anyway here are some tutorial you can create , update ,read and delete the sqlite table :

http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app

and

http://www.tutorialspoint.com/ios/ios_sqlite_database.htm



回答2:

     NSString *status;


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath=[[NSString alloc]initWithString:[documentsDir stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"]];

NSLog(@"Database Path %@",dbPath);

sqlite3_stmt    *statement;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    NSLog(@"open");

    NSString *updateSQL = [NSString stringWithFormat: @"UPDATE Table_Name set complete = 'YES' where id = 1994"];

    const char *update_stmt = [updateSQL UTF8String];
    sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        status=@" Updated";
    }

    else
    {
        status=@"Error occured";
    }