Question

I manage SQLite data by FMDB.

In this App, I'd like to put the data in advance, so I made the dbfile sample.db in advance by terminal and Lita, copied it and imported into the project.

Problem is that you cannot update the db.
Even though called saveData, Column title is Empty.
How do I need to fix to update the db?

・In sample.db, there is a table events.
・The table events schema is (id INTEGER PRIMARY KEY AUTOINCREMENT, day TEXT, title TEXT, time INTEGER);
・You add the data Event to the table events.

Now, you call the method saveData and put "test" into events.

Event.h

@interface Event : NSObject

@property (nonatomic, assign) NSInteger eventId;
@property (nonatomic,   copy) NSString* day;
@property (nonatomic,   copy) NSString* title;
@property (nonatomic, assign) NSInteger time;

DetailViewController.m

- (void)saveData:(id)sender
{
    Event* newEvent = [[Event alloc]init];
    newEvent.title = @“test";

    [self.delegate editEventDidFinish:self.event newEvent:newEvent];

@end

MasterViewController.m

- (void)viewDidLoad
{
     [super viewDidLoad];   
     self.daoEvents = [[DaoEvents alloc] init];
}

- (void)editEventDidFinish:(Event *)oldEvent newEvent:(Event *)newEvent
{
     [self.daoEvents update:newEvent];               
     [self.navigationController popViewControllerAnimated:YES];
}

- (void)removeOldEnent:(Event*)oldEvent
{
     [self.daoEvents remove:oldEvent.eventId];
}

DaoEvents.m

    #define DB_FILE_NAME @“sample.db"
    #define SQL_UPDATE @"UPDATE events SET day = ?, title = ?, time = ? WHERE id = ?;”
    #define SQL_DELETE @"DELETE FROM events WHERE id = ?;"

    - (FMDatabase *)dbConnect
    {
        NSArray  *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString* dir = [paths objectAtIndex:0];

        return [FMDatabase databaseWithPath:[dir stringByAppendingPathComponent:DB_FILE_NAME]];
    }

    - (BOOL)update:(Event *)event
    {
        FMDatabase* db = [self dbConnect];
        [db open];

        BOOL success = [db executeUpdate:SQL_UPDATE, event.day, event.title, [NSNumber numberWithInt:event.time],[NSNumber numberWithInteger:event.eventId]]; 

        [db close];

        NSLog(@"%@", event.title);  //correctly outputs “test"

        return success;
    }

    - (BOOL)remove:(NSInteger)eventId
    {
        FMDatabase* db = [self dbConnect];
        [db open];

        BOOL isSucceeded = [db executeUpdate:SQL_DELETE, [NSNumber numberWithInteger:eventId]];

        [db close];

        return isSucceeded;
    }

How do I need to fix to update the db?
Thank you.

Was it helpful?

Solution

Your UPDATE statement is likely not updating anything because there is no record with that id value. If you examine the value returned by the FMDB changes method (sqlite3_changes()), which tells you how many rows were updated, I'd wager it will return zero. When you do an UPDATE and the WHERE clause fails to find any matches, it does not result in an error, but rather it will quietly succeed while not changing any data. The changes method can be used to confirm whether records were really updated.

You can either check first, to see if there is a value with that id value, and do an UPDATE if found or do an INSERT if not. Or, if you know that there will be no value with the same id value, you can just do INSERT directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top