我在我的服務器中存儲了詳細信息。我使用服務器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");
}
在改變基於1的索引後,我插入了日誌。但數據沒有插入到db1.sqlite表中。我通過SQLiteManager打開了db1.sqlite文件。但表格沒有任何值。爲什麼? – user2674668
@ user2674668你在模擬器上運行這個嗎?你是否在模擬器的Document文件夾中打開數據庫(而不是在項目文件夾或應用程序包中的副本)?它位於〜/ Library/Application Support/iPhone Simulator/....如果在Mac OS中無法看到您的Library文件夾,則可能必須執行'chflags nohidden〜/ Library'。 – Rob
我在下面得到NSLog:綁定1失敗:綁定或列索引超出範圍,綁定2失敗:綁定或列索引超出範圍,綁定3失敗:綁定或列索引超出範圍 – user2674668