2012-03-23 62 views
1

當我在iphone應用程序中將數據推送到我的sqlite數據庫時遇到問題。 這裏是我如何創建數據庫:在iPhone應用程序中的sqlite數據庫錯誤:沒有這樣的表

-(IBAction)createButtonPressed:(id)sender 
{ 
    NSLog(@"createButtonPressed: called"); 
    status.text=[status.text stringByAppendingString:@"create button pressed.\n"]; 
    NSString *docsDir; 
    NSArray *dirPaths; 

    // Get the documents directory 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 

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

    NSFileManager *filemgr = [NSFileManager defaultManager]; 

    if ([filemgr fileExistsAtPath: databasePath ] == NO) 
    { 
     status.text=[status.text stringByAppendingString:@"creating database... "]; 
     status.text=[status.text stringByAppendingFormat:@"path: %@\n\n", databasePath]; 

    const char *dbpath = [databasePath UTF8String]; 

     if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
     { 
      status.text=[status.text stringByAppendingString:@"database opened successfully\n"]; 

      char *errMsg; 
      const char *sql_stmt; 
      NSString *sqlStatement=[[NSString alloc] init]; 

      /* 
      CREATING JOB TABLE 
      */ 
      [email protected]""; 
      [sqlStatement stringByAppendingString:@"CREATE TABLE IF NOT EXISTS JOB "]; 
      [sqlStatement stringByAppendingString:@"(ID INTEGER PRIMARY KEY AUTOINCREMENT, "]; 
      [sqlStatement stringByAppendingString:@"NUMBER TEXT, "]; 
      [sqlStatement stringByAppendingString:@"DATE_LOADING INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"DATE_DEPARTURE INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"DATE_UNLOADING INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"ORIGIN INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"DESTINATION INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"SUPPLIER INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"RAILROAD INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"FOREMAN INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"WEATHER INTEGER, "]; 
      [sqlStatement stringByAppendingString:@"COMMENT TEXT, "]; 
      [sqlStatement stringByAppendingString:@"MODIFY_STATUS INTEGER)"]; 

      sql_stmt = [sqlStatement UTF8String]; 

      if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
      { 
       status.text=[status.text stringByAppendingString:@"failed to create JOB table\n"]; 
      } 
      else 
      { 
       status.text=[status.text stringByAppendingString:@"JOB table created\n"]; 
      }; 


     status.text=[status.text stringByAppendingString:@"closing database...\n"]; 
     sqlite3_close(database); 

    } 
    else 
    { 
     status.text=[status.text stringByAppendingString:@"failed to open/create database\n"]; 
    }; 
} 
else 
{ 
    status.text=[status.text stringByAppendingString:@"database exists "]; 
    status.text=[status.text stringByAppendingFormat:@"path: %@\n\n", databasePath]; 
}; 

[status scrollRangeToVisible:NSMakeRange([status.text length], 0)]; 

}

一切都與創建正常。 但是! 當我嘗試將數據推到我的餐桌我得到一個錯誤:「沒有這樣的表JOB」 推送數據代碼:

NSLog(@"populateButtonPressed: called"); 
status.text=[status.text stringByAppendingString:@"populate button pressed.\n"]; 

NSFileManager *filemgr = [NSFileManager defaultManager]; 

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

    if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
    { 
     status.text=[status.text stringByAppendingString:@"database opened successfully\n"]; 

     char *errMsg; 
     const char *sql_stmt; 
     NSString *sqlStatement=[[NSString alloc] init]; 

     /* 
     POPULATING JOB TABLE 
     */ 
     [email protected]"INSERT INTO JOB (NUMBER, DATE_LOADING, DATE_DEPARTURE, DATE_UNLOADING, ORIGIN, DESTINATION, SUPPLIER, RAILROAD, FOREMAN, WEATHER, COMMENT, MODIFY_STATUS) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

     sql_stmt = [sqlStatement UTF8String]; 
     sqlite3_stmt *stmt; 
     sqlite3_prepare_v2(database, sql_stmt, -1, &stmt, nil); 
     sqlite3_bind_text(stmt, 1, "number", -1, SQLITE_STATIC); //number 
     sqlite3_bind_int(stmt, 2, 100); //date_loading 
     sqlite3_bind_int(stmt, 3, 101); //date_departure 
     sqlite3_bind_int(stmt, 4, 102); //date_unloading 
     sqlite3_bind_int(stmt, 5, 103); //origin 
     sqlite3_bind_int(stmt, 6, 104); //destination 
     sqlite3_bind_int(stmt, 7, 105); //supplier 
     sqlite3_bind_int(stmt, 8, 106); //railroad 
     sqlite3_bind_int(stmt, 9, 107); //foreman 
     sqlite3_bind_int(stmt, 10, 108); //weather 
     sqlite3_bind_text(stmt, 11, "commentstring", -1, SQLITE_STATIC); //comment 
     sqlite3_bind_int(stmt, 12, 1); 

     if (sqlite3_step(stmt)!=SQLITE_DONE) 
     { 
      status.text=[status.text stringByAppendingString:@"WARNING!!! Could not step (execute) stmt\n"]; 
      status.text=[status.text stringByAppendingFormat:@"error: %@\n", [NSString stringWithUTF8String:sqlite3_errmsg(database)]]; 
     } 
     else 
     { 
      status.text=[status.text stringByAppendingString:@"it seems like stmt step (execute) was successfull"]; 
     }; 

     if(sqlite3_finalize(stmt)==SQLITE_DONE) 
     { 
      status.text=[status.text stringByAppendingString:@"finalizing stmt was successfull\n"]; 
     } 
     else 
     { 
      status.text=[status.text stringByAppendingString:@"WARNING!!! failed finalizing stmt\n"]; 
     }; 


     status.text=[status.text stringByAppendingString:@"closing database...\n"]; 
     sqlite3_close(database); 

    } 
    else 
    { 
     status.text=[status.text stringByAppendingString:@"failed to open database\n"]; 
    }; 
} 
else 
{ 
    status.text=[status.text stringByAppendingString:@"database does not exist. you need to create it.\n"]; 
}; 

[status scrollRangeToVisible:NSMakeRange([status.text length], 0)]; 

數據庫中存在的路徑,併成功打開。 iOS5 ipad應用程序,使用ARC和libsqlite3.dylib,我在模擬器中運行我的應用程序。

+0

'sqlite3_prepare_v2(數據庫,sql_stmt,-1,和語句,無);'只是這行後,我得到一個錯誤: 「沒有這樣的表JOB」 – Oleg 2012-03-23 13:53:14

回答

1

是你的db路徑是否正確?你爲什麼不先檢查你的終端?

> sqlite3 <your_db_path> 

> show tables; 
+0

'lifeinlagsworkstation:文檔lifeinlags $ sqlite3的database.sqlite 的SQLite版本3.6.12 輸入「.help」作爲指令 輸入以「;」結尾的SQL語句 sqlite> show tables ...> ' – Oleg 2012-03-23 14:09:05

+0

file database.sqlite exists there。但它似乎沒有創建表。爲什麼?我的代碼有什麼問題? (你可以看到在我的問題上創建表代碼) – Oleg 2012-03-23 14:10:35

+1

我對此表示懷疑。以及我會建議你在bit.ly/iUPld0上留出2分鐘的時間。我不知道,因爲我討厭使用簡單的sqlite3導致很多痛苦,因爲有許多sql包裝類。 bit.ly/GFOS1Q – 2012-03-23 14:22:23

相關問題