Neelang Neelang - 9 months ago 60
iOS Question

How to use FMDB to save data in SQLite database?

I have a car rental app that uses FMDB. After the user logs in (authenticated against the database), the user navigates to multiple tableviews, one after another. In the 2nd tableview he can select a car from cells showing car's name, on tapping any cell, next view i shown with car image, and a button to proceed to next view having a datapicker to select date & time when the car is booked and a button to show the car name, image and date & time of selection to be used for history of user.

How can I save the text & image related to that cell in the database with the date & time of booking?

Rob Rob
Answer Source

On the basis of your other question, I infer you're asking how to save data using FMDB. Take a look at the fmdb.m file for tons of wonderful examples.

For example, this is you create a table:

BOOL success;

success = [db executeUpdate:@"create table if not exists test (a text, b text, c integer, d double, e double)"];

if (!success) {
    NSLog(@"%s: create table error: %@", __FUNCTION__, [db lastErrorMessage]);

    // do whatever you want upon error

This is how you insert data (including a date):

success = [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
        @"hi'", // look!  I put in a ', and I'm not escaping it!
        [NSString stringWithFormat:@"number %d", i],
        [NSNumber numberWithInt:i],
        [NSDate date],
        [NSNumber numberWithFloat:2.2f]];

if (!success) {
    NSLog(@"%s: insert error: %@", __FUNCTION__, [db lastErrorMessage]);

    // do whatever you need to upon error

And then they show you how to retrieve the information (including the date):

FMResultSet *rs = [db executeQuery:@"select rowid,* from test where a = ?", @"hi'"];

if (!rs) {
    NSLog(@"%s: select error: %@", __FUNCTION__, [db lastErrorMessage]);

    // do whatever you want upon error


while ([rs next]) {
    // just print out what we've got in a number of formats.
    NSLog(@"%d %@ %@ %@ %@ %f %f",
          [rs intForColumn:@"c"],
          [rs stringForColumn:@"b"],
          [rs stringForColumn:@"a"],
          [rs stringForColumn:@"rowid"],
          [rs dateForColumn:@"d"],
          [rs doubleForColumn:@"d"],
          [rs doubleForColumn:@"e"]);

[rs close];  

You should give this a go using your own table (with meaningful table and column names), but this illustrates the basic techniques for creating, inserting, and selecting data.

For example, let's assume the table was defined as follows:

CREATE TABLE IF NOT EXISTS reservation (reservation_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, car_name TEXT, car_image_name TEXT, reservation_date DOUBLE)

You can then save a record in that table like so:

- (BOOL)saveReservationForUser:(NSString *)username carName:(NSString *)carName carImageName:(NSString *)carImageName reservationDate:(NSDate *)reservationDate
    BOOL success = [db executeUpdate:@"insert into reservation (username, car_name, car_image_name, reservation_date) values (?, ?, ?, ?)" ,

    if (!success) {
        NSLog(@"%s: insert error: %@", __FUNCTION__, [db lastErrorMessage]);

        // do whatever you need to upon error

    return success;

Note, I'm storing the car image filename, not the actual image, in the database. Frankly, the car image name probably doesn't even belong in this reservation table (you'd probably want a table of cars that maps between a car identifier number to the name and the image of the car). But hopefully this is enough to get you going.