iOS資料庫之尋找功能的實現

來源:互聯網
上載者:User

首先引入檔案:

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

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.