mysql实现记事本_记事本 __ 数据库

本文介绍了如何在iOS应用中利用SQLite数据库实现记事本功能,包括创建记事本表和文件夹表,进行数据的增删查改操作。详细展示了AppDelegate中的数据库初始化代码以及创建表的SQL语句,同时还提供了用于插入、删除和查询数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目功能

1. 记录自己创建的事件

(1) 可以保存在系统文件夹

(2) 也可以保存在自定义的文件夹

2. 创建自定义文件夹

实现方法

sqlite

1. 记事本表

(1) WID , title , note , time , FID

(2)    创建表

(3)    插入数据

(4) 删除数据

(5) 根据FID查询数据

(6)    更改数据

2. 文件夹表

(1) FID ,  title , time

(2) 创建表

(3) 插入数据

(4) 查询数据(All)

(5) 删除数据

代码实现

--- AppDelegate.m

1 #import "AppDelegate.h"

2 #import "MainListViewController.h"

3 #import "DataBaseHandle.h"

4

5 @interface AppDelegate ()6

7 @end8

9 @implementation AppDelegate10

11

12 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {13

14 self.window =[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];15

16

17 self.window.backgroundColor =[UIColor whiteColor];18

19 [self.window makeKeyAndVisible];20

21 //创建表

22 DataBaseHandle *dataBase =[DataBaseHandle shareDataBaseHandle];23 [dataBase openDB];24

25 [dataBase creatWordNoteTable];26

27 [dataBase creatFileMessageTable];28

29 [dataBase closeDB];30

31

32 MainListViewController *mainListVC =[[MainListViewController alloc] init];33

34

35 UINavigationController *mainListNaviVC =[[UINavigationController alloc] initWithRootViewController:mainListVC];36

37 self.window.rootViewController =mainListNaviVC;38

39 returnYES;40 }

---DataBaseHandle.h

#import #import"WordNote.h"#import"FileMessage.h"@interface DataBaseHandle : NSObject//数据库单例

+ (DataBaseHandle *)shareDataBaseHandle;//打开数据库

- (void)openDB;//关闭数据库

- (void)closeDB;//创建记事本表

- (void)creatWordNoteTable;//创建文件夹表

- (void)creatFileMessageTable;//向WoedNote表里边插入一个WoedNote类型的model

- (void)insertIntoWordNoteWith:(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

1 #import "DataBaseHandle.h"

2 #import

3

4 @interface DataBaseHandle ()5

6 @property (nonatomic, strong) NSString *dbPath;7

8 @end9

10 static DataBaseHandle *dataBase =nil;11

12 @implementation DataBaseHandle13

14 + (DataBaseHandle *)shareDataBaseHandle15 {16 if (dataBase ==nil ) {17 dataBase = [DataBaseHandle new];18 }19 returndataBase;20 }21

22 - (NSString *)dbPath23 {24 if (!_dbPath) {25 NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];26 _dbPath = [document stringByAppendingPathComponent:@"wordnote.sqlite"];27 }28 return_dbPath;29 }30

31 static sqlite3 *db =nil;32

33 - (void)openDB34 {35 int result = sqlite3_open(self.dbPath.UTF8String, &db);36

37 if (result ==SQLITE_OK) {38 NSLog(@"打开成功");39 }else{40 NSLog(@"失败了");41 }42

43 }44

45 - (void)closeDB46 {47 int result =sqlite3_close(db);48 if (result ==SQLITE_OK) {49 NSLog(@"关闭成功");50 }else{51 NSLog(@"关不上");52 }53

54 }55

56 - (void)creatWordNoteTable57 {58 NSString *creatString = @"create table if not exists WordNote (WID integer primary key autoincrement,title text, note text, time text, FID integer)";59 int result =sqlite3_exec(db, creatString.UTF8String, NULL, NULL, NULL);60

61 if (result ==SQLITE_OK) {62 NSLog(@"创表成功");63 }else

64 {65 NSLog(@"创表失败");66 }67 }68

69 - (void)creatFileMessageTable70 {71 NSString *creatString = @"create table if not exists FileMessage (FID integer primary key autoincrement ,title text, time text)";72

73 int result =sqlite3_exec(db, creatString.UTF8String, NULL, NULL, NULL);74

75 if (result ==SQLITE_OK) {76 NSLog(@"创建成功");77 }else

78 {79 NSLog(@"创你妹");80 }81 }82

83 - (void)insertIntoWordNoteWith:(WordNote *)model84 {85 //插入流程86 //第一步:创建一个SQL语句 insert into 表名 (字段) values (多少个字段要有多少个?)

87 NSString *insertString = @"insert into WordNote (title, note, time, FID) values (?, ?, ?, ?)";88 //第二步 : 创建一个伴随指针

89 sqlite3_stmt *stmt =nil;90 //第三步 : 使用sqlite3_prepare方法预执行SQL语句

91 int result = sqlite3_prepare(db, insertString.UTF8String, -1, &stmt, NULL);92 //第四步 : 判断是否预执行成功,如果成功了 进行下一步

93 if (result ==SQLITE_OK) {94 //第五步 : 向?的位置 插入数据

95 sqlite3_bind_text(stmt, 1, model.title.UTF8String, -1, NULL);96 sqlite3_bind_text(stmt, 2, model.note.UTF8String, -1, NULL);97 sqlite3_bind_text(stmt, 3, model.time.UTF8String, -1, NULL);98 sqlite3_bind_int64(stmt, 4, model.FID);99 //第六步 : 判断伴随指针是否完成

100 if (sqlite3_step(stmt) ==SQLITE_DONE) {101 NSLog(@"插入成功");102 }103 }else

104 {105

106 NSLog(@"错误信息 result== %d",result);107

108 }109

110 //第七步: 释放伴随指针

111 sqlite3_finalize(stmt);112 }113

114 - (void)insertIntoFileMessageWith:(FileMessage *)model115 {116 NSString *insertString = @"insert into FoleMessage (title, time) values (?, ?)";117

118 sqlite3_stmt *stmt =nil;119

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

122 if (result ==SQLITE_OK) {123 sqlite3_bind_text(stmt, 1, model.title.UTF8String, -1, NULL);124 sqlite3_bind_text(stmt, 2, model.time.UTF8String, -1, NULL);125 if (sqlite3_step(stmt) ==SQLITE_DONE) {126 NSLog(@"插入成功");127 }128 }129 sqlite3_finalize(stmt);130 }131

132 #pragma mark -- 删除方法

133

134 - (void)deleteFromWordNoteWithWID:(NSInteger)WID135 {136 NSString *deleteString = [NSString

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值