我将详细信息存储在我的服务器中。我使用服务器 url 获取详细信息并存储到数据库表。我插入失败的 NSLog。我在文档文件夹中有 2 个 sqlite 文件。在 copyItemAtPath 之后,我正在写入该文件。
代码:
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) {
NSString *query = @"delete from categories";
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSLog(@"result is here");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
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");
}