3.sqlite3
打开数据库:
sqlite3 *database;
int result = sqlite3_open([pathString UTF8String],&database);
关闭数据库
sqilte3_close(database);
创建新表
char *errorMsg;
congest char *createSQL = "create table if not exists people (ID integer primary key, name text)";
int result = sqlite3_exec(database,createSQL,NULL,NULL,&errorMsg);
查询设置
NSString *query = @"SELECT ROW, FIELD_DATA FROM FIELDS ORDER BY ROW";
sqlite3_stmt = statement;
if(sqlite3_prepare_v2(database, [query UTF8String],
-1, &statement, nil) == SQLITE_OK){
while(sqlite3_step(statement == SQLITE_OK){
int row = sqlite3_column_int(statement, 0);
char *rowData = (char *)sqlite3_column_text(statement, 1);
NSString *fieldValue = [[NSString alloc]
initWithUTF8String:rowData];
((UITextField *)self.lineFields[row]).text = fieldValue;
}
sqlite3_finalize(statement);
}
增:
sqlite3_stmt *statement = nil;
NSString *sql = [NSString stringWithFormat:@"INSERT INTO region (id, parent_id, level, region_name, sort) VALUES (\"%d\",\"%d\",\"%d\",\"%@\",\"%d\");",
region.rid, region.parentId, region.level, region.name, region.sort];
sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL);
sqlite3_step(statement) ;
sqlite3_finalize(statement);
删:
NSString *sql = [NSString stringWithFormat:@"DELETE FROM region WHERE id = \"%d\";", (int)rid];
sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL);
sqlite3_step(statement) ;
sqlite3_finalize(statement);
批量增改:
char *update = "INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA) "
"VALUES (?, ?);";
sqlite3_stmt *stmt;
sqlite3_prepare_v2(database, update, -1, &stmt, nil);
for (int i = 0; i < 4; i++) {
UITextField *field = self.lineFields[i];
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, i);
sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);