0

I have a messages object called masterMessages. That looks like this:

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}"
)

I create my sqlite database by calling this method in my viewDidLoad and its saying my sqlite3_errmsg variable is being used incorrectly (it used to be just a char):

-(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");
    }
}

}

I'm trying to save masterMessages to my SQLite 'MESSAGES' table by using fast enumeration and sqlite3_bind_xxx as rmaddy suggested but I believe I am invoking it incorrectly because i keep getting an error of expected identifier.

-(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);
}
}

How do I fix this?

Thank you.

ian
  • 1,002
  • 2
  • 12
  • 29
  • 1
    Did you try to log `insertSQL` to check that its value seems correct? – Larme Mar 28 '15 at 17:08
  • Do not create SQL statements using `stringWithFormat:`. Create proper prepared statements and bind values to the statement using the appropriate `sqlite3_bind_xxx` functions. – rmaddy Mar 28 '15 at 17:32
  • And use `sqlite3_errmsg` to log error message when things fail. – rmaddy Mar 28 '15 at 17:33
  • 1
    I think you meant `[lean valueForKey:@"TheKey"]` instead of `[masterMessages valueForKey:@"theKey"]` for `insertSQL`. I don't know about SQL and iOS, but @rmaddy could be pointing another issue ;) – Larme Mar 28 '15 at 18:02
  • @rmaddy i tried to do as you said but i believe i'm calling it incorrectly because i keep getting and expected identifier error – ian Mar 28 '15 at 18:42
  • @user2792129 If you want help with that issue, post a relevant question with the details. – rmaddy Mar 29 '15 at 01:01

0 Answers0