2013-03-02 157 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