| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
cmn6
10年前发布

sqlite之增删改查实例 - 实现简单的iOS收藏夹功能

使用sqlite可以实现简单的收藏夹功能,不多说直接上代码。

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

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

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



model类只是建立需要的字符串(我只写了.h .m里什么都没写)

#import <Foundation/Foundation.h>


@interface TopicDB : NSObject

@property(nonatomic, retain)NSString *topic_id;

@property(nonatomic, retain)NSString *topic_title;

@property(nonatomic, retain)NSString *topic_name;

@property(nonatomic, retain)NSString *topic_image;

@end




真正的部分是封装的这个管理类:
.h里声明相应方法,导入相应头文件

#import <sqlite3.h>

#import "TopicDB.h"//我的那个model类

//创建、关闭数据库以及增删改查

+(void)open;

+ (void)close;

+(void)create;

+ (void)insert:(TopicDB *)mymodel;

+ (void)remove:(TopicDB *)mymodel;

+ (void)update;

+ (NSArray *)select;


.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:(TopicDB *)mymodel

{

    [self open];

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

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

}

+ (void)remove:(TopicDB *)mymodel

{

    [self open];

    NSString *sql = [NSString stringWithFormat:@"delete from travel_Topic where title = '%@'",mymodel.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

            TopicDB *mymodel = [[TopicDB alloc] init];

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

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

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

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

            [arr addObject:mymodel];

        }

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

        return arr;

    }

    sqlite3_finalize(stmt);

    return [NSMutableArray array];

}

3.在所需的页面(标签4中的详情页)创建一个收藏按钮,编写一个点击事件的方法(将相应的信息存到model类里) </div>
如下:

-(void)collectionButton

{

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

    [DataBaseManager create];

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

    TopicDB *model = [[TopicDB 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:@"已收藏