sqlite3 带占位符插入数据

//用问号形式插入数据

-(void) insertRecord:(NSString *)table

                  withValue1:(NSString*) value1

                  withValue2:(NSString*) value2

                  withValue3:(NSString*) value3

{

    NSString * sql = [NSStringstringWithFormat:@"insert into members('name','email','birthday') values (?,?,?)"];

   sqlite3_stmt *statement;

    

   if(sqlite3_prepare_v2(db, [sqlUTF8String], -1, &statement,nil) ==SQLITE_OK){

       sqlite3_bind_text(statement,1, [value1 UTF8String], -1,NULL);

       sqlite3_bind_text(statement,2, [value2 UTF8String], -1,NULL);

       sqlite3_bind_text(statement,3, [value3 UTF8String], -1,NULL);

    }

   if (sqlite3_step(statement) !=SQLITE_DONE) {

       NSAssert(0,@"插入数据失败");

    }

   sqlite3_finalize(statement);

}


sqlite3_bind_text 是关键 ,第一个参数为sqlite3_stmt对象,第二个参数是问号的索引,第三个参数是替换后的值,第四个参数是被替换值的长度,若小于0则自动计算其长度,第五个参数是毁掉函数,类似的还有 

SQLITE_APIint sqlite3_bind_blob(sqlite3_stmt*,int,const void*,int n, void(*)(void*));

SQLITE_APIint sqlite3_bind_double(sqlite3_stmt*,int,double);

SQLITE_APIint sqlite3_bind_int(sqlite3_stmt*,int,int);

SQLITE_APIint sqlite3_bind_int64(sqlite3_stmt*,int,sqlite3_int64);

SQLITE_APIint sqlite3_bind_null(sqlite3_stmt*,int);

SQLITE_APIint sqlite3_bind_text(sqlite3_stmt*,int,const char*,int n, void(*)(void*));

SQLITE_APIint sqlite3_bind_text16(sqlite3_stmt*,int,const void*,int, void(*)(void*));

SQLITE_APIint sqlite3_bind_value(sqlite3_stmt*,int,const sqlite3_value*);

SQLITE_APIint sqlite3_bind_zeroblob(sqlite3_stmt*,int,int n);


你可能感兴趣的:(ios与SQLite3)