samedi 28 mars 2015

Cant use Fast enumeration to fill sqlite table

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:



-(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)
{
char *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 in this next method but it keeps logging 'Failed to add message':



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

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

NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO MESSAGES (objectId, messageBody, recipientId, senderId, create_time) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",
[masterMessages valueForKey:@"objectId"],
[masterMessages valueForKey:@"messageBody"],
[masterMessages valueForKey:@"recipientId"],
[masterMessages valueForKey:@"senderId"],
[masterMessages valueForKey:@"timestamp"]];

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


What is wrong with my last section of code?


Thank you.


Aucun commentaire:

Enregistrer un commentaire