2011-02-03 90 views
0

我無法理解問題。插入不能在SQLite iOS上工作

- (void)viewDidLoad { 
    NSString *docsDir; 
    NSArray *dirPaths; 

    // 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: @"contacts.sqlite"]]; 
} 

- (IBAction) saveData{ 
    sqlite3_stmt *statement; 

    const char *dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 
     NSLog(@"enter %@",name.text); 

     NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO EMP (name) VALUES (\"%@\")", name.text]; 

     //NSLog(@"enter123 %@",dbpath); 

     const char *insert_stmt = [insertSQL UTF8String]; 

     sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); 
     NSLog(@"enter789 "); 

     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      NSLog(@"Done success"); 

      /*status.text = @"Contact added"; 
      name.text = @""; 
      address.text = @""; 
      phone.text = @"";*/ 
     } 
     else 
     { 
      NSLog(@"fail"); 
      //status.text = @"Failed to add contact"; 
     } 
     sqlite3_finalize(statement); 
     sqlite3_close(contactDB); 
    } 
} 
+2

都不會對任何人,如果你的問題仍然存在,因爲它是。修改問題,添加有關您所看到的任何錯誤或警告的更多詳細信息。描述你期望發生的事情和實際發生的事情,並展示你已經嘗試過的解決問題的方法。沒有這些,你的問題就會很快關閉。 – Jasarien 2011-02-03 11:00:27

+0

什麼是你得到確切的問題?任何崩潰?或失敗? – KingofBliss 2011-02-03 14:18:47

回答

-1

利用這一點,的幫助你

sqlite3_stmt *statement; 
    const char *dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK){ 
    const char *insert_stmt = "INSERT INTO EMP (name) VALUES (?)"; 
    if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL)==SQLITE_OK){ 
     sqlite3_bind_text(statement,1, [name.text UTF8String], -1, SQLITE_TRANSIENT); 
    } 
    if (sqlite3_step(statement) == SQLITE_DONE) 
     NSLog(@"You have Sucessfully saved!"); 
    else 
     NSLog(@"Failed to Save!"); 

     sqlite3_finalize(statement); 
    }