FMDB框架下载<= 点击左边下载FMDB框架.....(要使用一个框架之前肯定免不了先下载框架啦)
下载完毕后只需要将以下文件导入项目中
FMDB框架配置
1.导入libsqlite3.0.tbd
好了,废话不多说。直接上代码
创建一个FMDB工具类
#import <Foundation/Foundation.h>
@interface SQLiteTool : NSObject
// 单粒
+ (instancetype)shareInstance;
// 创建表
- (void)createTable;
// 插入数据
- (void)insertRecord;
// 执行多条语句
- (void)executeStaments;
// 查询所有数据
- (void)queryAll;
// 事件方法:将多条语句一起插入数据库,只要有一条不成功便全部插入不成功
- (void)transaction;
@end
#import "SQLiteTool.h"
@interface SQLiteTool ()
@property (nonatomic ,strong)FMDatabase *db;
@property (nonatomic ,strong)FMDatabaseQueue *dbQueue;
@end
@implementation SQLiteTool
static id shareInstance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareInstance = [super allocWithZone:zone];
});
return shareInstance;
}
+ (instancetype)shareInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareInstance = [[self alloc]init];
});
return shareInstance;
}
- (id)copyWithZone:(NSZone *)zone
{
return shareInstance;
}
- (instancetype) init {
if (self = [super init]) {
// 创建一个数据库
//_db = [FMDatabase databaseWithPath:@"/Users/zhongwangheyi/Desktop/db/demo.sqlite"];
_dbQueue = [FMDatabaseQueue databaseQueueWithPath:@"/Users/zhongwangheyi/Desktop/db/demo.sqlite"];
// 打开数据库
if (_db.open) {
NSLog(@"数据库打开成功");
}else{
NSLog(@"数据库打开失败");
}
}
return self;
}
- (void)createTable
{
NSString *sql = @"create table t_stu(id integer primary key autoincrement,name text not null,age integer,score real default 60)";
BOOL result = [_db executeUpdate:sql values:nil error:nil];
if (result) {
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
}
- (void)executeStaments
{
NSString *sql = @"insert into t_stu(name,age,score) values('zhangsan2',162,882);
insert into t_stu(name,age,score) values('zhangsan1',161,881);";
BOOL result = [_db executeStatements:sql];
if (result) {
NSLog(@"多条语句执行成功");
}else{
NSLog(@"执行失败");
}
}
- (void)insertRecord
{
NSString *sql = @"insert into t_stu(name,age,score) values('zhangsan',5,8)";
//
[_dbQueue inDatabase:^(FMDatabase *db) {
BOOL result = [db executeUpdate:sql values:nil error:nil];
if (result) {
NSLog(@"数据插入成功");
}else{
NSLog(@"数据插入失败");
}
}];
}
- (void)queryAll
{
NSString *sql = @"select * from t_stu";
FMResultSet *resultSet = [_db executeQuery:sql values :nil error:nil];
while ([resultSet next]) {
NSString *name = [resultSet stringForColumn:@"name"];
NSInteger age = [resultSet intForColumn:@"age"];
double score = [resultSet doubleForColumn:@"score"];
NSLog(@"name = %@,age = %zd,score = %lf",name,age,score);
}
}
- (void)transaction
{
NSString *sql = @"insert into t_stu(name,age,score) values('lyon',0000,0000)";
NSString *sql2 = @"insert into t_stu2(name,age,score) values('ios',51112,81112)";
[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
BOOL result = [db executeUpdate:sql withArgumentsInArray:nil];
BOOL result2 = [db executeUpdate:sql2 withArgumentsInArray:nil];
if (result && result2) {
// 如果是,数据插入成功
}else{
// 如果不是,设置*rollback = YES,回滚至原始状态
*rollback = YES;
}
}];
}
@end
网友评论