iOS -- SQLite 实现 收藏功能

iOS -- SQLite 实现 收藏功能


1.首先导入libsqlite3.0.dylib,libz.dylib两个类库;

2.封装一个NSObject类型的工具类(DataBaseManager)去管理 (即写sql语句)

在这个类里首先导入一个头文件和你建好的model类 (实现收藏本质是存model类)

在model类的.h文件中声明需要收藏的字符串

在DataBaseManager.h文件中,声明方法:

+(void)open; // 打开数据库

+ (void)close; // 关闭数据库

+(void)create;  // 创建表

+ (void)insert:(Model*)model;   // 插入模型数据

+ (void)remove:(Model*)model;  // 删除模型数据

+ (void)update;   // 修改模型数据

+ (NSArray *)select;  // 查询模型数据

在DataBaseManager.m中实现声明的方法

#import "DataBaseManager.h"

static sqlite3 *dbPoint = nil;

@implementation DataBaseManager

+ (void)open

{

if (dbPoint) {

return;

}

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

path = [path stringByAppendingPathComponent:@"travel_Topic.rdb"];

NSLog(@"%@", path);

int result = sqlite3_open(path.UTF8String, &dbPoint);

if (result == SQLITE_OK) {

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

}

}

+ (void)close

{

sqlite3_close(dbPoint);

}

+ (void)create

{

[self open];

NSString *sql = @"create table travel_Topic (title text, name text, image text, id text)";

sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);

}

+ (void)insert:(Model*)model

{

[self open];

NSString *sql = [NSString stringWithFormat:@"insert into travel_Topic values ('%@','%@','%@','%@')", model.topic_title ,model.topic_name, model.topic_image, model.topic_id];

sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);

}

+ (void)remove:(Model*)model

{

[self open];

NSString *sql = [NSString stringWithFormat:@"delete from travel_Topic where title = '%@'",model.topic_title];

sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);

}

+ (void)update

{

[self create];

NSString *sql = @"update travel_Topic set name = '汲国兴250' where number = '250'";

sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);

}

+ (NSArray *)select

{

[self open];

//创建数据库替身

sqlite3_stmt *stmt = nil;

NSString *sql = @"select * from travel_Topic";

//执行语句

int result = sqlite3_prepare_v2(dbPoint, sql.UTF8String, -1, &stmt, NULL);

//判断是否成功

if (result == SQLITE_OK) {

//创建数组用来保存查询的数据

NSMutableArray *arr = [NSMutableArray array];

//如果还有下一行

while (sqlite3_step(stmt) == SQLITE_ROW) {

//获取数据

const unsigned char * topic_title = sqlite3_column_text(stmt, );

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

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

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

//封装成model

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

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

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

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

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

[arr addObject:model];

}

sqlite3_finalize(stmt);   //释放数据库替身

return arr;

}

sqlite3_finalize(stmt);

return [NSMutableArray array];

}

3.在所需的页面创建一个收藏按钮,编写一个点击事件的方法(将相应的信息存到model类里)

如下:

-(void)collectionButton

{

//创建数据库  数据库内同一个表不会叠加 所以放心建。

[DataBaseManager create];

//初始化model类存所需数据

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

model.topic_title = _str2;

model.topic_name = _str1;

model.topic_image = _str;

model.topic_id  = [NSString stringWithFormat:@"%lu", _Travel_Topic_Sub_Id];

//alert提示框

_alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已收藏

你可能感兴趣的:(iOS,--,SQLite,实现,收藏功能)