// ViewController.m
// 练习:FMDB的简单使用
//
// Created by yongma11 on 15/12/13.
// Copyright © 2015年 XUE. All rights reserved.
//
#import "ViewController.h"
#import "FMDatabase.h"
@interface ViewController ()
- (IBAction)add:(id)sender;
- (IBAction)delete:(id)sender;
- (IBAction)modefy:(id)sender;
- (IBAction)wnquey:(id)sender;
//数据库对象
@property(nonatomic,strong)FMDatabase *db;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//沙盒路径
NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"one.sqlite"];
//通过指定dqlite数据库文件来创建FMDataBase对象
_db = [FMDatabase databaseWithPath:path];
// NSLog(@"%@",[NSFileManager defaultManager]);//打印当前。
//
//打开数据库
if([_db open])
{
NSLog(@"打开成功!");
}
//创建数据库
//数据库操作:插入,更新,删除都属于update
//数据库查询:一个类
//建表语句
[_db executeUpdate:@"create table if not exists t_one(id integer primary key ,name text ,phone text)"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)add:(id)sender {
//?表示数据库里面的占位符.
[_db executeUpdate:@"insert into t_one(name,phone) values(?,?)",
@"oooo",@"1234"];
}
- (IBAction)delete:(id)sender {
[_db executeUpdate:@"delete from t_one"];//删除数据
}
//修改
- (IBAction)modefy:(id)sender {
[_db executeUpdate:@"update t_one set name= ?",@"abc"];
}
//查询
- (IBAction)wnquey:(id)sender {
FMResultSet *result = [_db executeQuery:@"select *from t_one"];
//从结果集中往下找
while([result next])
{
NSString* name = [result stringForColumn:@"name"];
NSString* phone = [result stringForColumn:@"phone"];
NSLog(@"%@,%@",name,phone);
}
}
@end