当前位置:编程学习 > wap >>

iOS数据库之查找功能的实现

首先引入文件:

libsqlite3.

FMDB(包含Global.m,Global.h文件)

关闭arc

用mesaSqlite创建一个数据库,引入文件中

其次:

首先,在Global.h文件中找到#define kDBName @"shuJu.db",如果你建立的数据库文件名为:liyongxing.db,那就将shuJu.db更改为liyongxing.db,然后再delegate里的self.window下添加一行代码copyMainBundleResourceToCacheDir(@"liyongxing.db");

OK,准备工作已经做好

再次,上代码:

 


创建一个增删改查的函数类:


//
//  CaoZuoData.h
//  shuJuKu
//
//  Created by liyongxing on 13-7-31.
//  Copyright (c) 2013年 liyongxing. All rights reserved.
//

#import <Foundation/Foundation.h>


#import "FMDatabase.h"


@class baoCunData;


@inte易做图ce CaoZuoData : NSObject

//创建一个数据库对象

@property (nonatomic ,strong) FMDatabase * sqlite3;


//创建一个数据中转站的对象,数据中转站就是临时存放数据,做传递用的,最好一个数据类单独创建一个

@property (nonatomic ,strong) baoCunData * baoCun;

//增添数据

-(void)insertData:(baoCunData * )data;


//删除数据

-(void)delete:(NSString *)data;

//更改数据


//查找数据

-(NSMutableArray*) selectAll


@end

 


.m文件中

//
//  CaoZuoData.m
//  shuJuKu
//
//  Created by liyongxing on 13-7-31.
//  Copyright (c) 2013年 liyongxing. All rights reserved.
//

#import "CaoZuoData.h"

#import "Global.h"

#import "baoCunData.h"

@implementation CaoZuoData

-(id)init
{
    if (self==[super init])
    {
        self.sqlite3 = [FMDatabase databaseWithPath:dbPath()];
       
        [self.sqlite3 open];
    }
    return self;
}

#pragma mark-----增添数据


-(void)insertData:(baoCunData * )data
{
   [self.sqlite3 executeUpdate:@"INSERT INTO lyxShuJu(name,number) VALUES          (?,?)",data.nameData,data.numberData];
   
     NSLog(@"data.nameData == %@",data.nameData);
   
     NSLog(@"numberData == %@",data.numberData);

}

#pragma mark-----删除数据

-(void)delete:(NSString *)data

{


}

#pragma mark-----更改数据

#pragma mark-----查找数据--将所有数据库中查找到的值放在一个变动数组中

-(NSMutableArray*) selectAll
    {
        //从数据库中调出所有值赋给字符串
       
        NSString * query = @"SELECT * FROM lyxShuJu";
       
        //将数据库中的数据放到一个集合类FMResultSet中
       
        FMResultSet *set = [self.sqlite3 executeQuery:query];
       
        //创建一个动态数组
       
        NSMutableArray *dataArr = [[NSMutableArray alloc]init];
       
    //将集合中的数据循环取出,并赋给变动数组,返回值为这个变动数组
       
        while ([set next])
        {
           
            baoCunData * data = [[baoCunData alloc]init];
           
            data.nameData =[set stringForColumn:@"name"];
           
            data.numberData = [set stringForColumn:@"number"];
           
            [dataArr addObject:data];
           
        }
        return dataArr;
    }
@end

 

 

建立一个保存数据的类

 


//
//  baoCunData.h
//  shuJuKu
//
//  Created by liyongxing on 13-7-31.
//  Copyright (c) 2013年 liyongxing. All rights reserved.
//


#import <Foundation/Foundation.h>


@inte易做图ce baoCunData : NSObject

//一个姓名字符串

@property (nonatomic ,strong) NSString * nameData;

//号码字符串(号码中可能存在符号或者英文)

@property (nonatomic ,strong) NSString * numberData;


@end


.m中

//
//  baoCunData.m
//  shuJuKu
//
//  Created by liyongxing on 13-7-31.
//  Copyright (c) 2013年 liyongxing. All rights reserved.
//


#import "baoCunData.h"


@implementation baoCunData


-(id)init
{
    if (self == [super init])
    {
       
        self.nameData = nil;
       
        self.numberData = nil;
       
    }


    return self;
}


@end

 

 

//数据库的引用类

 


.h文件

//
//  LYXViewController.h
//  shuJuKu
//
//  Created by liyongxing on 13-7-31.
//  Copyright (c) 2013年 liyongxing. All rights reserved.
//


#import <UIKit/UIKit.h>


@inte易做图ce LYXViewController : UIViewController

@property(nonatomic ,strong) UITextField * nameFile;

@property (nonatomic ,strong) UITextField * numberFile;

@end

 

 

.m文件中

//
//  LYXViewController.m
//  shuJuKu
//
//  Created by liyongxing on 13-7-31.
//  Copyright (c) 2013年 liyongxing. All rights reserved.
//

#import "LYXViewController.h"


#import "baoCunData.h"

#import "FMDatabase.h"

#import "CaoZuoData.h"


@inte易做图ce LYXViewController ()


@property (nonatomic ,strong) baoCunData * baoCunShuJu;

@property (nonatomic ,strong) CaoZuoData * sqlite;

@end

@implementation LYXViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //创建一个增删改查得类得对象
   
    self.sqlite = [[CaoZuoData alloc]init];


    //创建两个输入框

    self.nameFile = [[UITextField alloc]initWithFrame:CGRect

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,