2013-03-02 74 views
0

我有SQL數據庫的每一件事工作的權利需要正確的SQL語句

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS where (modifiedAdress) VALUES (\"%@\")", hemaInsert]; 

我用這個插入我多麼但條件

只有我需要的SQL語句,上述嵌件接觸柱modifiedAdress價值hemainsert (condition hemaInsert = ID)

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS where (modifiedAdress) VALUES (\"%@\")", hemaInsert]; 
+0

這是一個非常不好的做法是使用字符串格式創建的SQL語句。如果替換值具有某些字符,則會發生不好的事情。這可能會導致查詢失敗。這也是許多SQL注入攻擊的基礎。查看iPatel的答案,將正確的方法綁定到準備好的語句中。 – rmaddy 2013-03-02 17:07:56

回答

0

如果要插入一個整行/元組/記錄。

​​

編輯:

INSERT用來添加一元組(行)。如果您只想插入某一列,則需要使用UPDATE

的語法是:

UPDATE tableName 
SET column1=value, column2=value2,... 
WHERE some_column=some_value 

用途:

NSString *insertSQL = [NSString stringWithFormat: @"UPDATE CONTACTS SET modifiedAdress = \"%@\"", hemaInsert]; 
+0

我需要插入特定列(modifiedAdress),而條件(hema = ID)插入表聯繫人 – 2013-03-02 13:33:20

+0

插入插入一個元組(行)。如果您只想在特定列中插入,則需要使用UPDATE。 – 2013-03-02 13:36:10

+0

我該如何在這種情況下使用 – 2013-03-02 13:38:48

1
- (void) InserRecorinTable:(NSString*) hemaInsert 
{ 


     int ret; 
     const char *sql = "insert into CONTACTS (modifiedAdress) values (?);"; 

     sqlite3_stmt *insStmt = NULL; 
     if (!insStmt) 
      if ((ret = sqlite3_prepare_v2(_database, sql, -1, &insStmt, NULL)) != SQLITE_OK) {} 

     // bind values 
     sqlite3_bind_text(insStmt, 1, hemaInsert, -1, SQLITE_TRANSIENT);     

     if ((ret = sqlite3_step(insStmt)) != SQLITE_DONE) {NSLog(@"error while inserting marker");} 
     sqlite3_reset(insStmt); 


} 
+0

+1不使用字符串格式,而是正確使用'sqlite3_bind_xxx'。 – rmaddy 2013-03-02 17:05:31

+0

但是,你應該花時間更新你的回答使用OP所使用的查詢和變量。 – rmaddy 2013-03-02 17:06:21