初步接触了SQLite3,记录下使用方法
打开数据库
sqlite3_open(dataBasePathStr.UTF8String, &dataBase);
关闭数据库
sqlite3_close(dataBase);
无返回数据的操作
char *errs;
if(sqlite3_exec(dataBase, sql.UTF8String, NULL, NULL, &errs) == SQLITE_OK)
{
NSLog(@"OK");
}
else
{
NSLog(@"Error:%s", errs);
isSuccess = NO;
}
有数据返回的操作
sqlite3_stmt *statment;
if(sqlite3_prepare(dataBase, sql.UTF8String, -1, &statment, NULL) == SQLITE_OK)
{
//所有的数据
while(sqlite3_step(statment) == SQLITE_ROW)
{
//输出所有的字段名和对应的值
for(int col = 0; col < sqlite3_column_count(statment); col++)
{
NSString *key = [NSString stringWithFormat:@"%s", sqlite3_column_name(statment, col)];
NSString *value = [NSString stringWithFormat:@"%s", sqlite3_column_text(statment, col)];
NSLog(@"%@:%@", key, value);
}
}
}
使用数据库前需要打开数据库,同时使用完毕别忘了关闭数据库。