用数据库落成收藏作用.

那里将数据库的包装为一个工具类.将数据库设计为一个单例,封装一些开辟,成立表格,增,删,改,查的方法.在深藏作用中至关紧要选取,查询,伸张,和删除的方法.

询问,重回的数目,是你存入的数目内容.可以回到数据库的全体内容,也得以依照某个属性,重临符合该属性的内容.

代码不一致:

依照title来查询数据库中情节,重回与title一样的数据.

第一思路:

1.查询数据库的内容,遍历再次来到的数组来判断数据库中是不是有想要收藏的数量(model),以此来判断收藏提示界面的状态.

2.窖藏界面状态的更换,来判断曾几何时往数据库中插入某条数据,何时删除数据库中的某条数据.

3.查询数据库中的内容,,将数据库中的内容在其余一个界面将数据库中的model彰显出来.

快要收藏的数量(model)插入导数据库当中

落到实处该功用中遇见的题材: 

1.什么样时候插入数据,曾几何时删除数据,无法写在   遍历查询重回的数组当中
 ,会招致多条数据同时插入,同时删除.

2.判定起始的馆藏状态,必须在该界面早先化之后,界面出现从前,多注意程序走方法的次序顺序.

3.数据库建立的
表格,如若同名,后建的报表不会交替从前的表格,假使表格添加了新的一列,属性,必须在文书夹中,将原有建立的表格删除.

4.数据库存数量的小技巧,先考虑自己想要存的情节,按照想要存的情节建立model类型,

在深藏界面,用该model的属性接收来自差距界面来的model类型的性质(一般为字符串属性,分开接收,那样幸免直接接受整个model时须要判定差其余model类型)

幸免界面一样,model的习性差异 ,导致数据中蕴藏一些(null)的数据.

#import .h

@class ModelForListen;

@interface DataBaseHandler : NSObject

+ (instancetype)shareDataBaseHandler;

– (void)open;

– (void)createTable;

– (void)insertModel:(ModelForListen *) model;

– (void)updataModel:(ModelForListen *)model
ForNumber:(NSInteger)number;

– (void)deleteWithTitle:(NSString *)title;

– (void)deleteWithModel:(ModelForListen *)model;

– (NSArray *)selectForTitle:(NSString *)title;

– (NSArray *)selectForModel;

– (void)dropTable;

– (void)closeDB;

.m

////  DataBaseHandler.m//  UI21_SQLite////  Created by dllo on
16/1/11.//  Copyright © 2016年 dllo. All rights reserved.//#import
“DataBaseHandler.h”#import#import “ModelForListen.h”

@implementation DataBaseHandler

#pragma mark – 知识点 1 单例

+ (instancetype)shareDataBaseHandler {

//在静态区,只先导化三回 制造数据库对象

static DataBaseHandler *dataBase = nil;

if (dataBase == nil) {

dataBase = [[DataBaseHandler alloc] init];

}

return dataBase;

}

/** 定义一个 sqlite 结构体 (对象) */

sqlite3 *db;

#pragma mark – 知识点 2 :打开数据库

– (void)open{

// API: sqlite3_open

if (db != nil) {

NSLog(@”数据库已经拉开”);

return;

}

NSString *file =
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject]
stringByAppendingPathComponent:@”model.sqlite”];

NSLog(@”%@”,file);

/**

* @praram filename 数据库文件的不二法门(注意!类型为 c 字符串)

*

* @param ppDb 数据库对象

*

**/

int result = sqlite3_open(file.UTF8String, &db);

if (result == SQLITE_OK) {

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

}else{

NSLog(@”数据打开退步, code = %d”, result);

}

}

#pragma mark – 知识点 3 成立表格

– (void)createTable{

// API: sqlite3_exec

// 创建 SQL 语句.

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

NSString *createTableSQL = @”CREATE TABLE IF NOT EXISTS
lanou1130(number INTEGER PRIMARY KEY AUTOINCREMENT, title text,
lastUptrackTitle text, coverSmall text, albumId text, nickname text,
coverLarge text, trackTitle text)”;

int result = sqlite3_exec(db, createTableSQL.UTF8String, NULL, NULL,
nil);

if (result == SQLITE_OK) {

NSLog(@”创建表格成功”);

}else{

NSLog(@”创制表格败北. code :%d”, result);

}

}

#pragma mark – 知识点 4 插入数据

– (void)insertModel:(ModelForListen *)model {

// API :sqlite3_exec

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

NSString *insertSQL = [NSString stringWithFormat:@”INSERT into
lanou1130(title, lastUptrackTitle, coverSmall, albumId, nickname,
coverLarge, trackTitle) VALUES(‘%@’, ‘%@’, ‘%@’, ‘%@’, ‘%@’, ‘%@’,
‘%@’)”, model.title, model.lastUptrackTitle,
model.coverSmall,model.albumId,model.nickname,model.coverLarge,model.trackTitle];

int result = sqlite3_exec(db, insertSQL.UTF8String, nil, nil, nil);

if (result == SQLITE_OK) {

NSLog(@”添加数据形成”);

}else{

NSLog(@”添加数据败北.code:%d”, result);

}

}

#pragma mark – 知识点 5 更新数据

– (void)updataModel:(ModelForListen *)model
ForNumber:(NSInteger)number{

//API :sqlite2_exec

NSString *updataSQL = [NSString stringWithFormat:@”UPDATE lanou1130
SET title = ‘%@’, lastUptrackTitle = ‘%@’, coverSmall = ‘%@’ albumId =
‘%@’  nickname = ‘%@’ coverLarge = ‘%@’ trackTitle = ‘%@’ WHERE number =
‘%ld'”,model.title, model.lastUptrackTitle, model.coverSmall,
model.albumId,model.nickname,model.coverLarge,model.trackTitle,
number];

int result = sqlite3_exec(db, updataSQL.UTF8String, nil, nil, nil);

if (result == SQLITE_OK) {

NSLog(@”更新成功”);

}else {

NSLog(@”更新失利, code:%d”, result);

}

}

#pragma mark — 知识点 6 :删除数据

– (void)deleteWithTitle:(NSString *)title {

//API sqlite3_exec

NSString *deleteSQL = [NSString stringWithFormat:@”DELETE FROM
lanou1130 WHERE title = ‘%@'”,title];

int result = sqlite3_exec(db, deleteSQL.UTF8String, nil, nil, nil);

if (result == SQLITE_OK) {

NSLog(@”删除成功”);

}else{

NSLog(@”删除败北 code:%d”, result);

}

}

– (void)deleteWithModel:(ModelForListen *)model {

NSString *deleteSQL = [NSString stringWithFormat:@”DELETE FROM
lanou1130 WHERE title = ‘%@'”,model.title];

int result = sqlite3_exec(db, deleteSQL.UTF8String, nil, nil, nil);

if (result == SQLITE_OK) {

NSLog(@”删除成功”);

}else{

NSLog(@”删除失利 code:%d”, result);

}

}

#pragma mark –知识点 7  查询数据

– (NSArray *)selectForTitle:(NSString *)title{

/** 存放符合条件的询问数据 (元素是 model 对象)*/

NSMutableArray *arr = [NSMutableArray array];

// sql 语句

NSString *selectSQL = [NSString stringWithFormat:@”SELECT * FROM
lanou1130 WHERE title = ‘%@'”, title];

//API : stmt 结构体, sqlite3_prepareV2,sqlite3_step, sqlite_column

// 声Bellamy个社团体 stmt 对象 (结构体)

sqlite3_stmt *stmt = nil;

//(准备好的说话对象)

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

if (result == SQLITE_OK) {

// 逐行查询 每行都执行查询语句

while (sqlite3_step(stmt) == SQLITE_ROW) {

// 假若查询条件非凡, 通过 sqlite3_column 函数蔟 取出值;

const unsigned char *title = sqlite3_column_text(stmt, 1);//
取出哪一列的数据值

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

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

const unsigned char *albumId = sqlite3_column_text(stmt, 4);

const unsigned char *nickname = sqlite3_column_text(stmt, 5);

const unsigned char *coverLarge = sqlite3_column_text(stmt, 6);

const unsigned char *trackTitle = sqlite3_column_text(stmt, 7);

//          int age = sqlite3_column_int(stmt, 3);

// 创设 model 对象, 赋值之后放入数组中

ModelForListen *model = [[ModelForListen alloc] init];

model.title = [NSString stringWithUTF8String:(const char *) title];

model.lastUptrackTitle = [NSString stringWithUTF8String:(const char*)
lastUptrackTitle];

model.coverSmall = [NSString stringWithUTF8String:(const char*)
coverSmall];

model.albumId = [NSString stringWithUTF8String:(const char*)albumId];

model.nickname = [NSString stringWithUTF8String:(const
char*)nickname];

model.coverLarge = [NSString stringWithUTF8String:(const
char*)coverLarge];

model.trackTitle = [NSString stringWithUTF8String:(const
char*)trackTitle];

[arr addObject:model];

}

NSLog(@”准备妥当,查询中…”);

// 销毁对象

sqlite3_finalize(stmt);

}else {

NSLog(@”无法健康查询, code:%d”, result);

// 销毁对象

sqlite3_finalize(stmt);

}

return arr;

}

– (NSArray *)selectForModel {

/** 存放符合条件的询问数据 (元素是 model 对象)*/

NSMutableArray *arr = [NSMutableArray array];

// sql 语句

NSString *selectSQL = [NSString stringWithFormat:@”SELECT * FROM
lanou1130″];

//API : stmt 结构体, sqlite3_prepareV2,sqlite3_step, sqlite_column

// 声明一(Wissu)(Beingmate)(Meadjohnson)个结构体 stmt 对象 (结构体)

sqlite3_stmt *stmt = nil;

//(准备好的言语对象) -1 代表询问所有对象.

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

if (result == SQLITE_OK) {

// 逐行查询 每行都举行查询语句

while (sqlite3_step(stmt) == SQLITE_ROW) {

// 假设查询条件相当, 通过 sqlite3_column 函数蔟 取出值;

const unsigned char *title = sqlite3_column_text(stmt, 1);//
取出哪一列的数据值

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

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

const unsigned char *albumId = sqlite3_column_text(stmt, 4);

const unsigned char *nickname = sqlite3_column_text(stmt, 5);

const unsigned char *coverLarge = sqlite3_column_text(stmt, 6);

const unsigned char *trackTitle = sqlite3_column_text(stmt, 7);

//          int age = sqlite3_column_int(stmt, 3);

// 创立 model 对象, 赋值之后放入数组中

ModelForListen *model = [[ModelForListen alloc] init];

model.title = [NSString stringWithUTF8String:(const char *) title];

model.lastUptrackTitle = [NSString stringWithUTF8String:(const char*)
lastUptrackTitle];

model.coverSmall = [NSString stringWithUTF8String:(const char*)
coverSmall];

model.albumId = [NSString stringWithUTF8String:(const char*)albumId];

model.nickname = [NSString stringWithUTF8String:(const
char*)nickname];

model.coverLarge = [NSString stringWithUTF8String:(const
char*)coverLarge];

model.trackTitle = [NSString stringWithUTF8String:(const
char*)trackTitle];

[arr addObject:model];

}

NSLog(@”准备妥当,查询中…”);

// 销毁对象

sqlite3_finalize(stmt);

}else {

NSLog(@”不能正常查询, code:%d”, result);

// 销毁对象

sqlite3_finalize(stmt);

}

return arr;

}

#pragma mark –知识点 8 删除表单

-(void)dropTable{

NSString *dropSQL = @”DROP TABLE lanou1130″;

int result = sqlite3_exec(db, dropSQL.UTF8String, nil, nil, nil);

if (result == SQLITE_OK) {

NSLog(@”删除表格成功!!!!”);

}else{

NSLog(@”删除败北”);

}

}

#pragma mark –关掉数据库

– (void)closeDB {

// API:sqlite3_close

int result = sqlite3_close(db);

if (result == SQLITE_OK) {

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

db = nil;

}else{

NSLog(@”数据库关闭战败. code :%d”, result);

}

}

@end

网站地图xml地图