你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS數據庫之查找功能的實現

iOS數據庫之查找功能的實現

編輯: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;


@interface 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>


@interface 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>


@interface 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"


@interface 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:CGRectMake(50, 50, 150, 50)];
   
    self.nameFile.backgroundColor = [UIColor greenColor];
   
    [self.view addSubview:self.nameFile];
   
    self.numberFile = [[UITextField alloc]initWithFrame:CGRectMake(50, 200, 150, 50)];
   
    self.numberFile.backgroundColor = [UIColor redColor];
   
    [self.view addSubview:self.numberFile];
   
 
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   
    button.frame = CGRectMake(100, 80, 50, 40);
   
    button.backgroundColor = [UIColor redColor];
   
    [button addTarget:self action:@selector(baoCunData) forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:button];
   
}

-(void)baoCunData
{
  
    self.baoCunShuJu= [[baoCunData alloc]init];
   
    self.baoCunShuJu.nameData = self.nameFile.text;
   
    self.baoCunShuJu.numberData = self.numberFile.text;
   
    [self.sqlite insertData:self.baoCunShuJu];


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

 


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved