用數(shù)據(jù)庫實現(xiàn)收藏功能.

這里將數(shù)據(jù)庫的封裝為一個工具類.將數(shù)據(jù)庫設計為一個單例,封裝一些打開,創(chuàng)建表格,增,刪,改,查的方法.在收藏功能中主要用到,查詢,增添,和刪除的方法.

查詢,返回的數(shù)據(jù),是你存入的數(shù)據(jù)內(nèi)容.可以返回數(shù)據(jù)庫的全部內(nèi)容,也可以根據(jù)某個屬性,返回符合該屬性的內(nèi)容.

代碼區(qū)別:

根據(jù)title來查詢數(shù)據(jù)庫中內(nèi)容,返回與title一樣的數(shù)據(jù).



主要思路:

1.查詢數(shù)據(jù)庫的內(nèi)容,遍歷返回的數(shù)組來判斷數(shù)據(jù)庫中是否有想要收藏的數(shù)據(jù)(model),以此來判斷收藏提示界面的狀態(tài).

2.收藏界面狀態(tài)的更換,來判斷什么時候往數(shù)據(jù)庫中插入某條數(shù)據(jù),什么時候刪除數(shù)據(jù)庫中的某條數(shù)據(jù).



3.查詢數(shù)據(jù)庫中的內(nèi)容,,將數(shù)據(jù)庫中的內(nèi)容在另外一個界面將數(shù)據(jù)庫中的model展現(xiàn)出來.

將要收藏的數(shù)據(jù)(model)插入導數(shù)據(jù)庫當中



實現(xiàn)該功能中遇到的問題:?

1.什么時候插入數(shù)據(jù),什么時候刪除數(shù)據(jù),不能寫在 ? 遍歷查詢返回的數(shù)組當中 ?,會導致多條數(shù)據(jù)同時插入,同時刪除.

2.判斷起初的收藏狀態(tài),必須在該界面初始化之后,界面出現(xiàn)之前,多注意程序走方法的先后順序.

3.數(shù)據(jù)庫建立的 表格,若是同名,后建的表格不會替換之前的表格,若是表格添加了新的一列,屬性,必須在文件夾中,將原始建立的表格刪除.

4.數(shù)據(jù)庫存數(shù)據(jù)的小技巧,先考慮自己想要存的內(nèi)容,根據(jù)想要存的內(nèi)容建立model類型,

在收藏界面,用該model的屬性接收來自不同界面來的model類型的屬性(一般為字符串屬性,分開接收,這樣避免直接接收整個model時需要判斷不同的model類型)

避免界面一樣,model的屬性不同 ,導致數(shù)據(jù)中存儲一些(null)的數(shù)據(jù).


#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 {

//在靜態(tài)區(qū),只初始化一次 創(chuàng)建數(shù)據(jù)庫對象

static DataBaseHandler *dataBase = nil;

if (dataBase == nil) {

dataBase = [[DataBaseHandler alloc] init];

}

return dataBase;

}

/** 定義一個 sqlite 結(jié)構(gòu)體 (對象) */

sqlite3 *db;

#pragma mark - 知識點 2 :打開數(shù)據(jù)庫

- (void)open{

// API: sqlite3_open

if (db != nil) {

NSLog(@"數(shù)據(jù)庫已經(jīng)開啟");

return;

}

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

NSLog(@"%@",file);

/**

* @praram filename 數(shù)據(jù)庫文件的路徑(注意!類型為 c 字符串)

*

* @param ppDb 數(shù)據(jù)庫對象

*

**/

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

if (result == SQLITE_OK) {

NSLog(@"數(shù)據(jù)庫打開成功");

}else{

NSLog(@"數(shù)據(jù)打開失敗, code = %d", result);

}

}

#pragma mark - 知識點 3 創(chuàng)建表格

- (void)createTable{

// API: sqlite3_exec

// 創(chuàng)建 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(@"創(chuàng)建表格成功");

}else{

NSLog(@"創(chuàng)建表格失敗. code :%d", result);

}

}

#pragma mark - 知識點 4 插入數(shù)據(jù)

- (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(@"添加數(shù)據(jù)完成");

}else{

NSLog(@"添加數(shù)據(jù)失敗.code:%d", result);

}

}

#pragma mark - 知識點 5 更新數(shù)據(jù)

- (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 :刪除數(shù)據(jù)

- (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? 查詢數(shù)據(jù)

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

/** 存放符合條件的查詢數(shù)據(jù) (元素是 model 對象)*/

NSMutableArray *arr = [NSMutableArray array];

// sql 語句

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

//API : stmt 結(jié)構(gòu)體, sqlite3_prepareV2,sqlite3_step, sqlite_column

// 聲明一個結(jié)構(gòu)體 stmt 對象 (結(jié)構(gòu)體)

sqlite3_stmt *stmt = nil;

//(準備好的語句對象)

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

if (result == SQLITE_OK) {

// 逐行查詢 每行都執(zhí)行查詢語句

while (sqlite3_step(stmt) == SQLITE_ROW) {

// 如果查詢條件匹配, 通過 sqlite3_column 函數(shù)蔟 取出值;

const unsigned char *title = sqlite3_column_text(stmt, 1);// 取出哪一列的數(shù)據(jù)值

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);

// 創(chuàng)建 model 對象, 賦值之后放入數(shù)組中

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 {

/** 存放符合條件的查詢數(shù)據(jù) (元素是 model 對象)*/

NSMutableArray *arr = [NSMutableArray array];

// sql 語句

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

//API : stmt 結(jié)構(gòu)體, sqlite3_prepareV2,sqlite3_step, sqlite_column

// 聲明一個結(jié)構(gòu)體 stmt 對象 (結(jié)構(gòu)體)

sqlite3_stmt *stmt = nil;

//(準備好的語句對象) -1 代表查詢?nèi)繉ο?

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

if (result == SQLITE_OK) {

// 逐行查詢 每行都執(zhí)行查詢語句

while (sqlite3_step(stmt) == SQLITE_ROW) {

// 如果查詢條件匹配, 通過 sqlite3_column 函數(shù)蔟 取出值;

const unsigned char *title = sqlite3_column_text(stmt, 1);// 取出哪一列的數(shù)據(jù)值

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);

// 創(chuàng)建 model 對象, 賦值之后放入數(shù)組中

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 --關掉數(shù)據(jù)庫

- (void)closeDB {

// API:sqlite3_close

int result = sqlite3_close(db);

if (result == SQLITE_OK) {

NSLog(@"數(shù)據(jù)庫關閉成功");

db = nil;

}else{

NSLog(@"數(shù)據(jù)庫關閉失敗. code :%d", result);

}

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末报慕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件喷斋,死亡現(xiàn)場離奇詭異,居然都是意外死亡兑巾,警方通過查閱死者的電腦和手機损姜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門庵寞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人薛匪,你說我怎么就攤上這事捐川。” “怎么了逸尖?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵古沥,是天一觀的道長。 經(jīng)常有香客問我娇跟,道長岩齿,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任苞俘,我火速辦了婚禮盹沈,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吃谣。我一直安慰自己乞封,他們只是感情好,可當我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布岗憋。 她就那樣靜靜地躺著肃晚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仔戈。 梳的紋絲不亂的頭發(fā)上关串,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音监徘,去河邊找鬼晋修。 笑死,一個胖子當著我的面吹牛凰盔,可吹牛的內(nèi)容都是我干的墓卦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼廊蜒,長吁一口氣:“原來是場噩夢啊……” “哼趴拧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起山叮,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤著榴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后屁倔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脑又,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了问麸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片往衷。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖严卖,靈堂內(nèi)的尸體忽然破棺而出席舍,到底是詐尸還是另有隱情,我是刑警寧澤哮笆,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布来颤,位于F島的核電站稠肘,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撞芍,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望璃氢。 院中可真熱鬧,春花似錦、人聲如沸舆蝴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背芭挽。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工蝗肪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留袜爪,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓薛闪,卻偏偏與公主長得像辛馆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子豁延,可洞房花燭夜當晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容