iOS-数据持久化之--SQLite3

四种数据持久化方式总目录

3.SQLite3

iOS的嵌入式SQL数据库,名为SQLite3。SQLite3在存储和检索大量数据方面非常有效。它还能够对数据进行复杂的聚合,与使用对象执行这些操作相比,获得结果的熟读更快。
例如,假设应用程序需要计算其中所有对象的特殊字段的总和,或者需要只符合特定条件的对象的总和,SQLite3可以执行该操作,而不需要将所有对象加载到内存中。从SQLite3获取巨额和比将所有对象加载到内存,然后计算他们值的总和要快几个数量级。作为一个功能比较完善的嵌入式数据库,SQLite3还可以通过一些工具进一步提升速度(如创建表索引加快查询速度)。

生成路径

NSArray *documentArra = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentpath = [documentArra firstObject];
NSString *path = [NSString stringWithFormat:@"%@/data.db",documentpath];

创建打开数据库

sqlite3 *database;
const char *charPath = [path UTF8String];
int databaseResult = sqlite3_open(charPath, &database);

if (databaseResult != SQLITE_OK) {
      NSLog(@"创建/打开数据库失败,%d",databaseResult);
      return;  
}

创建表

//建表格式:create table if not exists 表明(列名 类名,....) 注:如需生成默认增加的id:id integer primary key autoincrement
char *error;
const char *createSQL = "create table if not exists list(id integer primary key autoincrement,name char,sex char)";
int tableReslult = sqlite3_exec(database, createSQL, NULL, NULL, &error);
    
if (tableReslult != SQLITE_OK) { 
    NSLog(@"创建表失败:%s",error);   
}

添加数据

// 对SQL语句执行预编译
int sqlite3_prepare(sqlite3 *db, const char *sql,int byte,sqlite3_stmt **stmt,const char **tail)

1.db代表打开的数据库连接
2.sql代表的sql语句
3.byte代表SQL语句的最大长度
4.传出参数,指向预编译SQL语句产生的sqlite3_stmt
5.指向SQL语句中未使用的部分
int sqlite3_prapare_v2()版本,代表该函数的最新版本。

sqlite3_stmt *stmt;
//对SQL语句执行预编译
const char *insertSQL = "insert into list (name,sex)values('iosRunner','male')";
int insertResult = sqlite3_prepare_v2(database, insertSQL, -1, &stmt,nil);

if (insertResult != SQLITE_OK) {
    NSLog(@"添加失败,%d",insertResult);
}else{
        //执行sql语句
    sqlite3_step(stmt);
}

查找数据

//返回sqlite3_stmt(预编译SQL语句产生的结果)
const char* sqlite3_colum_int/text...(sqlite3_stmt *,int N)

根据结果返回的值的类型不同选择int/text等,N代表列名在表中的位置
结束处理

//    查找
//   sql语句格式: select 列名 from 表名 where 列名 = 参数       注:前面的列名为查询结果里所需要看到的 列名,后面的 列名 = 参数 用于判断删除哪条数据
     const char *searchSQL = "select id,name,sex from haha where name = 'puyun2'";
        int searchResult = sqlite3_prepare_v2(database, searchSQL, -1, &stmt, nil);
        if (searchResult != SQLITE_OK) {
            NSLog(@"查询失败,%d",searchResult); 
        }
        else{
            while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 查询的结果可能不止一条,直到 sqlite3_step(stmt) != SQLITE_ROW,查询结束。
                int idWord = sqlite3_column_int(stmt, 0);
                char *nameWord = (char *) sqlite3_column_text(stmt, 1);
                char *sexWord = (char *)sqlite3_column_text(stmt, 2);
                NSLog(@"%d,%s,%s",idWord,nameWord,sexWord);
            }
        }

修改数据

        // 修改
       // sql语句格式: update 表名 set  列名 = 新参数 where 列名 = 参数   注:前面的 列名 = 新参数 是修改的值, 后面的 列名 = 参数 用于判断删除哪条数据
        const char *changeSQL = "update haha set name = 'buhao' where name = 'iosRunner'";

        int updateResult = sqlite3_prepare_v2(database, changeSQL, -1, &stmt, nil);

        if (updateResult != SQLITE_OK) {

            NSLog(@"修改失败,%d",updateResult);
        }
        else{

            sqlite3_step(stmt);
        }

删除数据

//        删除
//        sql语句格式: delete from 表名 where 列名 = 参数     注:后面的 列名 = 参数 用于判断删除哪条数据
        const char *deleteSQL = "delete from haha where name = 'iosRunner'";

        int deleteResult = sqlite3_prepare_v2(database, deleteSQL, -1, &stmt, nil);

        if (deleteResult != SQLITE_OK) {

            NSLog(@"删除失败,%d",deleteResult);

        }
        else{
            sqlite3_step(stmt);
        }
sqlite3_finalize(stmt);
sqlite3_close(database);

你可能感兴趣的:(iOS-数据持久化之--SQLite3)