IOS中常用的四种数据持久化方法


  • (1)属性列表:简单 ,只能适用于小数据量
  • (2)对象归档:加密, 保存的方式是序列化,只能适用于小数据量
  • (3)SQLite:SQLite可移植性好,很容易使用,很小,高效而且可靠。
  • (4)CoraData :Core Data本质上是使用SQLite保存数据,但是它不需要编写任何SQL语句。
1.属性列表:容器对象——>property list
将数组保存到沙盒路径下


2.对象归档:把数据进行序列化处理


3.SQLite
  • - (void)copyDBFile

    {

        NSFileManager *manager = [NSFileManager defaultManager];

        //如果文件存在,就不复制

        if ([manager fileExistsAtPath:[self databasePath]]) {

            return;

        }

        NSString *atPath = [[NSBundle mainBundlepathForResource:kDatabaseName ofType:nil];

        //复制数据库文件到沙盒路径下

        [manager copyItemAtPath:atPath toPath:[self databasePatherror:nil];

    }


    //取得数据库文件所在的路径

    - (NSString *)databasePath

    {

    //    NSLog(@"%@", [[NSBundle mainBundle] pathForResource:kDatabaseName ofType:nil]);

        

        return [NSHomeDirectory() stringByAppendingFormat:@"/Library/%@"kDatabaseName];

        

    //    return [[NSBundle mainBundle] pathForResource:kDatabaseName ofType:nil];

    }


    //增加一条数据

    - (BOOL)addUser:(User *)user

    {

        //数据库对象

        sqlite3 *mysqlite = nil;


        //1.打开数据库

        //filename:数据库文件的路径(C的字符串)

        //sqlite3:具体执行的数据库对象

        int openResult = sqlite3_open([[self databasePathUTF8String], &mysqlite);

        //如果open函数执行成功,会返回0SQLITE_OK

        if (openResult != SQLITE_OK) {

            NSLog(@"数据库打开失败");

            return NO;

        }

        

        

        //2.准备SQL语句

        

        //可以把 sqlite3_stmt * 所保存的内容看成是 sql语句

        sqlite3_stmt *stmt = nil;

        //构造sql语句

        NSString *sql = [NSString stringWithFormat:@"insert into UserTable (username, password, phone, age) values (\"%@\", \"%@\", \"%@\", %ld)", user.username, user.password, user.phone, user.age];

        //sqlite3:具体执行的数据库对象

        //zSql: sql语句

        //stmt: 语句保存的对象

        sqlite3_prepare(mysqlite, [sql UTF8String], -1, &stmt, NULL);

        

        //3.执行SQL语句

        int stepResult = sqlite3_step(stmt);

        if (stepResult == SQLITE_ERROR || stepResult == SQLITE_MISUSE) {

            NSLog(@"执行失败");

        }

        

        //4.SQL语句完结

        sqlite3_finalize(stmt);

        

        //5.关闭数据库

        sqlite3_close(mysqlite);

         

        

        return YES;

    }

    4.CoreData

    - (void)openDataBase

    {

        //1.加载数据模型文件 xcdatamodeld

        NSURL *url = [[NSBundle mainBundleURLForResource:@"DataModel" withExtension:@"momd"];

        //NSManagedObjectModel 用于加载数据模型文件

        NSManagedObjectModel *dataModel = [[NSManagedObjectModel allocinitWithContentsOfURL:url];

        

        //2.打开模型文件对应的数据库文件

        //创建协调器对象,使用协调器管理数据库文件

        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator allocinitWithManagedObjectModel:dataModel];

        

        //指定数据库文件在沙盒中的位置

        NSString *storePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/DataStore.sqlite"];

        NSURL *storeURL = [NSURL fileURLWithPath:storePath];

        

        //打开一个数据文件 PersistentStore --> 数据库文件

        /*

         1.如果文件不存在,创建新的数据库文件

         2.如果文件存在,直接打开这个文件

         */

        NSError *error = nil;

        [psc addPersistentStoreWithType:NSSQLiteStoreType //生成的数据库文件格式为SQLite

                          configuration:nil

                                    URL:storeURL    //数据库文件保存的路径

                                options:nil

                                  error:&error];

        if (error) {

            NSLog(@"数据库文件打开失败");

        } else

        {

            NSLog(@"数据库打开成功");

        }

        

        //3.操作数据库

        context = [[NSManagedObjectContext allocinit];

        //指定context使用哪一个coordinator来操作数据库文件

        context.persistentStoreCoordinator = psc;

        

        

    }



你可能感兴趣的:(数据)