使用Sqlite和CoreData實現(xiàn)的
常用數(shù)據(jù)庫:
SQLServer 2000—保存游戲的素有用戶的信息;
OracleMySQL—網(wǎng)上PHP網(wǎng)站室友較多,特點是網(wǎng)路數(shù)據(jù)庫,支持的功能多,程序比較大。
移動開發(fā)常用:
Sqlite數(shù)據(jù)庫:
特點:足夠小,足夠快(本地數(shù)據(jù)庫),使用比較簡單;
常用軟件:MesaSqlite數(shù)據(jù)庫操作軟件;
數(shù)據(jù)庫操作語言:SQL(結構和查詢語言);
常用開源庫:FMDB。
CoreData是iOS開發(fā)中經(jīng)常用的數(shù)據(jù)持久化的技術。但其操作過程稍微繁瑣,即使你只是實現(xiàn)簡單的存取,不涉及請求優(yōu)化,也要進行許多配置工作,代碼量動輒幾十行,對新手來說也需要較大時間成本。
MagicalRecord是OC的一個庫,協(xié)助CoreData的工作。其吸收了Ruby on Rails 的Active Record模式,目標是:假話CoreData相關代碼,允許清晰,簡單,單行獲取,當需要優(yōu)化請求的時候,仍然允許修改NSFetchRequest.
CoreData的用法:
創(chuàng)建Model:創(chuàng)建一個Model.xcdatamodeld,添加一個Person Entity,添加age firstname,lastname三個屬性,最后時候用Editor >Create NSManagerdObject Subclass ORM生成Person類。
初始化:在AppDelegate中:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[MagicalRecord setupCoreDataStackWithStoreNamed:@"Model.sqlite"];
return YES;
}
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
[MagicalRecord cleanUp];
}
這樣初始化就完成
增加數(shù)據(jù):
Person *person = [Person MR_createEntity];
person.firstname = @"Frank";
person.lastname = @"Zhang";
person.age = @26;
[[NSManagedObjectContext MR_defaultContext] MR_save];
查找數(shù)據(jù):
//查找數(shù)據(jù)庫中的所有Person。
NSArray *persons = [Person MR_findAll];
//查找所有的Person并按照first name排序。
NSArray *personsSorted = [Person MR_findAllSortedBy:@"firstname" ascending:YES];
//查找所有age屬性為25的Person記錄。
NSArray *personsAgeEuqals25 = [Person MR_findByAttribute:@"age" withValue:[NSNumber numberWithInt:25]];
//查找數(shù)據(jù)庫中的第一條記錄
Person *person = [Person MR_findFirst];
修改數(shù)據(jù):
Person *person = ...;//此處略
person.lastname = object;
[[NSManagedObjectContext MR_defaultContext] MR_save];
刪除數(shù)據(jù):
Person *person = ...;//此處略
[person MR_deleteEntity];
[[NSManagedObjectContext MR_defaultContext] MR_save];
更多的:
MagicalRecord 官方
Magical Record入門教程
Using CoreData with MagicalRecord
Magical Record: how to make programming with Core Data pleasant
來自:http://www.cnblogs.com/mybkn/p/3328183.html
最后編輯于 :2017.12.03 07:43:35
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者 平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。