FMDB的简单使用


//  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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值