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

本文共 3043 字,大约阅读时间需要 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/

你可能感兴趣的文章
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>