2011-02-03 62 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); 
    }