我将图像存储在服务器URL中,然后从JSON中获取图像,然后写入本地数据库。但NSLog显示categories insert is insert into categories (id,cat_name,order_by) values('3','test','3')
。但是db1.sqlite文件中没有显示值。
代码:
文件管理器:
- (void)viewDidLoad
{
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *err;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"db4" ofType:@"sqlite"];
//NSLog(@"bundlePath %@", bundlePath);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSLog(@"docs dir is %@", documentsDirectory);
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];
BOOL success = [fileMgr copyItemAtPath:bundlePath toPath:appFile error:&err];
if (!success) {
NSLog(@"Failed to create writable database file with message '%@'.", [err localizedDescription]);
}
从服务器URL获取数据添加到数据库文件:
NSURL *URL = [NSURL URLWithString:@"http://serverurl.net/projects/mobile/jsonstring.php"];
NSError *error;
NSString *stringFromFileAtURL = [[NSString alloc]
initWithContentsOfURL:URL
encoding:NSUTF8StringEncoding
error:&error];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];
//NSLog(@"filepath %@",path);
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) {
const char *sql = [[NSString stringWithFormat:@"SELECT id FROM categories where id = '%@'",[tuser objectForKey:@"id"]] cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"categories select is %s", sql);
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
notExist = TRUE;
while (sqlite3_step(statement) == SQLITE_ROW) {
notExist = FALSE;
}
}
if(notExist){
NSLog(@"categories does not exist");
// const char *sqlInsert = "insert into categories (id,cat_name,order_by) Values(?, ?, ?)";
const char *sqlInsert = [[NSString stringWithFormat:@"insert into categories (id,cat_name,order_by) values('%@','%@','%@')", [tuser objectForKey:@"id"], [tuser objectForKey:@"cat_name"],[tuser objectForKey:@"order_by"]] cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"categories insert is %s", sqlInsert);
if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) != 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);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
sqlite3_reset(addStmt);
}
sqlite3_close(database);
}
}
}
我问了很多问题。但没有得到解决方案。