I'm trying to bulk write an Array of Dictionaries into an SQLite db using a SQLite Transaction for speed as the Array can become quite large.
The code:
-(void)bulkCachetoSQLite:(NSArray *)dataToCache
{
//Logging Comment
NSLog(@"SQLiteClass - bulkCachetoSQLite");
sqlite3_stmt *statement;
const char *dbpath = [_filePath UTF8String];
char * sErrMsg = 0;
const char *tail = 0;
if(sqlite3_open(dbpath, &_DB) == SQLITE_OK){
char sSQL [BUFFER_SIZE] = "\0";
sprintf(sSQL, "INSERT INTO livedata (device,datatype,timestamp,rawdata)");
sqlite3_prepare_v2(_DB, sSQL, BUFFER_SIZE, &statement, &tail);
sqlite3_exec(_DB, "BEGIN TRANSACTION", NULL, NULL, &sErrMsg);
for (NSObject* data in dataToCache)
{
//NSLog(@"%@",data);
sqlite3_bind_text(statement, 1, [[data valueForKey:@"device"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [[data valueForKey:@"type"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [[data valueForKey:@"timestamp"] UTF8String], -1, SQLITE_TRANSIENT);
NSString *str = [[NSString alloc] initWithData:[data valueForKey:@"data"] encoding:NSUTF8StringEncoding];
sqlite3_bind_text(statement, 4, [str UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_clear_bindings(statement);
sqlite3_reset(statement);
}
sqlite3_exec(_DB, "END TRANSACTION", NULL, NULL, &sErrMsg);
NSLog(@"SQLiteClass - bulkCachetoSQLite - EorrorA is %s", sErrMsg);
}
else{
//db isn't open
}
NSLog(@"SQLiteClass - bulkCachetoSQLite - EorrorB is %s", sqlite3_errmsg(_DB));
sqlite3_finalize(statement);
sqlite3_close(_DB);
}
Everything appears okay ERRORS return Null, but nothing is being written it to the SQLite db.
The db table structure is:
CREATE TABLE `livedata` (
`ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`device` TEXT,
`datatype` TEXT,
`timestamp` TEXT,
`rawdata` TEXT
);
Any ideas?
Aucun commentaire:
Enregistrer un commentaire