2015-06-22 39 views
0

我一直在爲iOS設計Ionic Phonegap項目。在Appdelegate.m中實現了一個方法,該方法使得AJAX請求從服務器下載文本文件,該文件包含連接到另一個服務器的URL以使應用程序工作。iOS - 在真實設備上連接到數據庫的錯誤

我已經做了兩個班, WebContentWebCustomContent

WebContent.m我插入從文本文件帶到一個SQLite數據庫特定的URL,然後使用WebCustomContent.m

檢索參考下面的代碼塊

-(NSString*)getDataBasePath{ 

    //CHECK 
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString* foofile = [documentsPath stringByAppendingPathComponent:@"webcontentdb.sqlite"]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile]; 
    NSLog(@"%d", fileExists); 


    //END OF CHECK 



    //SIMULATOR 
    NSString *databasePath1 = [[NSBundle mainBundle] pathForResource:@"webcontentdb" ofType:@"sqlite"]; 
    // return databasePath1; 


    //REAL DEVICE 
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0]; 
    NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"webcontentdb.sqlite"]; 
    return databasePath; 
} 


-(void)updateUserAgeRange:(NSString*)age{ 

    NSString* databasePath = [self getDataBasePath]; 
    sqlite3 *database; 


    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 

     NSString *query = [NSString stringWithFormat:@"update user_setting set valstr = '%@' where keystr = 'AGE' ", age]; 

     NSLog(@"update %@" , query); 

     const char * sql = [query UTF8String]; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      sqlite3_step(compiledStatement); // Here is the added step. 
      NSLog(@"updateContact SUCCESS - executed command %@",query); 
     } 
     else { 
      NSLog(@"updateContact FAILED - failed to execute command %@",query); 
     } 

     sqlite3_finalize(compiledStatement); 

    } 
    else { 
     //NSLog(@"pdateContact FAILED - failed to open database"); 
    } 

    sqlite3_close(database); 

} 


- (NSString *)getUserPreferenceValues:(NSString*)keystr { 



    NSString *retval = [[NSString alloc] init] ; 
    NSString *query = [NSString stringWithFormat:@"SELECT valstr FROM user_setting where keystr = '%@' " , keystr]; 

    NSLog(@" query %@", query); 


    sqlite3_stmt *statement; 
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) { 
     while (sqlite3_step(statement) == SQLITE_ROW) { 


      char *nameChars = (char *) sqlite3_column_text(statement, 0); 

      NSString *name = [[NSString alloc] initWithUTF8String:nameChars]; 

      NSLog(@" valstr %@", name); 
      retval = name; 


     } 
     sqlite3_finalize(statement); 
    } 


    return retval; 
} 

-(void)insertDatabaseCommonValues:(NSString*)urlstr{ 

    NSString* databasePath = [self getDataBasePath]; 


    sqlite3 *database; 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 

     NSString *query = [NSString stringWithFormat:@"delete from url_preference"]; 


     const char * sql = [query UTF8String]; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      sqlite3_step(compiledStatement); // Here is the added step. 
      // NSLog(@"updateContact SUCCESS - executed command %@",query); 
     } 
     else { 
      //NSLog(@"updateContact FAILED - failed to execute command %@",query); 
     } 

     sqlite3_finalize(compiledStatement); 

    } 
    else { 
     //NSLog(@"pdateContact FAILED - failed to open database"); 
    } 




    //************************************INSERT************************************// 

    //sqlite3 *database; 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 


     //NSLog(@"URL STRING %@",urlstr); 

     NSString *query = [NSString stringWithFormat:@"insert into url_preference (name) values ( '%@' ) ", urlstr]; 

     NSLog(@"inset %@" , query); 

     const char * sql = [query UTF8String]; 
     sqlite3_stmt *compiledStatement; 

     NSLog(@" error code.. %d",sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL)); 

     if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      sqlite3_step(compiledStatement); // Here is the added step. 
      NSLog(@"updateContact SUCCESS - executed command %@",query); 
     } 
     else { 
      NSLog(@"updateContact FAILED - failed to execute command %@",query); 
     } 

     sqlite3_finalize(compiledStatement); 

    } 
    else { 
     //NSLog(@"pdateContact FAILED - failed to open database"); 
    } 

    sqlite3_close(database); 

} 

在這裏,當我打印BOOL變量fileExists時,它打印YES,意味着數據庫存在於Document中s文件夾。 但插入和更新查詢失敗,如下所示;

2015-06-22 11:18:18.215 App Name[5510:60b] URL http://www.google.lk 
2015-06-22 11:18:22.082 App Name[5510:60b] 1 
2015-06-22 11:18:24.103 App Name[5510:60b] success to open database! 
2015-06-22 11:18:26.197 App Name[5510:60b] 1 
2015-06-22 11:18:28.673 App Name[5510:60b] inset insert into url_preference (name) values ( 'http://www.google.lk' ) 
2015-06-22 11:18:28.676 App Name[5510:60b] error code.. 1 
2015-06-22 11:18:28.679 App Name[5510:60b] updateContact FAILED - failed to execute command insert into url_preference (name) values ( 'http://www.google.lk' ) 

我已經將數據庫文件放在項目文件夾中,如下所示;

enter image description here

我似乎無法找出什麼我做錯了。請幫忙。

回答

0

這可能是文件權限問題嗎?我建議右鍵點擊Finder中的webcontentdb.sqlite文件,選擇Get Info,並在共享下&權限使所有用戶擁有Read & Write權限。

如果這不起作用,我會使用SQLite瀏覽器應用程序來驗證數據庫文件是否可以寫入並且不會以任何方式損壞。