Coder Social home page Coder Social logo

li6185377 / lkdbhelper-sqlite-orm Goto Github PK

View Code? Open in Web Editor NEW
1.2K 1.2K 287.0 1.18 MB

全自动的插入,查询,更新,删除, an automatic database operation thread-safe and not afraid of recursive deadlock

License: MIT License

Objective-C 97.19% Swift 2.42% Ruby 0.17% Rich Text Format 0.22%

lkdbhelper-sqlite-orm's People

Contributors

aelam avatar amjunliang avatar coderzhuxh avatar dehengxu avatar li6185377 avatar nathanli avatar satgi avatar zhangao0086 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lkdbhelper-sqlite-orm's Issues

如何更新db呢

比如 实体的字段 增加了....但是之前的表并没有....只能删除?重建?这样的话数据就没有了.

BLOB data

Hi,
I have imported an sqlite database into my project, the database has a column of BLOB type(it's an image), I made a variable of NSDate type to receive that column data, however, it's returning (null). Can you please add support of the BLOB type?

Thank you very much.

模型里面有一个数组,数组里是另外一个模型如何处理?

2016-01-04 11:52:44.061 MNCats[8575:864551] Bugly: BlockMonitor: monitor is stop.
2016-01-04 11:52:44.061 MNCats[8575:864551] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WorkSysTimeModel countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7fb50593abe0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f94ce65 exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000110ad5deb objc_exception_throw + 48
2 CoreFoundation 0x000000010f95548d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010f8a290a __forwarding
+ 970
4 CoreFoundation 0x000000010f8a24b8 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010f30c1f4 -[NSObject(NSKeyValueCoding) setValuesForKeysWithDictionary:] + 139
6 MNCats 0x000000010c3d746d -[BaseModel initWithDic:] + 141
7 MNCats 0x000000010c2fd387 -[WorkSysModel setValue:forKey:] + 487
8 MNCats 0x000000010c4a6d38 -[NSObject(LKModel) modelSetValue:value:] + 3368
9 MNCats 0x000000010c49ba7b -[LKDBHelper(DatabaseExecute) executeResult:Class:tableName:] + 1067
10 MNCats 0x000000010c49abac __56-[LKDBHelper(DatabaseExecute) searchWithRAWSQL:toClass:]_block_invoke + 140
11 MNCats 0x000000010c492db7 -[LKDBHelper executeDB:] + 199
12 MNCats 0x000000010c49aa4e -[LKDBHelper(DatabaseExecute) searchWithRAWSQL:toClass:] + 318
13 MNCats 0x000000010c49a8d5 -[LKDBHelper(DatabaseExecute) searchWithSQL:toClass:] + 133
14 MNCats 0x000000010c4a8ab7 -[NSObject(LKModel) db_objectWithDictionary:] + 1799
15 MNCats 0x000000010c4a8112 -[NSObject(LKModel) db_modelWithJsonValue:] + 370
16 MNCats 0x000000010c4a6c71 -[NSObject(LKModel) modelSetValue:value:] + 3169
17 MNCats 0x000000010c49ba7b -[LKDBHelper(DatabaseExecute) executeResult:Class:tableName:] + 1067
18 MNCats 0x000000010c499af3 __52-[LKDBHelper(DatabaseExecute) searchBaseWithParams:]_block_invoke + 387
19 MNCats 0x000000010c4931ac __24-[LKDBHelper executeDB:]_block_invoke171 + 108
20 MNCats 0x000000010c43232c __30-[FMDatabaseQueue inDatabase:]_block_invoke + 124
21 libdispatch.dylib 0x00000001114b749b _dispatch_client_callout + 8
22 libdispatch.dylib 0x000000011149bfb5 _dispatch_barrier_sync_f_invoke + 393
23 MNCats 0x000000010c43225d -[FMDatabaseQueue inDatabase:] + 301
24 MNCats 0x000000010c492f90 -[LKDBHelper executeDB:] + 672
25 MNCats 0x000000010c499806 -[LKDBHelper(DatabaseExecute) searchBaseWithParams:] + 2838
26 MNCats 0x000000010c49a34d -[LKDBHelper(DatabaseExecute) searchBase:columns:where:orderBy:offset:count:] + 685
27 MNCats 0x000000010c4983e5 -[LKDBHelper(DatabaseExecute) search:where:orderBy:offset:count:] + 181
28 MNCats 0x000000010c4a34ee +[NSObject(LKDBHelper) searchWithWhere:orderBy:offset:count:] + 190
29 MNCats 0x000000010c2e6875 -[BMStaffVC getDBEmployee] + 69
30 MNCats 0x000000010c2e68d7 -[BMStaffVC getAllEmployee] + 55
31 MNCats 0x000000010c2e61e2 -[BMStaffVC viewDidLoad] + 354
32 UIKit 0x000000010d645f98 -[UIViewController loadViewIfRequired] + 1198
33 UIKit 0x000000010d64bf4f -[UIViewController __viewWillAppear:] + 120
34 UIKit 0x000000010d67be44 -[UINavigationController _startCustomTransition:] + 1203
35 UIKit 0x000000010d68c23f -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
36 UIKit 0x000000010d68d3af -[UINavigationController __viewWillLayoutSubviews] + 57
37 UIKit 0x000000010d833ff7 -[UILayoutContainerView layoutSubviews] + 248
38 UIKit 0x000000010d5664a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
39 QuartzCore 0x000000010d22d59a -[CALayer layoutSublayers] + 146
40 QuartzCore 0x000000010d221e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
41 QuartzCore 0x000000010d221cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
42 QuartzCore 0x000000010d216475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
43 QuartzCore 0x000000010d243c0a _ZN2CA11Transaction6commitEv + 486
44 UIKit 0x000000010d4a9f7c _UIApplicationHandleEventQueue + 7329
45 CoreFoundation 0x000000010f878a31 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
46 CoreFoundation 0x000000010f86e95c __CFRunLoopDoSources0 + 556
47 CoreFoundation 0x000000010f86de13 __CFRunLoopRun + 867
48 CoreFoundation 0x000000010f86d828 CFRunLoopRunSpecific + 488
49 GraphicsServices 0x00000001122acad2 GSEventRunModal + 161
50 UIKit 0x000000010d4af610 UIApplicationMain + 171
51 MNCats 0x000000010c3d71bf main + 111
52 libdyld.dylib 0x00000001114ec92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

不知道是不是这个原因导致的崩溃?

请问怎么给数据库表加字段

请问怎么给数据库表加字段,我直接在里面加字段,然后更新应用,有个表提示DB Error: 1 "duplicate column name: linkAccount" 一个表,没有提示也没有更新,我想问问具体要做哪些操作

如果我的属性是 double 类型的,能否在数据库中,也保存成 REAL 类型的?

我遇到的一个问题是,有个属性是表示时间戳。需要根据其对应的字段来排序,找出最晚的一条记录(想直接通过 select order by 来实现)。
如果属性是 NSDate 类型,是会被转换成 string 后再保存到数据库的吧。比较的是字符串,会导致 10001 年比 2014 年小的问题。

如果把这个属性改成 double 类型。(通过
NSDate - (NSTimeInterval)timeIntervalSince1970; 来取得这个值。)
也会被转成 string 保存到数据库吧。

如果存成 int 型,数据库中倒是会保存 int 型字段。但是要从 int 再转成 NSDate 会存在误差。这样就只能定义2个属性,一个是 NSDate 型,还有一个是 timeIntervalSince1970 后,再乘以 1000,然后取整保存,感觉上不太好。

更新:我发现了这样两行代码:
if([columnType isEqualToString:LKSQL_Type_Double])
columnType = LKSQL_Type_Text;

是什么原因需要增加上述代码的呢?请问是否可以去掉这两行代码。
在没有得到反馈之前,我只能先本地注释掉。暂时没发现有什么问题。

方法不执行

-(id)userGetValueForModel:(LKDBProperty_)property;
-(void)userSetValueForModel:(LKDBProperty_)property value:(id)value;
这两个方法都不执行

我的模型中嵌套着模型,所以建立了两个表,我想要在这两个方法中,将对应字段改成存被嵌套模型的主键。发现不执行。

并且发现,数据库中,LKDBHelper 貌似已经自动处理了模型中嵌套模型?会自动生成两个表?
{"DB_PKeyValue":{"rowId":"0"},"DB_Type":"DB_Type_Model","DB_Class":"zijinSocketReallContent","DB_RowId":1,"DB_TableName":"zijinSocketReallContent"}还是sqlite的功能呢?

并且表示您提供的DEMO 中也不执行

类似视图概念的字段应该怎么做?

我的需求是类似视图的概念,Model中需要一个property存储统计的数据,不需要对应表column,使用removePropertyWithColumnName方法虽然不会创建该column,但是也不能自动mapping上对应的model属性,使用的是searchWithSQL toClass方法来执行SQL语句,SQL类似

SELECT T1.*, (SELECT COUNT(rowid) FROM T2 WHERE T1.tid=T2.tid) AS P1 FROM T1

表T1对应数据模型T1,模型T1有属性P1,P1不需要在数据库中创建列。
请问这种情况应该怎么做,多谢!

创建默认值的问题

如果我要创建一列Createdate,默认值为当前时间,请问应该如何实现?看实例似乎应该先printsql?

关于删除表中有图片的问题

我在Model中重写了下面方法:

-(id)modelGetValueWithKey:(NSString *)key type:(NSString *)columeType
{
    id value = [self valueForKey:key];
    if([key isEqualToString:@"address"])
    {
        [LKTestForeign insertToDB:self.address];
        return @(self.address.addid);
    }else if([value isKindOfClass:[UIImage class]])
    {
        long random = arc4random();
        long date = [[NSDate date] timeIntervalSince1970];
        NSString* filename = [NSString stringWithFormat:@"img%ld%ld",date&0xFFFFF,random&0xFFF];

        NSData* datas = UIImageJPEGRepresentation(value, 1);
        [datas writeToFile:[SandboxFile GetPathForCaches:filename inDir:[self.class getDBImageDir]] atomically:YES];
        value = filename;
        return value;
    }else
    {
        return [super modelGetValueWithKey:key type:columeType];
    }
}

主要的作用是我想把图片存到Caches中(总之不想放到Document文件夹下,这里需要iCloud同步,数据不宜太大),这样实现一切都没有问题,但是当删除一条记录的时候,对应的图片没有删除。而要是存放到Document中就可以自己删除,我想问问这个问题应该如何解决呢?

浮点型的 double或者float 的问题

也遇到这种存浮点型后,取出的东西不对的问题。。
我的一个model里有个price字段是double型的,在查询中,where 用的是 @“price” < 50000 这样的。
取出的数据的price 基本不对.. 如果price改成int型后,是没问题的..

发现是在建表的地方有:
if ([columnType isEqualToString:LKSQL_Type_Double]) {
columnType = LKSQL_Type_Text;
}
这种应该是double的小于,大于的条件查询,变成了text的小于大于查询了?

如果是为了适配精度问题,却会导致这种查询的错误,得不偿失啊。。

关于getDBImageDir的问题

在我的项目中,只是用sqlite来存放资源(主要有一些图片、声音等),这些内容都在程序的主目录里(NSBundle),而LKDBHelper只支持Document目录,这种情况下使用LKDBHelper, 我有两种方法可以解决:

1、程序第一次启动时,把数据库,图片等资源都拷贝到Document文件夹下面。
这样做的缺点是 代码量大,并且图片保存了两份,太占空间。

2、修改NSObject+LKModel.m文件中所有类似这样的"[LKDBUtils getPathForDocuments:filename inDir:[self.class getDBImageDir]]"代码, 这样就不需要copy图片到Document文件夹下,直接从主目录查找。

我想知道有没有更加好的方法解决我这个问题?有没有可能让用户去定义目录。

Prime Key

hi, i think your API ,isExsitModel(NSobject *) , need a prime key
but, how could i set the model prime key
thanks for review.

xcode5 下的一个警告

__block int lastInsertRowId = 0;

[self executeDB:^(FMDatabase *db) {
    execute = [db executeUpdate:insertSQL withArgumentsInArray:insertValues];
    lastInsertRowId= db.lastInsertRowId;
}];

改为
__block sqlite_int64 lastInsertRowId = 0;

[self executeDB:^(FMDatabase *db) {
    execute = [db executeUpdate:insertSQL withArgumentsInArray:insertValues];
    lastInsertRowId= db.lastInsertRowId;
}];

Model produce physical copy of image repetadly

i am having model in which one property is "UIImage" type and in database that column is "Text" type.

This library have one feature, which create physical image locally and store that image name in database, vice versa.

But i am facing one issue, even i update model that time library create new physical copy of image for the same record but old copy of image won't deleted. So My app size increasing due to keeps unwanted image files.

Any way to hook some where else so it can be help me.

请教一个使用场景

例如浏览记录这个模型其实是跟原来某个数据的模型是一样的,但是它们是两个不同的数据源,那么请问如何为同一种数据模型建两个表呢?还是说继承一个子类更为安全?

'searchWithSQL' is returning a nil objects and must add 'FROM table'

Hi,
I just tried the new 'searchWithSQL' method, and it looks like it's returning the correct number of objects, BUT with nil fields. Plus, I need to add 'FROM table', otherwise, it'll indicate that there is no column named 'category' in my case.

NSMutableArray* arrayOfCategories=[globalHelper searchWithSQL:@"SELECT DISTINCT category FROM Transactions" toClass:[Transaction class]];

//Here it's returning 2 Transaction objects, with nil fields.
//the FROM Transactions is mandatory, or it'll not find category column.

Can you please check that?

Thank you very much.

关于多用户数据库的问题

更新增加设定数据库路径后,就只能使用下面这个方式创建一个LKDBHelper对象,getUsingLKDBHelper这个方法不能够在使用了。
LKDBHelper * helper = [[LKDBHelper alloc] initWithDBPath:dbPath]

然而我的需求是这样的,在documents目录下根据不同的用户有不同的用户文件夹,每个用户文件夹下都有一个相应的用户DB文件,我怎样根据不同的用户路径来取得LKDBHelper这个对象呢?非常感谢能够解答

关于一个项目使用两个数据库的情况

你好,我遇到一个问题。

在一个项目中,我打算维护两个数据库,一个数据库专门用来存放资源,也就是说只有读取操作,没有增、删、改操作; 另一个数据库用来存放app使用过程中生成的数据,比如用户的操作记录等等,这个数据库完全是有程序运行时维护的。

不知道你理解我意思没有,LKDBHelper是个单例类, 我每次使用之前调用:

[[LKDBHelper sharedDBHelper] setDBName:kDATABASENAME];

这样做行不行,不知道会不会产生不可预知的问题,尤其在异步情况下?
大概我想问的就是LKDBHelper怎么维护两个数据库?

可以自定义数据库路径后的版本升级问题

在更新了可自定义数据库路径后,+(void)tableUpdateAddColumnWithName:(NSString_)columnName sqliteType:(NSString_)sqliteType这个方法的内部最后执行的语句是:[[self getUsingLKDBHelper] executeDB:^(FMDatabase *db) {
BOOL success = [db executeUpdate:alertSQL];
if(success)
{
[db executeUpdate:initColumnValue];
}
}];
就会导致去更新默认的LKDB.db这个数据库了,有没有办法解决?

头文件bug

LKDBHelper.h中

#import <FMDatabaseQueue.h>

应该改为

#import "FMDatabaseQueue.h"

Use of undeclared identifier 'SQLITE_OPEN_READWRITE'

image
额,你好我更新了下pod,出现了编译错误,Use of undeclared identifier 'SQLITE_OPEN_READWRITE

platform :ios, '7.0'
pod 'FMDB/SQLCipher',:head
pod 'LKDBHelper',:head

我看了下 splite3.h里面有定义,但是没有看到LKDBhelper哪里引用了 sqlite3.h文件
是不是我哪里设置不对呢,我新建了一个项目,也是出现了这个错误。

Crash in the dbHelperWithPath method

Hi,
I usually have (not always) a crash in the dbHelperWithPath method, without any information:
screen shot 2015-01-09 at 3 02 08 pm
Do you have an idea of the source of the problem and can you please fix it, I tried something but did not achieve it .
Thank you.

pod文件没有更新到最新的版本

刚才看到了您更新了指定数据库路径的部分,但pod相关的文件貌似没有更新,我们有这样的需求在测试这块功能,用pod管理的这个库,希望更新一下,谢谢。

Swift 支持


import Foundation


class LKTest: NSObject {
    var name:String?;
    var age:NSNumber?;
    var isGirl:Bool?;


    override class func getTableName() -> String {
        return "LKTestTable";
    }
}

插入数据:

var globalHelper = LKDBHelper.getUsingLKDBHelper(); globalHelper.createTableWithModelClass(LKTest.classForCoder());
var test = LKTest.();
                test.name = "hello";
                test.age = 3;
                test.isGirl = false;

                globalHelper.insertToDB(test);

查询:

 var result = globalHelper.searchSingle(LKTest.classForCoder(), where: 0, orderBy: nil);
 if let resultConst = result {
     println("search single: \(resultConst)")
     if resultConst is LKTest {
         var codeResult = resultConst as LKTest
         println("code name: \(codeResult.name!), age: \(codeResult.age!)")

     }
 }

但是编译总过不去,因为where是swift中的一个关键字,好像没有更好的办法, 我只能是clone代码库到本地,然后pod指向本地代码库,修改方法签名,将where改成了query

 var result = globalHelper.searchSingle(LKTest.classForCoder(), query: 0, orderBy: nil);
 if let resultConst = result {
     println("search single: \(resultConst)")
     if resultConst is LKTest {
         var codeResult = resultConst as LKTest
         println("code name: \(codeResult.name!), age: \(codeResult.age!)")

     }
 }

不知道有没有更好的办法

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.