sqlite增删改查功能封装管理类

该文档代码书写在.m文件中。
/*
1.添加依赖库 libsqlist3.0.tbd
2.导入sqlite3.h头文件
*/
创建单例对象:

  • (DataBaseHelper *)sharedHelper {
    static DataBaseHelper *helper = nil;
    @synchronized(self) {
    if (!helper) {
    helper = [[DataBaseHelper alloc] init];
    [helper createTable];
    }
    }
    return helper;
    }

//数据库文件路径

  • (NSString *)dataBaseFilePath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"data.sqlite"];
    }

//数据库操作对象
static sqlite3 *db = NULL;

//打开数据库

  • (void)openDataBase {
    //参数1:底层数据库文件的沙盒路径
    //参数2:sqlite3类型的指针的指针
    /*函数的作用:
    ①创建一个数据库对象
    ②在给定路径上面若没有文件,创建一个文件并打开,有文件直接打开
    */
    //优化:对于一次数据库操作,只打开一次数据库就可以了
    if (db) {
    return;//如果不为空,说明之前的数据库已打开
    }
    int result = sqlite3_open([self dataBaseFilePath].UTF8String, &db);
    if (result == SQLITE_OK) {
    NSLog(@"数据库打开成功");
    }else{
    NSLog(@"数据库打开失败");
    }
    }

//关闭数据库

  • (void)closeDataBase {
    int result = sqlite3_close(db);
    if (result == SQLITE_OK) {
    NSLog(@"关闭成功");
    }else{ NSLog(@"关闭失败");
    }
    }

//创建表

  • (void)createTable {
    //1.打开数据库
    [self openDataBase];
    //2.创建SQL语句
    NSString sqlStr = @"create table if not exists Contact(con_id integer primary key autoincrement not null, con_name text default '无名氏', con_age text, con_phone text, con_photo blob)";
    //3.执行sql语句
    //sqlite3_exec():sqlite数据中最简单的执行函数,当前执行的是DDL类型的SQL
    /
    参数:
    ①数据库对象 ②sql语句字符串(C语言) ③回调函数的指针 ④回调函数参数 ⑤错误信息
    */
    char *error = nil;
    int result = sqlite3_exec(db, [sqlStr UTF8String], NULL, NULL, &error);
    if (result == SQLITE_OK) {
    NSLog(@"创建表成功");
    } else {
    NSLog(@"创建表失败 err:%s", error);
    }
    //4.关闭数据库
    [self closeDataBase];
    }

/***************** 添加 ******************/
//添加一条数据

  • (void)insertContactWith:(Contact *)contact {
    //1.打开数据库
    [self openDataBase];
    //2.创建sql语句
    NSString *sqlStr = @"insert into Contact(con_id, con_name, con_age, con_phone, con_photo) values(?, ?, ?, ?, ?)";
    // 第二种创建sql语句方式
    //NSString *str = [NSString stringWithFormat:@"insert into Contact(con_id, con_name, con_age, con_phone, con_photo) values(%ld, '%@', '%@', '%@', '%@')", contact.con_id, contact.name, contact.age, contact.phone, UIImagePNGRepresentation(contact.photo)];
    //3.创建指令集对象
    sqlite3_stmt stmt = NULL;
    /

    参数: ①数据库对象 ②sql语句(C语言字符串) ③一般给 -1:长度不限制 ④指向指令集的指针 ⑤预留参数(方便日后扩展)
    /
    int result = sqlite3_prepare_v2(db, sqlStr.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
    //4.绑定参数
    /

    参数: ①指令集对象 ②绑定第几个 ③绑定的值
    /
    sqlite3_bind_int(stmt, 1, (int)contact.con_id);
    /

    参数: ④文本长度 -1不限制长度 自动计算 ⑤函数指针 回调函数指针
    */
    sqlite3_bind_text(stmt, 2, contact.name.UTF8String, -1, NULL);
    sqlite3_bind_text(stmt, 3, contact.age.UTF8String, -1, NULL);
    sqlite3_bind_text(stmt, 4, contact.phone.UTF8String, -1, NULL);
    //UIImage -> NSData
    NSData *imgData = UIImagePNGRepresentation(contact.photo); sqlite3_bind_blob(stmt, 5, imgData.bytes, (int)imgData.length, NULL);
    //5.执行指令集
    int result1 = sqlite3_step(stmt);
    if (result1 == SQLITE_DONE) {
    NSLog(@"数据插入成功");
    } else {
    NSLog(@"数据插入失败");
    }
    }
    //6.关闭指令集 释放资源
    sqlite3_finalize(stmt);
    //7.关闭数据库
    [self closeDataBase];
    }

/*************** 查询数据 ******************/
//查询数据

  • (NSMutableArray *)selectDataBase {
    //1.打开数据库
    [self openDataBase];
    //2.创建sql语句 -查询
    NSString *sqlStr = @"select *from Contact";
    //3.生成指令集
    sqlite3_stmt *stmt = NULL;
    int result = sqlite3_prepare_v2(db, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
    //4.执行
    //sqlite数据库会根据执行的结果result来判断,数据库中满足条件的,当执行结果为SQLITE_ROW时,说明下面还有满足条件的数据,接着检索。若不为此结果,循环结束
    while (sqlite3_step(stmt) == SQLITE_ROW) {
    //一次循环就代表有一条数据
    NSInteger con_id = sqlite3_column_int(stmt, 0);
    NSString *name = [[NSString alloc] initWithCString:(const char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
    NSString *age = [[NSString alloc] initWithCString:(const char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];
    NSString *phone = [[NSString alloc] initWithCString:(const char *)sqlite3_column_text(stmt, 3) encoding:NSUTF8StringEncoding];
    //获取头像
    const void *blob = sqlite3_column_blob(stmt, 4);
    //获取二进制数据的长度
    NSUInteger length = sqlite3_column_bytes(stmt, 4);
    NSData *data = [NSData dataWithBytes:blob length:length];
    UIImage *image = [UIImage imageWithData:data scale:1.0];
    //创建一个contact对象
    Contact *con = [[Contact alloc] init];
    con.con_id = con_id;
    con.name = name;
    con.age = age;
    con.phone = phone;
    con.photo = image;
    //存储到数组中
    [array addObject:con];
    }
    NSLog(@"查询成功");
    }
    //5.关闭指令集 释放资源
    sqlite3_finalize(stmt);
    //6.关闭数据库
    [self closeDataBase];
    //返回数组
    return array;
    }

/**************** 删除数据 ****************/
//删除数据

  • (void)deleteWithConId:(Contact *)contact {
    //1.打开数据库
    [self openDataBase];
    //2.sql语句
    NSString *sqlStr = @"delete from Contact where con_id = ?";
    //3.创建指令集
    sqlite3_stmt *stmt = NULL;
    int result = sqlite3_prepare_v2(db, sqlStr.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
    //4.绑定参数(有问号、需绑定参数)
    int bindValue = sqlite3_bind_int(stmt, 1, (int)contact.con_id);
    if (bindValue == SQLITE_OK) {
    //5.执行删除
    int result = sqlite3_step(stmt);
    if (result == SQLITE_DONE) {
    NSLog(@"删除成功");
    }else{
    NSLog(@"删除失败");
    }
    }
    }
    //6.关闭指令集 释放资源
    sqlite3_finalize(stmt);
    //7.关闭数据库
    [self closeDataBase];
    }

/***************** 更新数据 *******************/
//更新数据,修改多条数据可在sql语句中添加多个参数

  • (void)updateDataWithContact:(Contact *)contact {
    //1.打开数据库
    [self openDataBase];
    //2.sql语句
    NSString *sqlStr = @"update Contact set con_name = ?, con_phone = ? where con_id = ?";
    //3.生成指令集
    sqlite3_stmt *stmt = NULL;
    int result = sqlite3_prepare_v2(db, sqlStr.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
    //4.绑定参数
    sqlite3_bind_text(stmt, 1, contact.name.UTF8String, -1, NULL);
    sqlite3_bind_text(stmt, 2, contact.phone.UTF8String, -1, NULL);
    sqlite3_bind_int(stmt, 3, (int)contact.con_id);
    //5.执行
    int res = sqlite3_step(stmt);
    if (res == SQLITE_DONE) {
    NSLog(@"更新成功");
    }
    }
    //6.关闭指令集 释放资源
    sqlite3_finalize(stmt);
    //7.关闭数据库
    [self closeDataBase];
    }

你可能感兴趣的:(sqlite增删改查功能封装管理类)