2014-02-12 69 views
1

我有一個應用程序,我把用戶的細節保存到SQLite數據庫。此外,我能夠找到細節並顯示結果。找不到更新的行SQLite IOS

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

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

docsDir = dirPaths[0]; 

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

NSFileManager *filemgr = [NSFileManager defaultManager]; 

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

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = 
     "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT , ADDRESS TEXT, PHONE TEXT)"; 

     if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
     { 
      _status.text = @"Failed to create table"; 
     } 
     sqlite3_close(_contactDB); 
    } else { 
     _status.text = @"Failed to open/create database"; 
    } 
} 



- (IBAction)saveData:(id)sender { 
sqlite3_stmt *statement; 
const char *dbpath = [_databasePath UTF8String]; 

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
{ 

    NSString *insertSQL = [NSString stringWithFormat: 
          @"INSERT OR IGNORE INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", 
          _name.text, _address.text, _phone.text]; 
     const char *insert_stmt = [insertSQL UTF8String]; 

    sqlite3_prepare_v2(_contactDB, insert_stmt, 
         -1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     _status.text = @"Contact added"; 
     _name.text = @""; 
     _address.text = @""; 
     _phone.text = @""; 
    } else { 
     _status.text = @"Failed to add contact"; 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(_contactDB); 
} 

}

- (IBAction)findContact:(id)sender { 
const char *dbpath = [_databasePath UTF8String]; 

sqlite3_stmt *statement; 

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
{ 
    NSString *querySQL = [NSString stringWithFormat: 
          @"SELECT address, phone FROM contacts WHERE name=\"%@\"", 
          _name.text]; 

    const char *query_stmt = [querySQL UTF8String]; 

    if (sqlite3_prepare_v2(_contactDB, 
          query_stmt, -1, &statement, NULL) == SQLITE_OK) 
    { 
     if (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      NSString *addressField = [[NSString alloc] 
             initWithUTF8String: 
             (const char *) sqlite3_column_text(
                     statement, 0)]; 
      _address.text = addressField; 
      NSString *phoneField = [[NSString alloc] 
            initWithUTF8String:(const char *) 
            sqlite3_column_text(statement, 1)]; 
      _phone.text = phoneField; 
      _status.text = @"Match found"; 
     } else { 
      _status.text = @"Match not found"; 
      _address.text = @""; 
      _phone.text = @""; 
     } 
     sqlite3_finalize(statement); 
    } 
    sqlite3_close(_contactDB); 
} 

}

上面的代碼是工作良好。但是,當我嘗試使用更新時,當我嘗試查找它時不顯示。即使聯繫人被保存,它也沒有找到匹配。

這是由我試圖更新代碼:

- (IBAction)saveData:(id)sender { 
sqlite3_stmt *statement; 
const char *dbpath = [_databasePath UTF8String]; 

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
{ 

    NSString *insertSQL = [NSString stringWithFormat: 
          @"INSERT OR IGNORE INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", 
          _name.text, _address.text, _phone.text]; 

    insertSQL = [NSString stringWithFormat: 
       @"UPDATE CONTACTS SET name=\"%@\", address=\"%@\", phone=\"%@\"", 
       _name.text, _address.text, _phone.text]; 

    const char *insert_stmt = [insertSQL UTF8String]; 
    sqlite3_prepare_v2(_contactDB, insert_stmt, 
         -1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     _status.text = @"Contact added"; 
     _name.text = @""; 
     _address.text = @""; 
     _phone.text = @""; 
    } else { 
     _status.text = @"Failed to add contact"; 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(_contactDB); 
} 

}

基本上我的目標是能夠保存更新找到的。 (不失上更新的主鍵,因爲我有FK關係)

編輯 這是我創造分貝方法 - (無效){CREATEDB NSString的 * DOCSDIR; NSArray * dirPaths;

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

docsDir = dirPaths[0]; 

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

NSFileManager *filemgr = [NSFileManager defaultManager]; 

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

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = 
     "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT , ADDRESS TEXT, PHONE TEXT)"; 

     if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
     { 
      _status.text = @"Failed to create table"; 
     } 
     sqlite3_close(_contactDB); 
    } else { 
     _status.text = @"Failed to open/create database"; 
    } 
} 

回答

0

我不能得到你的第二個 - (IBAction爲)SAVEDATA:(ID)發送{}方法[第三代碼塊] ..

您上述2塊說是正常工作,所以它的確定。

我想你嵌入更新在刀片的邏輯的邏輯,即你在SAVEDATA方法嵌入更新方法..

這裏我不能得到什麼ü做了,所以不是代碼在這裏IM提供更新的模板查詢時,只是把它你的代碼...

sqlite3_stmt *statement; 
if(sqlite3_open(dbpath, &database_object) == SQLITE_OK) 
{ 
    NSString *sql = [NSString stringWithFormat:@"update table_name set column_name = \"%@\"", Your_value]; 
    const char *sql_stmt = [sql UTF8String]; 
    if(sqlite3_prepare_v2(database_object , sql_stmt, -1, &statement, NULL) != SQLITE_OK) 
     NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database_object)); 
    if(SQLITE_DONE != sqlite3_step(statement)) 
     NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database_object)); 
    sqlite3_finalize(statement); 
} 

轉到與此模板,我認爲它會爲你工作...