不能使用快速枚举来填充sqlite表

时间:2015-03-28 16:50:53

标签: ios objective-c sqlite nsdictionary fast-enumeration

我有一个名为masterMessages的消息对象。看起来像这样:

self.messages = (
"<lean: 0x7fcf98665140, objectId: vglE1UJ5KI, localId: (null)> {\n    messageBody = Jj;\n    recipientId = XvvxETqjph;\n    senderId = XvvxETqjph;\n    timestamp = \"1424991590106.210938\";\n}",
"<lean: 0x7fcf98667940, objectId: rgBFYBMKlU, localId: (null)> {\n    messageBody = \"test 3 from ian\";\n    recipientId = XvvxETqjph;\n    senderId = Hoy7UjLzOh;\n    timestamp = \"1424631667110.638184\";\n}",
"<lean: 0x7fcf98667f30, objectId: hB5uhwsYsu, localId: (null)> {\n    messageBody = \"test 2 from user1\";\n    recipientId = XvvxETqjph;\n    senderId = VQzxWbDnal;\n    timestamp = \"1424630904935.162109\";\n}",
"<lean: 0x7fcf986685b0, objectId: dOe2B9oq5b, localId: (null)> {\n    messageBody = \"test 1\";\n    recipientId = XvvxETqjph;\n    senderId = XvvxETqjph;\n    timestamp = \"1424630808309.478027\";\n}"
)

我通过在viewDidLoad中调用此方法来创建我的sqlite数据库,并且它说我的sqlite3_errmsg变量被错误地使用(它曾经只是一个字符):

-(void)createDatabase{

NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = dirPaths[0];

// Build the path to the database file
_databasePath = [[NSString alloc]
                 initWithString: [docsDir stringByAppendingPathComponent:
                                  @"messages.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &_messagesDB) == SQLITE_OK)
    {
        sqlite3_errmsg *errMsg;
        const char *sql_stmt =
        "CREATE TABLE IF NOT EXISTS MESSAGES (ID INTEGER PRIMARY KEY AUTOINCREMENT, objectId TEXT, messageBody TEXT, recipientId TEXT, senderId TEXT, create_time TEXT)";

        if (sqlite3_exec(_messagesDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
            NSLog(@"Failed to create table");

        }
        sqlite3_close(_messagesDB);
    } else {
        NSLog(@"Failed to open/create database");
    }
}

}

我正在尝试使用快速枚举和sqlite3_bind_xxx将masterMessages保存到我的SQLite'MESSAGES'表中,因为rmaddy建议但我相信我调用它的方法不正确,因为我一直收到预期标识符的错误。

-(void)saveQuery{
sqlite3_stmt    *statement;
const char *dbpath = [_databasePath UTF8String];

if (sqlite3_open(dbpath, &_messagesDB) == SQLITE_OK)
{
    for (id lean in masterMessages) {

       int *insertSQL = [int sqlite3_bind("INSERT INTO MESSAGES (objectId, messageBody, recipientId, senderId, create_time) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",
                           [lean valueForKeyPath:@"objectId"],
                           [lean valueForKeyPath:@"messageBody"],
                           [lean valueForKeyPath:@"recipientId"],
                           [lean valueForKeyPath:@"senderId"],
                           [lean valueForKeyPath:@"timestamp"])];

        NSLog(@"insertSQL = %@", insertSQL);

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(_messagesDB, insert_stmt,
                        -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Message added");
        } else {
            NSLog(@"Failed to add message");
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(_messagesDB);
}
}

我该如何解决这个问题?

谢谢。

0 个答案:

没有答案