本地存储Sqlite的用法:

本地存储Sqlite的用法:
文章图片

本地存储Sqlite的用法:
文章图片

.h文件声明:
【本地存储Sqlite的用法:】// 把这个类写成单例, 方便外部使用
+ (DataBaseHandle *)shareDataBaseHandle;

//打开数据库
- (void)openDB;
// 关闭数据库
- (void)closeDB;
// 创建表
- (void)createTable;

// 插入数据
- (void)insertName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age;

// 通过uid去更新数据
- (void)updateWithUID:(NSInteger)uid;

// 通过uid去删除数据
- (void)deleteWithUID:(NSInteger)uid;

// 查找所有数据
- (void)searchAll;

// 根据名字去查找相关的数据信息
- (void)searchWithName:(NSString *)name;
====================================== .m 实现
// 引入数据库操作的头文件
#import
// 延展
@interface DataBaseHandle ()

// 数据库的存储路径
@property (nonatomic, copy) NSString *dbPath;

@end

static DataBaseHandle *dataBase = nil;

@implementation DataBaseHandle


+ (DataBaseHandle *)shareDataBaseHandle
{
if (dataBase == nil) {
dataBase = [[DataBaseHandle alloc] init];

}

return dataBase;
}

// 懒加载需要给数据库路径赋值
- (NSString *)dbPath
{
if (_dbPath == nil) {
// 需求: 路径存储在Documents文件夹下,数据库文件为person.sqlite
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
_dbPath = [documentPath stringByAppendingPathComponent:@"person.sqlite"];
}
return _dbPath;
}
// 好多地方都会使用到数据库,所以初始化一个数据库的静态变量
static sqlite3 *db = nil;
//打开数据库
- (void)openDB
{
//打开数据库,使用int去接受打开的结果
//第一个参数: filename代表数据库的存储路径
//第二个参数: 二级指针,数据库的地址
int result = sqlite3_open([self.dbPath UTF8String], &db);
//result是个枚举值,有很多种情况
if (result == SQLITE_OK) {
NSLog(@"数据库打开成功");
} else {
NSLog(@"数据库打开失败");
}
}

// 关闭数据库
- (void)closeDB
{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"数据库关闭成功");
} else {
NSLog(@"数据库关闭失败");
}
}

// 创建一个表
- (void)createTable
{
// 创建一个person表,字段: uid integer类型主键 自增 不能为空, name text类型,gender text类型,age integer类型
NSString *createStr = @"create table if not exists person(uid integer primary key autoincrement not null,name text, gender text, age integer)";
// 第一个参数: 数据库
// 第二个参数: sql语句, 需要进行编码
// 第三个参数: 结果回调的一个函数
// 第四个参数: 回调函数的一个参数
// 第五个参数: 错误信息
int result = sqlite3_exec(db, [createStr UTF8String], NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败");
}
// 打印数据库的路径,检查表是否创建成功

NSLog(@"_dbPath = %@", self.dbPath);

}


// 插入数据
- (void)insertName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age
{
//当values不确定的情况下使用?代替,之后会进行其绑定的过程
NSString *insertStr = @"insert into person(name,gender,age)values(?,?,?)";
//伴随指针
sqlite3_stmt *stmt = nil;
// 预执行语句
// 第一个参数: 数据库
// 第二个参数: sql语句
// 第三个参数: 有正负之分,例如:1,代表只往后读一个字节,如果为负值,遇到特殊符号才会结束读取(\000,u000)
// 第四个参数: 伴随指针,会随着数据库的相关操作确定其中?的值
// 第五个参数: 取值的时候取不全,剩下的值都存在这里
int result = sqlite3_prepare(db, insertStr.UTF8String, -1, &stmt, NULL);
// 判断执行结果
if (result == SQLITE_OK) {
//在操作成功的方法里进行?值的一些绑定设置
// 第一个参数: 伴随指针
// 第二个参数: ?的位置,从1开始
// 第三个参数: 表示要插入的值
// 第四个参数: 有正负之分,例如:1,代表只往后读一个字节,如果为负值,遇到特殊符号才会结束读取(\000,u000)
// 第五个参数: 回调函数
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
sqlite3_bind_text(stmt, 2, gender.UTF8String, -1, NULL);
sqlite3_bind_int64(stmt, 3, age);

//sql语句执行完毕
// 执行伴随指针,根据伴随指针的情况判定是否插入成功[SQLITE_DONE代表伴随指针执行数据成功]
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"插入成功");
} else {
NSLog(@"插入失败");
}

} else {

NSLog(@"result = %d", result);
}
// 一定要释放伴随指针
sqlite3_finalize(stmt);

}

// 通过uid去更新数据
- (void)updateWithUID:(NSInteger)uid
{
NSString *updateStr = @"update person set name = '悟空' where uid = ?";
// 预执行语句
//伴随指针
sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare(db, updateStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
sqlite3_bind_int64(stmt, 1, uid);
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"更新数据成功");

} else {
NSLog(@"更新数据失败");
}

} else {
NSLog(@"result = %d", result);
}
// 一定要释放伴随指针
sqlite3_finalize(stmt);
}

// 通过uid去删除数据
- (void)deleteWithUID:(NSInteger)uid
{
NSString *deleteStr = [NSString stringWithFormat:@"delete from person where uid = %ld", uid];
int result = sqlite3_exec(db, deleteStr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
} else {
NSLog(@"删除失败");
}
}

// 查找所有数据
- (void)searchAll
{
NSString *searchAllStr = @"select *from person";
// 预执行
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare(db, searchAllStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
// 查询数据的时候当不知道执行多少次的时候使用while循环
while (sqlite3_step(stmt) == SQLITE_ROW) {
// 第一个参数: 伴随指针
// 第二个参数: 代表这个字段的位置[只有带? 的是从1开始,其余所有都是从0开始]
int uid = sqlite3_column_int(stmt, 0);
NSLog(@"uid = %d", uid);
//在OC代码中要使用C语言的相关内容应该使用UTF8String
NSString *name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NSLog(@"name = %@", name);
NSString *gender = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 2)];
NSLog(@"gender = %@", gender);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"age = %d", age);

}
} else {
NSLog(@"result = %d", result);
}

sqlite3_finalize(stmt);

}

// 根据名字去查找相关的数据信息
- (void)searchWithName:(NSString *)name
{
NSString *selectStr = @"select uid,gender,age from person where name = ?";

sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare(db, selectStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
while (sqlite3_step(stmt) == SQLITE_ROW) {

int uid = sqlite3_column_int(stmt, 0);
NSString *gender = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
int age = sqlite3_column_int(stmt, 2);
NSLog(@"++++uid = %d,gender = %@,age = %d",uid,gender,age);
}
}

sqlite3_finalize(stmt);

}



转载于:https://www.cnblogs.com/leikun1113/p/5488122.html

    推荐阅读