ios sqlite3多线程操作

记录下

单例的宏使用,是为了简化及统一,使用,参考前一篇文章或是去网上找相关的代码。
部分代码参考自互联网,主要是加了pthread_mutex_t, 保证线程安全, 在openDb, closeDb中lock, unlock保证安全,因此在操作数据库时,保证调用openDb, closeDb,成对使用,不要去考虑在主线程,还是其它线程中使用数据库了。
只是提供一个框架和示例,具体应用根据自己的需要去添加。

头文件

#import <Foundation/Foundation.h>
#include "SingletonMacro.h"

@interface VRSQLite : NSObject

HFSingletonH(VRSQLite)

@end

实现文件


#import "VRSQLite.h"
#import <sqlite3.h>  
#include <pthread.h>
@interface VRSQLite ()
{
    //数据指针,通过指针可以操作对应的数据库
    sqlite3 *dbPoint ;
    pthread_mutex_t  mutex;
}
@end

@implementation VRSQLite
HFSingletonM(VRSQLite)

-(instancetype) init{
    self = [super init];
    if(self){
        [self setup];
    }
    return self;
}

- (void) setup{
    pthread_mutex_init(&mutex, NULL);
    [self createTable];
}
//打开数据库

-(void)openDb{
    pthread_mutex_lock(&mutex);
    //想要打开的数据可路径
    NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"vr.db"];

    NSLog(@"%@",dbPath);

    /*
     参数1:想要打开的数据库的路径,需要的是C语言的字符串
     参数2:将dbPoint 指针和数据库绑定,通过dbPoint可以访问该路径下的数据库

     如果该路径下不存在对应的数据库,系统会自动创建一个数据库
     */
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (SQLITE_OK == result ) {
        NSLog(@"数据库打开成功");
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
}


//关闭数据库
-(void)closeDb{

    int result = sqlite3_close(dbPoint) ;

    [self judgeWithResult:result action:@"关闭数据库"];
    pthread_mutex_unlock(&mutex);
}
-(void)judgeWithResult:(int)result action:(NSString *)actionStr{

    if (result == SQLITE_OK) {
        NSLog(@"%@成功",actionStr);
    }
    else
    {
        NSLog(@"%@失败",actionStr);
    }
}

//创建表
-(void)createTable{
    [self openDb];
    NSString *sqlStr = @"create table vritem (localIdentifier text , media_id text )";

    /**
     *  参数1:要使用的是哪一个数据库
     *  参数2:想对数据做什么操作 SQL语句
     *  参数3/4:系统预留的参数
     *  参数5:错误信息
     *
     *  @return return value description
     */

    char **error ;
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);
    NSLog(@"%s",error);

    [self judgeWithResult:result action:@"创建表"];

    //销毁指针
    sqlite3_free(error) ;

    [self closeDb];

}

//添加

-(void)insertItem:(PTCloudSqlModel *)stu{

    [self openDb];

    NSString *sqlStr = [NSString stringWithFormat:@"insert into vritem values ('%@','%@')",stu.localIdentifier,stu.media_id];

    char**error ;

    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);

    [self judgeWithResult:result action:@"插入"];

    sqlite3_free(error);

    [self closeDb];

}


//删除记录

-(void)deleteItem:(PTCloudSqlModel *)stu{

    [self openDb];

    NSString *sqlStr = [NSString stringWithFormat:@"delete from vritem where localIdentifier = \'%@' ",stu.localIdentifier];
    char **error ;

    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, &error);

    [self judgeWithResult:result action:@"删除"];

    sqlite3_free(error);

    [self closeDb];

}



@end
发布了121 篇原创文章 · 获赞 7 · 访问量 16万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章