2014-07-24 171 views
0

我正在Xcode中開發iPhone應用程序,我無法訪問我創建的數據庫。以下是我在哪裏創建我的數據庫Sqlite3數據庫未執行查詢

//create local database 
NSString *docsDir; 
NSArray *dirPaths; 
sqlite3 *localDB; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

// Build the path to the database file 
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"local.db"]]; 

NSFileManager *filemgr = [NSFileManager defaultManager]; 

if ([filemgr fileExistsAtPath: databasePath ] == NO) { 
    const char *dbpath = [databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &localDB) == SQLITE_OK) { 
     char *errMsg; 
     const char *sql_stmt1 = "CREATE TABLE IF NOT EXISTS My_choices (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, na TEXT, got_it TEXT)"; 

     const char *sql_stmt2 = "CREATE TABLE IF NOT EXISTS Ratings (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, rating NUMERIC)"; 

     const char *sql_stmt3 = "CREATE TABLE IF NOT EXISTS Consultant_Details (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,website TEXT,domain TEXT,domain_no NUMERIC,phone TEXT,facebook TEXT)"; 

     if (sqlite3_exec(localDB, sql_stmt1, NULL, NULL, &errMsg) != SQLITE_OK) { 
      NSLog(@"%@",@"Failed to create table My_choices"); 

     } 
     if (sqlite3_exec(localDB, sql_stmt2, NULL, NULL, &errMsg) != SQLITE_OK) { 
      NSLog(@"%@",@"Failed to create table ratings"); 

     } 
     if (sqlite3_exec(localDB, sql_stmt3, NULL, NULL, &errMsg) != SQLITE_OK) { 
      NSLog(@"%@",@"Failed to create table Consultant_Details"); 

     } 


     sqlite3_close(localDB); 
    } 
    else { 
     NSLog(@"%@",@"Failed to open/create database"); 

    } 
} 

下面添加的代碼是在哪裏我試圖訪問它

NSString *docsDir; 
NSArray *dirPaths; 
sqlite3 *localDB; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

// Build the path to the database file 
NSString *dbPath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"local.db"]]; 


NSString *q = @"select * from Ratings where perfume_id in"; 

q = [q stringByAppendingString:idList]; 

NSLog(@"%@",q); 
const char *sqlStatement2 = [q UTF8String]; 
sqlite3_stmt *compiledStatement2; 
if(sqlite3_open([dbPath UTF8String], &localDB) == SQLITE_OK) { 
    if(sqlite3_prepare_v2(localDB, sqlStatement2, -1, &compiledStatement2, NULL) == SQLITE_OK) { 

     NSLog(@"%d", SQLITE_ROW); 
     // Loop through the results and add them to the feeds array 
     while(sqlite3_step(compiledStatement2) == SQLITE_ROW) { 


      // Read the data from the result row 
      int r = sqlite3_column_int(compiledStatement2, 1); 
      int id = sqlite3_column_int(compiledStatement2, 0); 
      NSLog(@"%d", id); 

      for (ListItem *item in self.listItemArray) { 

       if(item.perfumeId == id){ 
        item.rating = r; 

       } 
      } 

     } 
    } 
    else{ 
     NSLog(@"query failed: %s", sqlite3_errmsg(localDB)); 
    } 
} 
sqlite3_close(localDB); 

我總是得到的代碼「查詢失敗不存在頁表:評價」中我的日誌。 任何幫助將不勝感激。

+0

什麼是「中」後,在您的查詢的表名...? –

+0

如果([filemgr fileExistsAtPath:databasePath] == NO)在這裏放置斷點,並檢查這個條件是否爲真。 – karthikeyan

+0

好吧我已經把這個休息和文件存在...所以這意味着表沒有創建!但我找不到爲什麼? – user1554693

回答

0

基本上這個錯誤是由於數據庫中的Table無法使用或錯誤的查詢造成的。根據你的帖子,IT看起來像查詢中的錯誤。

查詢應該是這樣的:

NSString *q = @"select * from Ratings where perfume_id in ('12','13','14','15')"; 

打印您"String q",並確保它上面的跟隨。

+0

請檢查我編輯的問題。我收到一個錯誤「沒有這樣的表格存在:評級」。我想我的表格沒有正確創建。你可以在創建數據庫的代碼中發現任何問題嗎?謝謝 – user1554693

+0

請從您嘗試創建表的第一個方法發佈錯誤日誌。解決您的問題可能會有幫助。 – Surjeet

0

感謝大家的幫助。同樣的數據庫文件已存在於該路徑。因此,我的第一個方法是創建數據庫 if([filemgr fileExistsAtPath:databasePath] == NO)檢查是否爲false,並且未創建表。從文件目錄

0

先刪除數據庫文件,然後使用下面的代碼創建一個SQLite表..

if (![fileManager fileExistsAtPath: _databasePath ]) 
     { 
      const char *dbpath = [_databasePath UTF8String]; 
      if (sqlite3_open(dbpath, &_myDataBase) == SQLITE_OK) 
      { 
       char *errMsg; 
       const char *sql_stmt = 
       "CREATE TABLE IF NOT EXISTS My_choices (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, na TEXT, got_it TEXT);" //TEST 
       "CREATE TABLE IF NOT EXISTS Ratings (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, rating NUMERIC);" //USER 
       "CREATE TABLE IF NOT EXISTS Consultant_Details (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,website TEXT,domain TEXT,domain_no NUMERIC,phone TEXT,facebook TEXT)"; //METADATA 


       if (sqlite3_exec(_myDataBase, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
       { 
        NSLog(@"Failed to create tables"); 
       } 
       sqlite3_close(_myDataBase); 
       NSLog(@"DataBase created at: %@",_databasePath); 
      } else { 
       NSLog(@"Failed to open/create database"); 
      } 
     } 
+0

這有什麼用?你在這裏沒有得到真正的問題(OP也有他的「答案」)。問題是創建過程的任何失敗都會留下無法使用的半創建的數據庫文件。它應該在失敗時被刪除,應用程序應該崩潰,我估計。 – trojanfoe

+0

@trojanfoe是這個錯誤的答案?如果它告訴我,我會刪除它.. – karthikeyan

+0

它沒什麼大錯,但它與OPs代碼沒有什麼不同。 – trojanfoe