2014-02-12 4 views
1

사용자의 세부 정보를 가져 와서 SQLite db에 저장하는 응용 프로그램이 있습니다. 또한 세부 정보를 찾아 결과를 표시 할 수 있습니다. 위의 코드는 잘 작동하고업데이트 된 행을 찾을 수 없습니다 SQLite IOS

- (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); 
} 

}

-(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)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 관계를 가지고)

편집을 이 내 DB 방법 을 만들 - (무효) 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) 보낸 사람 {} 방법 [제 3 코드 블록을] ..

을 당신은 확인하므로 제대로 작동 2 개 블록 위에 말했다.

내가 여기

내가 u는 무엇을했는지 위선적 인 말투 .. 당신이하고 saveData 방법 업데이트 방법을 포함, 즉 당신이 삽입의 논리 업데이트의 논리를 내장 생각, 그래서 대신 코드로 여기 메신저 업데이트에 대한 템플릿을 제공 쿼리, 바로이 템플릿으로 귀하의 코드 ...

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); 
} 

이동을 넣어, 나는 당신을 위해 ...

작동합니다 생각
관련 문제