First introduce the file:
Libsqlite3.
FMDB (including Global. m and Global. H files)
Disable arc
Use mesaSqlite to create a database and introduce it to the file.
Second:
First, in Global. find # define kDBName @ "shuJu. db ", if the name of the database you created is liyongxing. db, shuJu. change db to liyongxing. db, and then delegate the self. add a line of code copyMainBundleResourceToCacheDir (@ "liyongxing. db ");
OK. The preparation is complete.
Again, go to the Code:
Create a function class for adding, deleting, modifying, and querying:
//
// 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
// Create a database object
@ Property (nonatomic, strong) FMDatabase * sqlite3;
// Create an object for a data transfer station. A data transfer station stores data temporarily for transmission. The best data class is to create one separately.
@ Property (nonatomic, strong) baoCunData * baoCun;
// Add data
-(Void) insertData :( baoCunData *) data;
// Delete data
-(Void) delete :( NSString *) data;
// Change Data
// Search for Data
-(NSMutableArray *) selectAll
@ End
In the. m file
//
// 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 ----- add data
-(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 ----- delete data
-(Void) delete :( NSString *) data
{
}
# Pragma mark ----- Change Data
# Pragma mark ----- search for data -- place the values found in all databases in a variable array
-(NSMutableArray *) selectAll
{
// Call up all values from the database and assign them to strings
NSString * query = @ "SELECT * FROM lyxShuJu ";
// Put the data in the database into a collection class FMResultSet
FMResultSet * set = [self. sqlite3 executeQuery: query];
// Create a dynamic array
NSMutableArray * dataArr = [[NSMutableArray alloc] init];
// Cyclically retrieve the data in the set and assign it to the variable array. The returned value is the variable array.
While ([set next])
{
BaoCunData * data = [baoCunData alloc] init];
Data. nameData = [set stringForColumn: @ "name"];
Data. numberData = [set stringForColumn: @ "number"];
[DataArr addObject: data];
}
Return dataArr;
}
@ End
Create a class to save data
//
// BaoCunData. h
// ShuJuKu
//
// Created by liyongxing on 13-7-31.
// Copyright (c) 2013 liyongxing. All rights reserved.
//
# Import <Foundation/Foundation. h>
@ Interface baoCunData: NSObject
// A name string
@ Property (nonatomic, strong) NSString * nameData;
// Number string (the number may contain symbols or English letters)
@ Property (nonatomic, strong) NSString * numberData;
@ End
In. 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
// Database reference class
. H file
//
// 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
In the. m file
//
// 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];
// Create an added, deleted, modified, and retrieved object
Self. sqlite = [[CaoZuoData alloc] init];
// Create two input boxes
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