如何使用FMDB在SQLite数据库中保存数据?

时间:2014-01-08 21:56:47

标签: ios sqlite uitableview fmdb

我有一个使用FMDB的租车应用。在用户登录(针对数据库进行身份验证)之后,用户一个接一个地导航到多个表视图。在第二个表格视图中,他可以从显示汽车名称的单元格中选择汽车,点击任何单元格,使用汽车图像显示的下一个视图,以及进入具有数据选项卡的下一个视图的按钮以选择日期&预订车辆的时间和显示车辆名称,图像和日期的按钮。选择时间用于用户的历史。

如何保存文字&与日期和数据库中的数据库中的该单元格相关的图像。预订时间?

3 个答案:

答案 0 :(得分:3)

your other question的基础上,我推断您正在询问如何使用FMDB保存数据。查看fmdb.m文件,了解大量精彩示例。

例如,您可以创建一个表:

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
}

这是您插入数据的方式(包括日期):

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
}

然后他们会告诉你如何检索信息(包括日期):

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

    return;
}

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];  

您应该使用自己的表(具有有意义的表和列名称)来实现此目的,但这说明了创建,插入和选择数据的基本技术。


例如,我们假设该表定义如下:

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

然后您可以像这样在表中保存记录:

- (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 (?, ?, ?, ?)" ,
            username,
            carName,
            carImageName,
            reservationDate];

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

        // do whatever you need to upon error
    }

    return success;
}

注意,我将汽车图像文件名存储在数据库中,而不是实际图像。坦率地说,汽车图像名称可能甚至不属于这个预订表(你可能想要一个汽车表,它在汽车标识号和汽车图像之间进行映射)。但希望这足以让你前进。

答案 1 :(得分:0)

解决方案:

NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:dbPath]];
[database open];
[database executeUpdate:@"CREATE TABLE IF NOT EXISTS USER_INFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, EMAIL TEXT, PASSWORD TEXT, C_PASSWORD TEXT)"];
[database close];

答案 2 :(得分:0)

保存您的数据并进行必要的更改:

//获取数据库路径     NSArray * pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);     NSString * docPathsStr = [pathArray objectAtIndex:0];

//Return our Database file path
NSString *appDBPathStr = [docPathsStr stringByAppendingPathComponent:@"employee.sqlite"];

//Create Sqlite DB
FMDatabase *aDatabase = [FMDatabase databaseWithPath:appDBPathStr];
[aDatabase open];
NSString *aInsertQuery = [NSString stringWithFormat:@"INSERT INTO EMPLOYEE VALUES ('%@', '%@' ,'%@' ,'%@' ,'%@' )",myFirstnameStr,myLastnameStr,myEmailStr,myPasswordStr,myDOBStr];
[aDatabase executeUpdate:aInsertQuery];
[aDatabase close];