博客
关于我
iOS基础——数据操作之Sqlite3、FMDB
阅读量:716 次
发布时间:2019-03-21

本文共 3088 字,大约阅读时间需要 10 分钟。

iOS基础数据操作之Sqlite3、FMDB

1 Sqlite3

Sqlite3 是 iPhone-app 开发中常用的本地数据库解决方案,适用于简单的增删改查操作。其特点是轻量级且无服务器,操作流程如下:

1.1 导入 Sqlite3 库

在 Xcode 项目设置中,进入 Build Phases > Add/Library,点击右上角的 + 按钮,选择 Sqlite3 库进行添加。

1.2 数据库操作

// 数据库路径获取
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"person.sqlite"];
sqlite3_open(path.UTF8String, &_db);
// 创建数据库表
NSString *createTableSql = @"create table if not exists t_person (id integer primary key autoincrement, name text not null, age integer default 10)";
if (sqlite3_exec(_db, createTableSql.UTF8String, NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"创建表成功");
}

1.3 常用操作

  • 插入数据:

    NSString *insertSql = @"insert into t_person (name, age) values('zhangsan', 15)";
    if (sqlite3_exec(_db, insertSql.UTF8String, NULL, NULL, NULL) == SQLITE_OK) {
    NSLog(@"插入成功");
    }
  • 删除数据:

    NSString *deleteSql = @"delete from t_person where age > 5";
    if (sqlite3_exec(_db, deleteSql.UTF8String, NULL, NULL, NULL) == SQLITE_OK) {
    NSLog(@"删除成功");
    }
  • 更新数据:

    NSString *updateSql = @"update t_person set name='lisi' where age > 5";
    if (sqlite3_exec(_db, updateSql.UTF8String, NULL, NULL, NULL) == SQLITE_OK) {
    NSLog(@"更新成功");
    }
  • 查询数据:

    NSString *querySql = @"select name, age from t_person";
    sqlite3_stmt *stmt = nil;
    if (sqlite3_prepare_v2(_db, querySql.UTF8String, -1, &stmt, NULL) == SQLITE_OK) {
    while (sqlite3_step(stmt) == SQLITE_ROW) {
    NSString *name = sqlite3_column_text(stmt, 0);
    int age = sqlite3_column_int(stmt, 1);
    NSLog(@"姓名: %@, 年龄: %d", name, age);
    }
    }

2 FMDB

FMDB 是一个流行的第三方 SQLite 库框架,简化了 Sqlite3 的使用,适合快速开发需求。其优势包括线程安全和易用性。

2.1 安装 FMDB

在项目中添加 FMDB.framework,注意包含 #import "FMDB.h" 头文件。

2.2 创建数据库

// 数据库路径
NSString *fmdbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data.sqlite");
FMDatabase *fmdb = [FMDatabase databaseWithPath:fmdbPath];
[fmdb open];
// 创建表
if ([fmdb executeUpdate:@"create table if not exists t_person (id integer primary key autoincrement, name text not null, age integer default 10)"]) {
NSLog(@"创建表成功");
}

2.3 常用操作

  • 插入数据:

    [self.database executeUpdate:@"insert into t_person (name, age) values('zhangsan', 15)"];
  • 删除数据:

    [self.database executeUpdate:@"delete from t_person where age > 5"];
  • 更新数据:

    [self.database executeUpdate:@"update t_person set name='lisi' where age > 5"];
  • 查询数据:

    [self.database executeQuery:@"select name, age from t_person"];
    while ([result next]) {
    NSString *name = [result stringForColumnIndex:0];
    int age = [result intForColumnIndex:1];
    NSLog(@"姓名: %@", name, age);
    }

2.4 线程安全操作

FMDB 提供线程安全操作,推荐使用 FMDatabaseQueue

FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:fmdbPath];
// 线程安全写法
[queue inDatabase:^{
// 在数据库操作
[数据库.open];
}];
// 查询操作
[queue inDatabase:^{
// 查询代码
}];

3 事务操作

[queue inDatabase:^{ 
[database beginTransaction];
// 同一事务内多个操作
}]);

事务可以通过 commitrollback 管理,并可通过 inTransaction 判断事务状态。

通过以上方法,您可以快速实现 iOS 应用中的本地数据存储需求,选择 Sqlite3 或 FMDB 根据项目复杂度和开发需求进行选择。

转载地址:http://aevrz.baihongyu.com/

你可能感兴趣的文章
nodejs npm常用命令
查看>>
NodeJS 导入导出模块的方法( 代码演示 )
查看>>
nodejs 的 Buffer 详解
查看>>
nodejs 读取xlsx文件内容
查看>>
nodejs 运行CMD命令
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
Nodejs中的fs模块的使用
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>
NodeJs学习笔记002--npm常用命令详解
查看>>
nodejs学习笔记一——nodejs安装
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>