iOS-数据库-FMDB-第三方-插入数据列表-(附件2-DataBaseHanfle.h-DataBaseHanfle.h-Student.h-Student.m)

2016-03-10 15:26:00

iOS-数据库-FMDB-第三方-插入数据列表-(附件2-DataBaseHanfle.h-DataBaseHanfle.h-Student.h-Student.m)

第三方-点击下载-

//
// DataBaseHandle.h
// UI18_FMDB
//
// Created by YIem on 16/3/4.
// Copyright © 2016年 YIem. All rights reserved.
//

import <Foundation/Foundation.h>

@class Student;
@interface DataBaseHandle : NSObject

  • (instancetype)shareDataBase;
  • (void)openDB;
  • (void)closeDB;
  • (void)createTable;
  • (void)deleteTable;
  • (void)insertDataWithStu:(Student *)stu;
  • (void)updateWithStu:(Student *)stu num:(NSInteger)num;
  • (void)deleteDataWithNum:(NSInteger)num;
  • (NSMutableArray )selectStuWithSex:(NSString )sex;
  • (void)insertMoreStudent:(Student *)stu;
    @end







//
// DataBaseHandle.m
// UI18_FMDB
//
// Created by YIem on 16/3/4.
// Copyright © 2016年 YIem. All rights reserved.
//

import "DataBaseHandle.h"

import "FMDatabase.h"

import "Student.h"

import "FMResultSet.h"

import "FMDatabaseQueue.h"

@interface DataBaseHandle ()

@property (nonatomic, retain) FMDatabase *fmdb;
@property (nonatomic, copy) NSString *filePath;
@end

@implementation DataBaseHandle

  • (void)dealloc
    {
    [_fmdb release];
    [_filePath release];
    [super dealloc];
    }
  • (instancetype)shareDataBase
    {
    static DataBaseHandle *dataBase = nil;
    if (nil == dataBase) {

    dataBase = [[DataBaseHandle alloc] init];

    }
    return dataBase;
    }

  • (void)openDB
    {
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", file);
    self.filePath = [file stringByAppendingPathComponent:@"dataBaseFMDB.db"];

    // FMDB是进行了OC 的封装
    // 注意先创建对象
    self.fmdb = [[FMDatabase alloc] initWithPath:_filePath];
    // 打开数据库
    BOOL ret = [self.fmdb open];
    if (ret) {

    NSLog(@"打开数据库成功");

    } else {

    NSLog(@"打开数据库失败");

    }

    }

  • (void)closeDB
    {
    BOOL ret = [self.fmdb close];
    if (ret) {

    NSLog(@"数据库关闭成功");

    } else {

    NSLog(@"数据库关闭失败");

    }
    }

  • (void)createTable
    {
    NSString *sql = @"CREATE TABLE IF NOT EXISTS student(number integer PRIMARY KEY AUTOINCREMENT, name TEXT, sex TEXT, age integer)";
    BOOL ret = [self.fmdb executeUpdate:sql];
    if (ret) {

    NSLog(@"创建表单成功");

    } else {

    NSLog(@"创建表单失败");

    }
    }

  • (void)deleteTable
    {
    NSString *sql = @"DROP TABLE student";
    BOOL ret = [self.fmdb executeUpdate:sql];
    if (ret) {

    NSLog(@"删除表单成功");

    } else {

    NSLog(@"删除本地失败");

    }
    }

  • (void)insertDataWithStu:(Student *)stu
    {
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO student(name, sex, age) VALUES ('%@', '%@', '%ld')", stu.name, stu.sex, stu.age];
    BOOL ret = [self.fmdb executeUpdate:sql];
    if (ret) {

    NSLog(@"插入数据成功");

    } else {

    NSLog(@"插入数据失败");

    }
    }

  • (void)updateWithStu:(Student *)stu num:(NSInteger)num
    {
    NSString *sql = [NSString stringWithFormat:@"UPDATE student SET name = '%@', sex = '%@', age = '%ld' WHERE number = '%ld'", stu.name, stu.sex, stu.age, num];
    BOOL ret = [self.fmdb executeUpdate:sql];
    if (ret) {

    NSLog(@"修改数据成功");

    } else {

    NSLog(@"修改数据失败");

    }
    }

  • (void)deleteDataWithNum:(NSInteger)num
    {
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM student WHERE number = '%ld'", num];
    BOOL ret = [self.fmdb executeUpdate:sql];
    if (ret) {

    NSLog(@"删除数据成功");

    } else {

    NSLog(@"删除数据失败");

    }
    }

  • (NSMutableArray )selectStuWithSex:(NSString )sex
    {
    NSString sql = [NSString stringWithFormat: @"SELECT FROM student WHERE sex LIKE '%%%@%%'", sex];

    // FMResultSet 类用来查询
    FMResultSet *resSet = [self.fmdb executeQuery:sql];

    NSMutableArray *stuArr = [NSMutableArray array];
    // 解析每一行
    while ([resSet next]) {

    Student *stu = [[Student alloc] init];
    stu.name = [resSet stringForColumn:@"name"];
    stu.sex = [resSet stringForColumn:@"sex"];
    stu.age = [resSet longForColumn:@"age"];
    [stuArr addObject:stu];
    [stu release];

    }
    return stuArr;

}

////////////

  • (void)insertMoreStudent:(Student *)stu
    {

    // 创建队列
    FMDatabaseQueue *dataQ = [FMDatabaseQueue databaseQueueWithPath:self.filePath];
    // 队列中任务要做的操作
    [dataQ inTransaction:^(FMDatabase *db, BOOL *rollback) {
    

    // [self insertDataWithStu:stu];
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO student(name, sex, age) VALUES ('%@', '%@', '%ld')", stu.name, stu.sex, stu.age];
    BOOL ret = [db executeUpdate:sql];
    if (ret) {

    NSLog(@"插入数据成功");

    } else {

    NSLog(@"插入数据失败");

    }

    }];
       

    }

@end




//
// Student.h
// UI18_FMDB
//
// Created by YIem on 16/3/4.
// Copyright © 2016年 YIem. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, assign) NSInteger age;
@end



//
// Student.m
// UI18_FMDB
//
// Created by YIem on 16/3/4.
// Copyright © 2016年 YIem. All rights reserved.
//

import "Student.h"

@implementation Student

  • (void)dealloc
    {

    [_name release];
    [_sex release];
    [super dealloc];

    }

@end

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »