zoukankan      html  css  js  c++  java
  • sqlitepersistentobject

    1、项目地址:http://code.google.com/p/sqlitepersistentobjects/;

    2、如何引入项目中:将Src目录下的文件复制到自己的项目中,并链接到libsqlite3.dylib库文件;
    3、如何使用它:
    1)、定义类
    #import <foundation/foundation.h>
    #import
    "SQLitePersistentObject.h"

    @
    interface PersistablePerson : SQLitePersistentObject {
    NSString *lastName;
    NSString *firstName;
    }
    @property (nonatomic, retain) NSString * lastName;
    @property (nonatomic, retain) NSString * firstName;
    @end
    2)、在代码中使用它
    PersistablePerson *person = [[PersistablePerson alloc] init];
    person.firstName =
    @"Joe";
    person.lastName =
    @"Smith";
    [person save];
    //保存到数据库中
    NSArray *people = [PersistablePerson findByLastName:
    @"Smith"]//从数据库中查询LastName="Smith"的记录
    PeristablePerson *joeSmith = [PersistablePerson findFirstByCriteria:
    @"WHERE last_name = 'Smith' AND first_name = 'Joe'];//从数据库中查询lastName="Smith"并且firstName= 'Joe'的记录
    当查询的条件只有一个时,可以使用类方法“findBy属性名”来查找相关记录。当需要对多个属性进行查询的时候,就需要使用类方法“
    findFirstByCriteria”来查找,其参数所使用的字段名称为实际数据库表中的字段名,而不是类中声明的属性名。
    4、使用索引
    在自己定义的类中重载类方法indices,定义一个返回数组元素的数组。
    +(NSArray *)indices;
    定义:
    +(NSArray *)indices
    {
    NSArray *index1 = [NSArray arrayWithObject:
    @"lastName"];
    NSArray *index2 = [NSArray arrayWithObjects:
    @"lastName", @"firstName", nil];
    NSArray *index3 = [NSArray arrayWithObjects:
    @"age", @"lastName", @"firstName", nil];
    return [NSArray arrayWithObjects:index1, index2, index3, nil];
    }
    5、如何标记
    transient属性
    所谓transient就是只在类中使用而不被持久化到数据库中的属性。跟标记索引差不多,需要重载类方法transients方法,返回一个字符串数组。例如:
    +(NSArray *)transients
    {
      
    return [NSArray arrayWithObject:@"transientNumber"];
    }
    6、查找关联表
            使用SQLitePersistentObject对象作为属性,可以表现“主-从”表关系,如下代码:
    1. //代码详见SQLitePersistentObject自带的Sample Code\SimpleConsoleText 
    2. //Class Post 
    3. @interface Post : SQLitePersistentObject { 
    4.     NSString *title; 
    5.     NSString *text; 
    6.     NSString *transientBit; 
    7. @property (nonatomic,readwrite,retain) NSString *title; 
    8. @property (nonatomic,readwrite,retain) NSString *text; 
    9. @property (nonatomic,readwrite,retain) NSString *transientBit; 
    10. @end; 
    11.  
    12. //Class PostComment  
    13. @class Post; 
    14. @interface PostComment : SQLitePersistentObject { 
    15.     NSString *title; 
    16.     Post *post; 
    17. @property (nonatomic,readwrite,retain) NSString *title; 
    18. @property (nonatomic,readwrite,retain) Post *post; 
    19. @end; 
    20.  
    21. //使用 
    22.     Post *p = [[Post alloc] init]; 
    23.     p.title = @"Test post 1"
    24.     p.text = @"text"
    25.         if ([p existsInDB] == NO) LOG(@"Confirmed not in DB yet"); 
    26.     [p save]; 
    27.  
    28.     PostComment *co = [[PostComment alloc] init]; 
    29.     co.title = @"Comment 1 to Post 2"
    30.     co.post = p; 
    31.     [co save]; 
    32.         [p release]; 
    33.         [co release]; 
    34.  
    35. //查找Post所有的从表 
    36.         Post *p = [Post findByPK:1]; 
    37.     for(PostComment *co in [p findRelated:[PostComment class]]) { 
    38.         LOG(@"\t\t%@", co); 
    39.     } 
    40.  
    41. //访问PostComment的主表 
    42.         PostComment *co = [PostComment findByPK:1]; 
    43.         NSLog(@"%@",co.post.text) 
    需要注意的是:当Post和PostComment的数据都被修改后,主表Post保存时并不会保存从表PostComment的数据,但从表Postcomment保存时,会同时保存主表Post的数据。
    7、使用clearCache
            在某个SQLitePersistentObject子类被直接或间接被引用时候,它会将相关联的表中数据加载到内存中,所以当数据很多的时候会长期占用 一部分内存,为了节省内存,可以在某些类不再被使用的时候,利用类方法clearCache来清除内存中驻留的表数据。clearCache函数实现代码 也是相当简单,如下:
    1. + (void)clearCache 
    2.   if(objectMap != nil) 
    3.     [objectMap removeAllObjects]; 
    4.    
    5.   if(checkedTables != nil) 
    6.     [checkedTables removeAllObjects]; 
    7.    
    8、其它
    1)、它会在内存中维护一个所有持久化对象有映射,所有持久化对象不会被多次加载,如果要实现不同的持久话实例的话,需要在持久化类中实现NSCopying接口。
    2)、目前没有rollback方法。
    3)、SQLitePersistentObject未使用私有非正式的类库。

     http://ddkangfu.blog.51cto.com

  • 相关阅读:
    [HEOI2016/TJOI2016]求和——第二类斯特林数
    RMAN备份脚本
    CF724E Goods transportation
    RMAN备份脚本--DataGuard primary
    [CEOI2017]Mousetrap
    healthcheck
    [学习笔记]斯特林数
    database.sql
    HDU 4372 Count the Buildings——第一类斯特林数
    orac
  • 原文地址:https://www.cnblogs.com/chenfulai/p/2355360.html
Copyright © 2011-2022 走看看