UI进阶之记事本4

创建model类   WordNote

WordNote.h中

添加几个属性

//记事本ID

@property (nonatomic, assign) NSInteger WID;

//记事本Title

@property (nonatomic, copy) NSString *title;

//记事本记录的内容

@property (nonatomic, copy) NSString *note;

//记录时间

@property (nonatomic, copy) NSString *time;

//文件夹的ID

@property (nonatomic, assign) NSInteger FID;

创建model类  FileMessage

FileMessage.h中

声明属性

//文件夹title

@property (nonatomic, copy) NSString *title;

//记录时间

@property (nonatomic, copy) NSString *time;

//文件夹的id

@property (nonatomic, assign) NSInteger FID;

创建DataBaseHandle

DataBaseHandle.h中

//引入头文件

#import "WordNote.h"

#import "FileMessage.h"

//添加方法


//数据库单例

+ (DataBaseHandle *)sharedDataBaseHandle;

//打开数据库

- (void)openDB;

//关闭数据库

- (void)closeDB;

//创建记事本表

- (void)createWordNoteTable;

//创建文件夹表

- (void)createfileMessageTable;

//向WOrdNote里面插入一个WordNote类型的model

- (void)insertToWordNoteWith:(WordNote *)model;

//向fileMessage 表里面插入fileMessage类型的model

- (void)insertIntoFileMessageWith:(FileMessage *)model;

//根据WID删除掉wordNote里面的某条数据

- (void)deleteFromWordNoteWithWID:(NSInteger )WID;

//根据FID删掉WordNote里的一堆数据

- (void)deleteFromWordNoteWithFID:(NSInteger)FID;

//根据FID删掉FileMessage里的一条数据

- (void)deleteFromFileMessageWithFID:(NSInteger)FID;

//根据FID在WordNote里面查询数据

- (NSMutableArray *)searchFromWordNoteWithFID:(NSInteger )FID;

//查询FileMessage里面的所有数据

- (NSMutableArray *)searchAllFromFileMessage;

DataBaseHandle.m中


#import

#import@interface DataBaseHandle()

@property (nonatomic, copy) NSString *dbPath;

@end

//设置全局变量,

static DataBaseHandle *dataBase = nil;

/数据库单例

+ (DataBaseHandle *)sharedDataBaseHandle

{

if (dataBase == nil) {

dataBase = [[DataBaseHandle alloc]init];

}

return dataBase;

}


//懒加载

- (NSString *)dbPath

{

if (_dbPath == nil) {

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

_dbPath = [document stringByAppendingPathComponent:@"word.sqlite"];

}

return _dbPath;

}


static sqlite3 *db = nil;

//打开数据库

- (void)openDB

{

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

if (result == SQLITE_OK)

{

NSLog(@"打开成功");

}else

{

NSLog(@"打开失败");

}

NSLog(@"databasePath == %@", _dbPath);

}

//关闭数据库

- (void)closeDB

{

int result = sqlite3_close(db);

if (result == SQLITE_OK)

{

NSLog(@"关闭成功");

}else

{

NSLog(@"关闭失败");

}

}

//创建记事本表

- (void)createWordNoteTable

{

NSString *createTable = @"create table  WordNote (WID integer primary key autoincrement, title text, note text, time text, FID integer)";

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

if (result == SQLITE_OK)

{

NSLog(@"创建记事本表成功");

} else

{

NSLog(@"创建失败 %d", result);

}

}

//创建文件夹表

- (void)createfileMessageTable

{

NSString *creatafile = @"create table if not exists fileMessage (FID integer primary key autoincrement, title text, time text)";

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

if (result == SQLITE_OK)

{

NSLog(@"创建文件夹表成功");

} else

{

NSLog(@"创建失败");

}

}

//向WOrdNote里面插入一个WordNote类型的model

- (void)insertToWordNoteWith:(WordNote *)model

{

//插入流程

//第一步创建sql语句

NSString *insdrtString= @"insert into WordNote (title, note, time, FID) values (?, ?, ?, ?)";

//第二步:创建伴随指针

sqlite3_stmt *stmt = nil;

//第三步:使用sqlite3_prepare方法预执行sql语句

int result = sqlite3_prepare(db, insdrtString.UTF8String, -1, &stmt, NULL);

//第四步: 判断是否预执行成功,如果成功 进行下一步

if (result == SQLITE_OK) {

//第五步:向?的位置插入数据

sqlite3_bind_text(stmt, 1, model.title.UTF8String, -1, NULL);

sqlite3_bind_text(stmt, 2, model.note.UTF8String, -1, NULL);

sqlite3_bind_text(stmt, 3, model.time.UTF8String, -1, NULL);

sqlite3_bind_int64(stmt, 4, model.FID);

//第六步:判断伴随指针是否完成

if (sqlite3_step(stmt) == SQLITE_DONE) {

NSLog(@"插入成功");

}

}

//第七步: 记得释放伴随指针

sqlite3_finalize(stmt);

}

//向fileMessage 表里面插入fileMessage类型的model

- (void)insertIntoFileMessageWith:(FileMessage *)model{

//插入流程

//第一步创建sql语句

NSString *fileMessageString = @"insert into fileMessage ( title, time) values (?, ?)";

//第二步:创建伴随指针

sqlite3_stmt *stmt = nil;

//第三步:使用sqlite3_prepare方法预执行sql语句

int result = sqlite3_prepare(db, fileMessageString.UTF8String, -1, &stmt, NULL);

//第四步: 判断是否预执行成功,如果成功 进行下一步

if (result == SQLITE_OK) {

//第五步:向?的位置插入数据

sqlite3_bind_text(stmt, 1, model.title.UTF8String, -1, NULL);

sqlite3_bind_text(stmt, 2, model.time.UTF8String, -1, NULL);

//第六步:判断伴随指针是否完成

if (sqlite3_step(stmt) == SQLITE_DONE) {

NSLog(@"插入成功");

}

}

//第七步: 记得释放伴随指针

sqlite3_finalize(stmt);

}

#pragma mark --删除方法

//根据WID删除掉wordNote里面的某条数据

- (void)deleteFromWordNoteWithWID:(NSInteger )WID

{

//删除流程

//第一步:创建sql语句

NSString *deleteString = [NSString stringWithFormat:@"delete from WordNote where WID = %ld", WID];

//第二步:执行sql语句

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

if (result == SQLITE_OK) {

NSLog(@"删除成功");

} else

{

NSLog(@"删除失败");

}

}

//根据FID删掉WordNote里的一堆数据

- (void)deleteFromWordNoteWithFID:(NSInteger)FID

{

NSString *deleteString = [NSString stringWithFormat:@"delete from WordNote where FID = %ld", FID];

int reslut = sqlite3_exec(db, deleteString.UTF8String, NULL, NULL, NULL);

if (reslut == SQLITE_OK) {

NSLog(@"删除成功");

}else

{

NSLog(@"删除失败");

}

}

//根据FID删掉FileMessage里的一条数据

- (void)deleteFromFileMessageWithFID:(NSInteger)FID

{

NSString *deleteString = [NSString stringWithFormat:@"delete from fileMessage where FID = %ld", FID];

int reslut = sqlite3_exec(db, deleteString.UTF8String, NULL, NULL, NULL);

if (reslut == SQLITE_OK) {

NSLog(@"删除成功");

}else

{

NSLog(@"删除失败");

}

}

//根据FID在WordNote里面查询数据

- (NSMutableArray *)searchFromWordNoteWithFID:(NSInteger )FID

{

//查询步骤

//第一步:创建sql语句

NSString *searchString = @"select * from WordNote where FID = ?";

//第二步:伴随指针

sqlite3_stmt *stmt = nil;

//第三步:让查询语句 预处理

int result = sqlite3_prepare(db, searchString.UTF8String, -1, &stmt, NULL);

NSMutableArray *resultArray = [NSMutableArray array];

//第四步: 判断预处理数据

if (result == SQLITE_OK)

{

//第四点五步:如果查询语句有 ? 需要一部绑定的过程

sqlite3_bind_int64(stmt, 1, FID);

//第五步 循环取值

while (sqlite3_step(stmt)  == SQLITE_ROW)

{

WordNote *model = [WordNote new];

model.WID = sqlite3_column_int(stmt, 0);

model.title = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];

model.note = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];

model.time = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];

model.FID = sqlite3_column_int(stmt, 4);

[resultArray addObject:model];

}

}

sqlite3_finalize(stmt);

return resultArray;

}

//查询FileMessage里面的所有数据

- (NSMutableArray *)searchAllFromFileMessage

{

//查询步骤

//第一步:创建sql语句

NSString *searchString = @"select  * from fileMessage";

//第二步:伴随指针

sqlite3_stmt *stmt = nil;

//第三步:让查询语句 预处理

int reslut = sqlite3_prepare(db, searchString.UTF8String, -1, &stmt, NULL);

NSMutableArray *searchArray = [NSMutableArray array];

//第四步: 判断预处理数据

if (reslut == SQLITE_OK)

{

//第五步 循环取值

while (sqlite3_step(stmt) == SQLITE_ROW)

{

FileMessage *model = [FileMessage new];

model.FID = sqlite3_column_int(stmt, 0);

model.title = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];

model.time = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];

[searchArray addObject:model];

}

}

sqlite3_finalize(stmt);

return searchArray;

}



 创建MyLongPressGestureRecognizer类

MyLongPressGestureRecognizer.h中

#warning 这是新的类

@interface MyLongPressGestureRecognizer : UILongPressGestureRecognizer

// 因为原始的长按手势没有indexPath属性,所以自定义一个长按类,添加一个indexPath属性

@property (nonatomic, strong) NSIndexPath *indexPath;

@end

你可能感兴趣的:(UI进阶之记事本4)