2013-10-12 33 views
-1
- (void)viewDidLoad 
{ 
    [super viewDidLoad];// Do any additional setup after loading the view. 
    NSString *docsDir; 
    NSArray *dirPaths; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 

    databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Notebook1.sqlite"]]; 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 

    NSLog(@"%@",databasePath); 

    if ([fileMgr fileExistsAtPath:databasePath]==NO) 
    { 
     const char *dbpath = [databasePath UTF8String];  
     NSLog(@"hohoho, %s",dbpath); 
     if (sqlite3_open(dbpath, &noteDB)==SQLITE_OK) 
     { 
      char *errMsg; 
      const char *sql_str = "CREATE TABLE IF NOT EXISTS Notebook1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, Whattime Text, Address TEXT, What TEXT, Who TEXT, NOTE TEXT)"; 

      if (sqlite3_exec(noteDB, sql_str, NULL, NULL, &errMsg)!=SQLITE_OK) 
      { 
       NSLog(@"Failed to create table"); 
      } 


      sqlite3_close(noteDB); 
     } 
     else 
     { 
      NSLog(@"Failed to open/create database"); 
     } 
    } 
    self.title = @"Add new"; 
} 

-(IBAction)addNote:(id)sender 
{ 
    char *errMsg; 
    const char *dbpath = [databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &noteDB)==SQLITE_OK) 
    { 
     NSLog(@"we are here"); 
     NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Notebook1(Whattime, Address, What, Who, Note) VALUES(\"%@\",\"%@\",\"%@\"\"%@\",\"%@\")", self.whenField.text, self.whenField.text, self.whenField.text,self.whenField.text, self.whenField.text]; 
     const char *insert_stmt = [insertSQL UTF8String]; 

     if (sqlite3_exec(noteDB, insert_stmt, NULL, NULL, &errMsg)==SQLITE_OK) 
     { 
      self.whenField.text = @""; 
      self.whereField.text = @""; 
      self.whatField.text = @""; 
      self.whoField.text = @""; 
      self.noteView.text = @""; 

      [self doAlert:@"add ok"]; 
     } 
     else 
     { 
      NSLog(@"error: %s",errMsg); 
      sqlite3_free(errMsg); 
     } 

     sqlite3_close(noteDB); 
    } 
} 

在「viewDidLoad」程序中創建一個具有6個屬性:ID,Whattime,Address,What,who,Note的sqlite表。 在「addNote」中,我試圖將一些數據插入到數據庫中。 但它沒有這樣做,並在日誌說: 2013-10-12 05:10:49.328筆記本[4266:A0B]錯誤:5列ios和sqlite:4列值爲5列

回答

2

你的SQLite表有6個屬性:ID,Whattime,地址,什麼,誰,請注意。而且你只插入5個值,即你沒有插入ID的值,因爲你必須插入ID的值。如果你想要你的ID是自動增量比嘗試這個:

-(IBAction)addNote:(id)sender 
{ 
    NSString *dbFilePath =[DBclass getDBPath]; 

    sqlite3_stmt *addStmt = nil; 
    if(sqlite3_open([dbFilePath UTF8String], &database)==SQLITE_OK) 
    { 

     const char *sql ="INSERT INTO Notebook1(ID,Whattime, Address, What, Who, Note) Values(?,?,?,?,?,?)"; 
     if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) 
      NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database)); 

     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_bind_int (addStmt, 1, ID); 
     sqlite3_bind_text(addStmt, 2, [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 3, [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 4, [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 5, [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 6, [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT); 


     if(SQLITE_DONE != sqlite3_step(addStmt)) 
      NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 
     else 
      //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid 
      ID = sqlite3_last_insert_rowid(database); 


     sqlite3_close(database); 
    } 

} 
+1

該代碼應該完成聲明。 – rmaddy

1

你在這個缺少一個逗號在這裏\"%@\",\"%@\",\"%@\"\"%@\",\"%@\" 4個值行:

NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Notebook1(Whattime, Address, What, Who, Note) VALUES(\"%@\",\"%@\",\"%@\"\"%@\",\"%@\")", self.whenField.text, self.whenField.text, self.whenField.text,self.whenField.text, self.whenField.text]; 

它應該是這樣的:

NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Notebook1(Whattime, Address, What, Who, Note) VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")", self.whenField.text, self.whenField.text, self.whenField.text,self.whenField.text, self.whenField.text]; 
+0

這是一個壞主意(和容易出錯)使用字符串格式來構建查詢。使用'sqlite3_bind_xxx'好得多,就像iMove的答案中所示。 – rmaddy