<span style="font-family:Microsoft YaHei;font-size:18px;"></span><pre name="code" class="objc">
在iOS中主要使用的数据库为Sqlite3这个轻量级的数据库,和CoreData这个重量级的数据库
<span style="font-family:Microsoft YaHei;font-size:18px;">在这里主要是简单介绍一下FMDB这个数据库的使用方法,直接下载FMDB后解压文件扔到工程项目中即可导入头文件进行使用。</span>
<span style="font-family:Microsoft YaHei;font-size:18px;">FMDB需要依赖libsql3.framework,因为FMDB是在sqlite3中进行封装的一个开源库</span>
</pre><pre name="code" class="objc"><h1 style="white-space: pre;"><span style="font-family:Microsoft YaHei;font-size:18px;">FMDatabase的简单使用如下</span></h1>
<span style="font-family:Microsoft YaHei;font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc">#import "FMDB.h"
@interface IWViewController ()
@property (nonatomic, strong) FMDatabase *db;
@end
@implementation IWViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 0.获得沙盒中的数据库文件名
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"];
// 1.创建数据库实例对象
self.db = [FMDatabase databaseWithPath:filename];
// 2.打开数据库
if ( [self.db open] ) {
NSLog(@"数据库打开成功");
// 创表
BOOL result = [self.db executeUpdate:@"create table if not exists t_student (id integer primary key autoincrement, name text, age integer);"];
if (result) {
NSLog(@"创表成功");
} else {
NSLog(@"创表失败");
}
} else {
NSLog(@"数据库打开失败");
}
}
- (IBAction)insert
{
for (int i = 0; i<40; i++) {
NSString *name = [NSString stringWithFormat:@"rose-%d", arc4random() % 1000];
NSNumber *age = @(arc4random() % 100 + 1);
[self.db executeUpdate:@"insert into t_student (name, age) values (?, ?);", name, age];
}
}
- (IBAction)query
{
// 1.查询数据
FMResultSet *rs = [self.db executeQuery:@"select * from t_student where age > ?;", @50];
// 2.遍历结果集
while (rs.next) {
int ID = [rs intForColumn:@"id"];
NSString *name = [rs stringForColumn:@"name"];
int age = [rs intForColumn:@"age"];
NSLog(@"%d %@ %d", ID, name, age);
}
}
<span style="font-family:Microsoft YaHei;font-size:18px;">
</span>
<span style="font-family:Microsoft YaHei;font-size:18px;">
</span>
以下是FMDatabaseQueue的用法,系统会自动执行多线程并保证线程资源安全
<span style="font-family:Microsoft YaHei;font-size:18px;"></span><pre name="code" class="objc"><span style="font-family:Microsoft YaHei;font-size:18px;">@interface IWViewController ()
@property (nonatomic, strong) FMDatabaseQueue *queue;
- (IBAction)insert;
- (IBAction)update;
- (IBAction)delete;
- (IBAction)query;
@end</span>
@implementation IWViewController
<span style="font-family:Microsoft YaHei;font-size:18px;">- (void)viewDidLoad
{
[super viewDidLoad];
// 0.获得沙盒中的数据库文件名
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"];
// 1.创建数据库队列
self.queue = [FMDatabaseQueue databaseQueueWithPath:filename];
// 2.创表
[self.queue inDatabase:^(FMDatabase *db) {
BOOL result = [db executeUpdate:@"create table if not exists t_student (id integer primary key autoincrement, name text, age integer);"];
if (result) {
NSLog(@"创表成功");
} else {
NSLog(@"创表失败");
}
}];
}
- (IBAction)insert
{
[self.queue inDatabase:^(FMDatabase *db) {
for (int i = 0; i<40; i++) {
NSString *name = [NSString stringWithFormat:@"rose-%d", arc4random() % 1000];
NSNumber *age = @(arc4random() % 100 + 1);
[db executeUpdate:@"insert into t_student (name, age) values (?, ?);", name, age];
}
}];
}
- (IBAction)update
{</span>
<span style="font-family:Microsoft YaHei;font-size:18px;">//事务指的是,如果你需要保证几个执行操作在多线程中有序执行,那你就需要用事务去包装那几个操作,系统会自动控制其执行顺序的
[self.queue inDatabase:^(FMDatabase *db) {
// 如果需要开启事务,则执行一下代码,并提交commit才可以成功
// [db executeUpdate:@"begin transaction;"];
// [db beginTransaction];
[db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"];
[db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"];
// if (发现情况不对){
// // 回滚事务
// [db rollback];
[db executeUpdate:@"rollback transaction;"];
// }
[db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"];
// 提交事务
// [db commit];
// [db executeUpdate:@"commit transaction;"];
}];
}
- (IBAction)delete
{</span>
<span style="font-family:Microsoft YaHei;font-size:18px;">//inTransaction 函数下系统会自动创建事务
[self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"];
[db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"];
// if (发现情况不对){
// // 回滚事务
// *rollback = YES;
// }
}];
}
- (IBAction)query
{
[self.queue inDatabase:^(FMDatabase *db) {
// 1.查询数据
FMResultSet *rs = [db executeQuery:@"select * from t_student where age > ?;", @50];
// 2.遍历结果集
while (rs.next) {
int ID = [rs intForColumn:@"id"];
NSString *name = [rs stringForColumn:@"name"];
int age = [rs intForColumn:@"age"];
NSLog(@"%d %@ %d", ID, name, age);
}
}];
}
@end</span>