Swift3 LKDBHelper使用

在OC项目中我一直使用LKDBHelper这个数据库框架,深度封装FMDB,使用起来感受不到数据库的存在,但又会让你更直观的了解表关系,总体来说是一个很棒的框架。在这也不作过多介绍,直接上代码。


  • 因为LKDBHelper是OC库,虽加了Swift支持,但在Swift3中完全封闭了dispatch_once的单例机制,暂且找不到重写OC单例的方法,致使不能够达到自定义数据库路径和数据库名的情况出现。所以我暂时采用的是OC跟Swift混编,以OC类作为基类,Swift类继承OC类的方法在OC类中重写类方法getUsingLKDBHelper从而实现能够获取LKDBHelper单例的办法暂时解决。如有更好的解决方法欢迎各路大牛指点。

创建OCbase类,系统自动生成桥接文件

#import "ocBaseModel.h"
#import "LKDBHelper.h"
@implementation ocBaseModel
//重写父类方法
+ (LKDBHelper *)getUsingLKDBHelper
{
    static LKDBHelper* db;
    //创建helper单例
    static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSString *sqlitePath = [ocBaseModel downloadPath];
            NSString *dbpath = [sqlitePath stringByAppendingPathComponent:[NSString stringWithFormat:@"JCMsgDB.db"]];
            db = [[LKDBHelper alloc]initWithDBPath:dbpath];
            
        });

    return db;
}
//拼接数据库保存路径
+ (NSString *)downloadPath
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *downloadPath = [documentPath stringByAppendingString:@""];
    
    NSLog(@"downloadPath = %@---",downloadPath);
    
    return downloadPath;
}

桥接文件,Cocopod已经支持swift桥接,不需在项目桥接文件导入

#ifndef addressBook_Bridging_Header_h
#define addressBook_Bridging_Header_h

#import "ocBaseModel.h"

#endif

创建SwiftContact类继承自ocBaseModel,并重写

import UIKit

class contactDBModel: ocBaseModel {

    var contactID:Int = 0
    
    var name:String?
    
    var phoneNum:String?
    
    
    init(name:String,phoneNum:String) {
        
        
        self.name = name
        self.phoneNum = phoneNum
        super.init()
        
    }
    
    //重写父类init方法
    override init() {
        super.init()
    }
    
    override class func getPrimaryKey() -> String {
        return "contactID"
    }
    override class func getTableName() -> String {
        return "contactList"
    }
    override class func isContainParent() -> Bool{
        return true
    }

}

简单CRUD操作

import UIKit
import LKDBHelper
class mainViewController: UIViewController {

    var dbHelper:LKDBHelper?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //基本使用
        
        //通过类方法获取helper单例
        self.dbHelper = contactDBModel.getUsingLKDBHelper()
        
        //增
        var contact1 = contactDBModel(name: "张三", phoneNum: "13888889998")
            //插入
        contact1.saveToDB()
        
        //查
        let searchResultArray = contactDBModel.search(withWhere:["name":"张三"], orderBy: nil, offset: 0, count: 0);
        
        //改
        var contactResult:contactDBModel = searchResultArray?.firstObject as! contactDBModel
        contactResult.name = "李四"
        contactResult.updateToDB()
        
        //删
        let searchResultArray1 = contactDBModel.search(withWhere:["phoneNum":"13888889998"], orderBy: nil, offset: 0, count: 0);
        var contactResult1:contactDBModel = searchResultArray?.firstObject as! contactDBModel
        contactResult1.deleteToDB()
  
                  }
            }

gitHub-- https://github.com/cenzuen/swiftAddressBook.git
欢迎指点

你可能感兴趣的:(Swift3 LKDBHelper使用)