需要正确的Sql Stmt

时间:2013-03-02 13:25:26

标签: ios objective-c sql

我的每个事情都有Sql数据库

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

我用它来插入,但我是如何但条件

只有我需要说出插入联系人列的Sql语句modifiedAdress Value hemainsert (condition hemaInsert = ID )

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

2 个答案:

答案 0 :(得分: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);


}

答案 1 :(得分:0)

如果要插入整行/元组/记录。

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS where modifiedAdress is '%@'", hemaInsert];//you can go for " or ' as per oracle or other sqls.


NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS where modifiedAdress = \"%@\"", hemaInsert];//you can go for " or ' as per oracle or other sqls.

编辑:

INSERT用于添加元组(行)。如果您只想在特定列中插入,则需要使用更新

语法为:

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

使用:

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