sqlite3最最基础用法小结
使用SQLite需要以下几个步骤:
- 引用SQLite库
- 声明sqlite3变量来保存对数据库的引用
- 使用sqlite3_open函数打开数据库
- 使用sqlite3_prepare_v2函数预处理SQL语句,将查询语句转换为二进制数据,加快处理速度
- 使用sqlite3_bind_text函数绑定参数
- 使用sqlite3_step函数执行SQL语句,遍历结果集
- 使用sqlite3_column_text等函数提取字段数据
- 使用sqlite3_finalize释放SQL语句对象(sqlite3_stmt)
- 使用sqlite3_close函数关闭数据库
sqlite3 *db;
sqlite3_stmt *statement;
sqlite3_open(存储路径,&db);
sqlite3_prepare_v2(db, sql语句, &statement, NULL);
sqlite3_bind_text(statement, 1, 要绑定的数据, -1, NULL);
sqlite3_finalize(statement);
sqlite3_close(db);
示例代码
创建表
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"RunRecord.db"];
const char *cPath = [path UTF8String];
if (sqlite3_open(cPath, &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO, @"数据库打开失败");
} else {
char *error;
NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS Record (cdate TEXT PRIMARY KEY, coordinate TEXT);"];
const char *cSql = [sql UTF8String];
if (sqlite3_exec(db, cSql, NULL, NULL, &error) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO, @"建表失败");
}
sqlite3_close(db);
}
查询指定行数据
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"RunRecord.db"];
const char *cPath = [path UTF8String];
if (sqlite3_open(cPath, &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO, @"数据库打开失败");
} else {
NSString *sql = @"select cdate, coordinate from Note where cdate = ?";
const char *cSql = [sql UTF8String];
sqlite3_stmt *statement;
// 将查询语句转换为二进制
if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) {
NSString *date = @"2018/1/24";
const char *cdate = [date UTF8String];
// 绑定参数
sqlite3_bind_text(statement, 1, cdate, -1, NULL);
// 执行查询
if (sqlite3_step(statement) == SQLITE_ROW) {
// 取数据
char *bufDate = (char *)sqlite3_column_text(statement, 0);
NSString *strDate = [[NSString alloc] initWithUTF8String:bufDate];
char *bufCoordinate = (char *)sqlite3_column_text(statement, 1);
NSString *strCoordinate = [[NSString alloc] initWithUTF8String:bufCoordinate];
sqlite3_finalize(statement);
sqlite3_close(db);
// 此处省略返回取得的数据 return
}
sqlite3_finalize(statement);
sqlite3_close(db);
// 查询失败,返回nil
}
查询多行数据
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"RunRecord.db"];
const char *cPath = [path UTF8String];
if (sqlite3_open(cPath, &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO, @"数据库打开失败");
} else {
NSString *sql = @"select cdate, coordinate from Note;
const char *cSql = [sql UTF8String];
sqlite3_stmt *statement;
// 预处理过程,将查询语句转换为二进制
if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) {
// 不需要绑定参数
// 执行查询
while (sqlite3_step(statement) == SQLITE_ROW) {
// 取数据
char *bufDate = (char *)sqlite3_column_text(statement, 0);
NSString *strDate = [[NSString alloc] initWithUTF8String:bufDate];
char *bufCoordinate = (char *)sqlite3_column_text(statement, 1);
NSString *strCoordinate = [[NSString alloc] initWithUTF8String:bufCoordinate];
// 将查到的数据添加到数组。
}
}
sqlite3_finalize(statement);
sqlite3_close(db);
}
return 数据数组
删除指定行数据
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"RunRecord.db"];
const char *cPath = [path UTF8String];
if (sqlite3_open(cPath, &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO, @"数据库打开失败");
} else {
NSString *sql = @"delete from Note where cdate = ?";
const char *cSql = [sql UTF8String];
sqlite3_stmt *statement;
// 将查询语句转换为二进制
if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) {
NSString *date = @"2018/1/24";
const char *cdate = [date UTF8String];
// 绑定参数
sqlite3_bind_text(statement, 1, cdate, -1, NULL);
// 执行删除操作
if (sqlite3_step(statement) != SQLITE_DONE) {
NSAssert(NO, @"删除数据失败");
}
sqlite3_finalize(statement);
sqlite3_close(db);
}
修改数据
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"RunRecord.db"];
const char *cPath = [path UTF8String];
if (sqlite3_open(cPath, &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO, @"数据库打开失败");
} else {
NSString *sql = @"update Note set coordinate = ? where cdate = ?";
const char *cSql = [sql UTF8String];
sqlite3_stmt *statement;
// 将查询语句转换为二进制
if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) {
NSString *date = @"2018/1/24";
const char *cdate = [date UTF8String];
NSString *coordinate = @"235.33232,-56,34566";
const char *cCoordinate = [coordinate UTF8String];
// 绑定参数
sqlite3_bind_text(statement, 1, cCoordinate, -1, NULL);
sqlite3_bind_text(statement, 2, cdate, -1, NULL);
// 执行插入
if (sqlite3_step(statement) != SQLITE_DONE) {
NSAssert(NO, @"更新数据失败");
}
sqlite3_finalize(statement);
sqlite3_close(db);
}