Database insert failed
I stored details in my server. Im using server url for fetching detail and
store to database table. I got insertion failed NSLog. I have 2 sqlite
file in documents folder. After copyItemAtPath, im writing to that file.
code:
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *err;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"db5"
ofType:@"sqlite"];
//NSLog(@"bundlePath %@", bundlePath);
//call update function to check any data updated,
//if there is a version difference
//update the data base with all the required fileds.
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSLog(@"docs dir is %@", documentsDirectory);
NSString *appFile = [documentsDirectory
stringByAppendingPathComponent:@"db1.sqlite"];
// [fileMgr copyItemAtPath:bundlePath toPath:appFile error:&err];
BOOL success = [fileMgr copyItemAtPath:bundlePath toPath:appFile
error:&err];
if (!success) {
NSLog(@"Failed to create writable database file with message '%@'.",
[err localizedDescription]);
}
NSURL *URL = [NSURL
URLWithString:@"http://myserver.net/projects/mobile/jsonstring.php"];
NSError *error;
NSString *stringFromFileAtURL = [[NSString alloc]
initWithContentsOfURL:URL
encoding:NSUTF8StringEncoding
error:&error];
//NSLog(@"response is %@", stringFromFileAtURL);
NSString *path = [documentsDirectory
stringByAppendingPathComponent:@"db1.sqlite"];
//NSLog(@"filepath %@",path);
//array
NSArray *userData = [stringFromFileAtURL JSONValue];
[stringFromFileAtURL release];
// NSLog(@"userdata is %@", userData);
int i = 0;
BOOL notExist = TRUE;
// sqlite3_stmt *statement, *addStmt;
for (NSArray *skarray in userData) {
//NSLog(@"test");
if(i == 0){
//insert all main category
for (NSDictionary *tuser in skarray) {
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
NSLog(@"path is %s", [path UTF8String]);
sqlite3_stmt *addStmt = NULL;
const char *sqlInsert = "INSERT INTO categories
(id,cat_name,order_by) VALUES(?, ?, ?)";
int result = sqlite3_prepare_v2(database,sqlInsert,
-1, &addStmt, NULL);
if(result != SQLITE_OK){
NSAssert1(0, @"Error while creating add statement.
'%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 0, [[tuser
objectForKey:@"id"] UTF8String], -1,
SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 1, [[tuser
objectForKey:@"cat_name"] UTF8String], -1,
SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [[tuser
objectForKey:@"order_by"] UTF8String], -1,
SQLITE_TRANSIENT);
//Execute the statement
if (result == SQLITE_OK) {
result = sqlite3_step(addStmt);
}
if (result == SQLITE_DONE || result == SQLITE_ROW) {
result = sqlite3_reset(addStmt);
NSLog(@"Inserted");
}
else{
NSLog(@"InsertFailed");
}
No comments:
Post a Comment