sqlite数据库

2016-05-23  本文已影响35人  71150ce14a00

.h

#import#import#import "Product.h"

@interface DataBase : NSObject

{

sqlite3 *dbPoint;

}

// 想要在任何一个类中使用同一个数据库 把数据库管理类写成一个单例

+ (DataBase *)shareInstance;

// 数据库操作步骤: 打开数据库-->创建表(只需要创建一次) --> 操作数据(增删改查) --> 关闭数据库

// 打开数据库

- (void)openDB;

// 关闭数据库

- (void)closeDB;

// 创建表

- (void)createTable;

// 添加数据

- (void)insertInfoWithLanouStu:(Product *)product;

// 删除数据

- (void)deleteInfoWithNum:(NSString *)pID;

//查询

- (NSMutableArray *)selectInfo;

// 删除表

- (void) dropTable;

// 修改

- (void)updateInfoLanouStu:(Product *)product pID:(NSString *)pID;

@end


.m

//

//  DataBase.m

//  LazyCat

//

//  Created by jike on 15/10/21.

//  Copyright (c) 2015年 zhanshu. All rights reserved.

//

#import "DataBase.h"

@implementation DataBase

+(DataBase *)shareInstance

{

// 通过GCD多线程 实现单例

static DataBase *dbManager = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

dbManager = [[DataBase alloc]init];

});

return dbManager;

}

// 打开数据库

-(void)openDB

{

//数据库在本地创建一个数据文件 一般存放在 documents 文件下

// 获取documents路径

NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;

NSLog(@"------------%@",path);

// 拼接数据库路径(后缀.db/.sqlite)

NSString *dbPath = [path stringByAppendingPathComponent:@"product.db"];

// 创建数据库

// 数据库打开函数 会检测路径下 是否已经存在数据库(看dbPointer指针 是否指向了数据库) 如果不存在 则创建一个新的数据库 如果存在 直接打开原有的数据库

// 参数1: 本地数据库路径(UTF-8转码)

// 参数2: 数据库指针

int result = sqlite3_open([dbPath UTF8String], &dbPoint);

NSLog(@"打开结果:%d, path:%@",result, dbPath);

}

// 关闭数据库

- (void)closeDB

{

sqlite3_close(dbPoint);

}

// 创建表

- (void)createTable

{

// 1. 创建sql语句

// create table 创建表

// if not exists 如果不存在

// primary key 主键 (用来作为每一条信息的唯一标识)

// autoincrement 自增

NSString *createSQL = @"create table if not exists product (name text, pID text,pCount text,pyuding text, num integer primary key autoincrement)";

// 2. 执行语句

// 参数1: 数据库指针

// 参数2: sql语句(oc转码c)

int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, NULL);

// 3. 判断

//    if (result == SQLITE_OK) {

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

//    }else{

//        NSLog(@"创建表失败,原因%d",result);

//    }

[self judgeResult:result type:@"创建表"];

}

// 添加方法

- (void)insertInfoWithLanouStu:(Product *)product

{

// 1. 创建sql语句

NSString *insertSQL = [NSString stringWithFormat:@"insert into product (name, pID, pCount,pyuding) values ('%@','%@','%@', '%@')",product.name,product.pID,product.pCount,product.pyuding];

// 2.执行

int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);

// 3. 判断

[self judgeResult:result type:@"插入"];

}

// 删除数据

-(void)deleteInfoWithNum:(NSString *)pID

{

// 1. sql语句

// 从哪个表删除

NSString *deleteSQL = [NSString stringWithFormat:@"delete from product where pID = '%@'",pID];

// 2. 执行

int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, NULL);

// 3. 判断

[self judgeResult:result type:@"删除"];

}

// 查询数据

- (NSMutableArray *)selectInfo

{

// 查询操作逻辑

// 1.从本地数据所有信息 遍历获取每条信息

// 2. 把每条信息 转化为model对象

// 3. 把model添加到数值进行返回

// 1. sql语句

NSString *selectSQL = @"select * from product";

// 2. 创建数据库替身

sqlite3_stmt *stmt = nil;

// 3. 准备sql语句

// prepare_v2函数 把数据库对象(dbPointer)/sql语句/数据库替身 关联一起

int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);

// 4. 创建用于返回数据的数组

NSMutableArray *arr = [NSMutableArray array];

// 5. 判断查询准备是否成功

if (result == SQLITE_OK) {

NSLog(@"查询准备成功");

// 6. 开始遍历每一行信息

while (sqlite3_step(stmt) == SQLITE_ROW) {

// 当数据库替身指向的数据符合查询条件 在while中返回

// 逐行获取每一列信息

// 列数 从0开始

const unsigned char *name = sqlite3_column_text(stmt, 0);

const unsigned char *pid = sqlite3_column_text(stmt, 1);

const unsigned char *pcount = sqlite3_column_text(stmt, 2);

const unsigned char *pyuding = sqlite3_column_text(stmt, 3);

// 把获取到的数据信息保存在model中

Product *temp = [[Product alloc]init];

temp.name = [NSString stringWithUTF8String:(const char *)name];

temp.pID = [NSString stringWithUTF8String:(const char *)pid];

temp.pCount = [NSString stringWithUTF8String:(const char *)pcount];

temp.pyuding = [NSString stringWithUTF8String:(const char *)pyuding];

// 添加到数组中

[arr addObject:temp];

}

}else{

NSLog(@"查询准备失败,原因:%d",result);

}

return  arr;

}

// 删除表

- (void)dropTable

{

// 1. sql语句

NSString *dropSQL = @"drop table product";

// 2. 执行

int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, NULL);

// 3. 判断

[self judgeResult:result type:@"删除表"];

}

// 修改表

- (void)updateInfoLanouStu:(Product *)product pID:(NSString *)pID

{

// 1. 创建sql语句

NSString *updateSQL = [NSString stringWithFormat:@"update product set pCount = '%@' where pID = '%@'",product.pCount,pID];

// 2. 执行

int result = sqlite3_exec(dbPoint, updateSQL.UTF8String, NULL, NULL, NULL);

// 3. 判断

[self judgeResult:result type:@"修改"];

}

// 判断方法

- (void)judgeResult:(int)r type:(NSString *)type

{

if (r == SQLITE_OK) {

NSLog(@"%@操作成功",type);

}else{

NSLog(@"%@操作失败,原因:%d",type,r);

}

}

@end

上一篇 下一篇

猜你喜欢

热点阅读