设为首页 加入收藏

TOP

iOS 学习 - 4.存储聊天记录(一)
2017-10-13 10:09:38 】 浏览:1592
Tags:iOS 学习 存储 聊天记录

主要是用sqlite3来存储聊天记录

先导入sqlite3.dylib, 点 Add Other, 同时按住 shift + command + G , 在弹出的 Go to the folder 中输入 /usr/lib/libsqlite3.dylib, 就 OK 了. 还需要#import<sqlite3.h>

1.new file 一个 Text 类用来存储, .m 无需操作

1 #import <Foundation/Foundation.h>
2 
3 @interface Text : NSObject 4 //聊天内容
5 @property(nonatomic,copy)NSString *userText; 6 //聊天内容发送的时间
7 @property(nonatomic,copy)NSString *currentTime; 8 @end

2.另封装一个 TextModel 类用来操作数据

 1 #import <Foundation/Foundation.h>
 2 #import <sqlite3.h>
 3 @class Text;  4 @interface TextModel : NSObject  5 //建表
 6 -(BOOL)createList:(sqlite3 *)db;  7 //插入
 8 -(BOOL)insertList:(Text *)insertList;  9 //获取数据
10 -(NSMutableArray *)getList; 11 @end

.m

//
// TextModel.m // 保存聊天记录 //
// Copyright © 2016年 736376103@qq.com. All rights reserved. // 
#import "TextModel.h"
#import "Text.h"
@implementation TextModel //定义一个变量
static sqlite3 *_database; -(NSString *)filename{ NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; //这里打印,是方便用MesaSQLite打开sqlite3文件,可以增删改查,非常方便,主要是免费(有时间限制)~~~
    NSLog(@"%@",path); return [path stringByAppendingPathComponent:@"LIKE.sqlite"]; } -(BOOL)openDB{ //获取路径
    NSString *path = [self filename]; NSFileManager *fileManager = [NSFileManager defaultManager]; //判断数据库是否存在
    BOOL find = [fileManager fileExistsAtPath:path]; //如果为真,就打开数据库,不存在,自动创建
    if (find) { NSLog(@"database存在"); if (sqlite3_open([path UTF8String], &_database)!=SQLITE_OK) { //failed关闭,据说这个习惯好,不明觉厉
 sqlite3_close(_database); NSLog(@"打开database失败"); return NO; } //建表
 [self createList:_database]; return YES; } //同上
    if (sqlite3_open(path.UTF8String, &_database) == SQLITE_OK) { [self createList:_database]; return YES; }else{ sqlite3_close(_database); NSLog(@"打开database失败"); return NO; } return NO; } #pragma mark -- 建表
-(BOOL)createList:(sqlite3 *)db{ //我这里缺少一个主键,自己加上即可--ID INTEGER PRIMARY KEY AUTOINCREMENT
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS DRINK(userText TEXT,currentTime TEXT)"]; sqlite3_stmt *stmt; //sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
    NSInteger sqlReturn = sqlite3_prepare_v2(_database, sql.UTF8String, -1, &stmt, NULL); //-1是sql语句的长度,<0会自动计算
    if (sqlReturn != SQLITE_OK) { NSLog(@"创建表失败"); return NO; } int success = sqlite3_step(stmt); //释放stmt
 sqlite3_finalize(stmt); if (success != SQLITE_DONE) { NSLog(@"创建表失败"); return NO; } NSLog(@"创建表成功"); return YES; }
#pragma mark -- 插入 -(BOOL)insertList:(Text *)insertList{ if ([self openDB]) { sqlite3_stmt *stmt; // ? 表示待会儿插入 NSString *sql = [NSString stringWithFormat:@"INSERT INTO DRINK(userText,currentTime)VALUES(?,?)"]; //int success = sqlite3_exec(_database, sql.UTF8String, NULL, NULL, &error); int success = sqlite3_prepare_v2(_database, sql.UTF8String, -1, &stmt, NULL); if (success != SQLITE_OK) { NSLog(@"insert failed"); sqlite3_close(_database); return NO; } sqlite3_bind_text(stmt, 1, [insertList.userText UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 2, [insertList.currentTime UTF8String], -1, SQLITE_TRANSIENT); //执行插入语句 success = sqlite3_s
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS--浅谈iOS沙盒目录 下一篇iOS做新浪微博sso授权登录遇到的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目