使用FMDB进行数据库操作
2016-07-11 13:35
447 查看
FMDB: https://github.com/ccgus/fmdb
按照教程可以通过pods的方式进行依赖引入。
简单的增删改查操作,代码如下:
[objc]
view plain
copy
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property(nonatomic,strong) FMDatabase *db;
- (IBAction)insert:(UIButton *)sender;
- (IBAction)delete:(UIButton *)sender;
- (IBAction)update:(UIButton *)sender;
- (IBAction)select:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//数据的路径,放在沙盒的cache下面
NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [cacheDir stringByAppendingPathComponent:@"contact.sqlite"];
//创建并且打开一个数据库
_db = [FMDatabase databaseWithPath:filePath];
BOOL flag = [_db open];
if (flag) {
NSLog(@"数据库打开成功");
}else{
NSLog(@"数据库打开失败");
}
//创建表
BOOL create = [_db executeUpdate:@"create table if not exists t_health(id integer primary key autoincrement, name text,phone text)"];
if (create) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
- (IBAction)insert:(UIButton *)sender {
BOOL insert = [_db executeUpdate:@"insert into t_health (name,phone) values(?,?)",@"jacob",@"138000000000"];
if (insert) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败");
}
}
- (IBAction)delete:(UIButton *)sender {
BOOL delete = [_db executeUpdate:@"delete from t_health where name like ?",@"jacob"];
if (delete) {
NSLog(@"删除数据成功");
}else{
NSLog(@"删除数据失败");
}
}
- (IBAction)update:(UIButton *)sender {
BOOL update = [_db executeUpdate:@"update t_health set name = ? where phone = '%@'",@"jacob111"];
if (update) {
NSLog(@"更新数据成功");
}else{
NSLog(@"更新数据失败");
}
}
- (IBAction)select:(UIButton *)sender {
FMResultSet *set = [_db executeQuery:@"select * from t_health "];
while ([set next]) {
NSString *name = [set stringForColumn:@"name"];
NSString *phone = [set stringForColumn:@"phone"];
NSLog(@"name : %@ phone: %@",name,phone);
}
}
@end
按照教程可以通过pods的方式进行依赖引入。
简单的增删改查操作,代码如下:
[objc]
view plain
copy
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property(nonatomic,strong) FMDatabase *db;
- (IBAction)insert:(UIButton *)sender;
- (IBAction)delete:(UIButton *)sender;
- (IBAction)update:(UIButton *)sender;
- (IBAction)select:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//数据的路径,放在沙盒的cache下面
NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [cacheDir stringByAppendingPathComponent:@"contact.sqlite"];
//创建并且打开一个数据库
_db = [FMDatabase databaseWithPath:filePath];
BOOL flag = [_db open];
if (flag) {
NSLog(@"数据库打开成功");
}else{
NSLog(@"数据库打开失败");
}
//创建表
BOOL create = [_db executeUpdate:@"create table if not exists t_health(id integer primary key autoincrement, name text,phone text)"];
if (create) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
- (IBAction)insert:(UIButton *)sender {
BOOL insert = [_db executeUpdate:@"insert into t_health (name,phone) values(?,?)",@"jacob",@"138000000000"];
if (insert) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败");
}
}
- (IBAction)delete:(UIButton *)sender {
BOOL delete = [_db executeUpdate:@"delete from t_health where name like ?",@"jacob"];
if (delete) {
NSLog(@"删除数据成功");
}else{
NSLog(@"删除数据失败");
}
}
- (IBAction)update:(UIButton *)sender {
BOOL update = [_db executeUpdate:@"update t_health set name = ? where phone = '%@'",@"jacob111"];
if (update) {
NSLog(@"更新数据成功");
}else{
NSLog(@"更新数据失败");
}
}
- (IBAction)select:(UIButton *)sender {
FMResultSet *set = [_db executeQuery:@"select * from t_health "];
while ([set next]) {
NSString *name = [set stringForColumn:@"name"];
NSString *phone = [set stringForColumn:@"phone"];
NSLog(@"name : %@ phone: %@",name,phone);
}
}
@end
相关文章推荐
- SQLite教程(十一):临时文件
- SQLite中重置自动编号列的方法
- 保护你的Sqlite数据库(SQLite数据库安全秘籍)
- SQLite教程(十):内存数据库和临时数据库
- SQLite 入门教程二 SQLite的创建、修改、删除表
- 详解SQLite中的数据类型
- 详解SQLite中的查询规划器
- C#简单查询SQLite数据库是否存在数据的方法
- C#操作SQLite数据库方法小结(创建,连接,插入,查询,删除等)
- C#操作SQLite数据库之读写数据库的方法
- C#封装的Sqlite访问类实例
- SQLite中的B-Tree实现细节分析
- SQLite 中文指南之FAQ第1/6页
- Android Sqlite命令详解(基本命令)
- SQLite 内存数据库学习手册
- SQLite中的WAL机制详细介绍
- Sqlite数据库里插入数据的条数上限是500
- SQLite速度评测代码
- 简单分析SQLite4的一些设计改变
- C#解决SQlite并发异常问题的方法(使用读写锁)