static void reset_environment()
{
unlink("test.db");
[[SQLiteInstanceManager sharedManager] setDatabaseFilepath:@"test.db"];
}
并在main方法中调用,该方法用于指定sqlite数据库文件。
4.对象
#import <foundation/foundation.h>
#import "SQLitePersistentObject.h"
@interface PersistablePerson : SQLitePersistentObject
@property (nonatomic,readwrite, retain) NSString * lastName;
@property (nonatomic,readwrite, retain) NSString * firstName;
@end
----------------------------------------------------------------------
在.m中重载+(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];
}
-----------------------insert-----------------------------------------
PersistablePerson *person = [[PersistablePerson alloc] init];
person.firstName = @"Joe";
person.lastName = @"Smith";
[person save];
-----------------------select with no condition------------------
NSArray *people = [PersistablePerson allObjects];
-----------------------findByPropertyName-----------------------
NSArray *people = [PersistablePerson findByLastName:@"Smith"]
-----------------------select one record-----------------------
PeristablePerson *joeSmith = [PersistablePerson findFirstByCriteria:@"WHERE last_name = 'Smith' AND first_name = 'Joe'"];
-----------------------select records by condition----------------------
NSArray *people = [PersistablePerson findByCriteria:@"WHERE ......."];
--------------------------清空表数据缓存--------------------------------
[PersistablePerson clearCache];
5.心得:
一个对象对应一张表,对象名PeristablePerson对应表名persistable_person,属性名lastName对应字段名last_name,即在大写字母前加"_"并将大写字母替换为小写。没有大写字母则不变。
属性名不能加"_",如last_name,否则查询时属性值为nil,即便数据库中不为null。