#import <sqlite3.h>
@implementation DataBaseManager
//单例方法
+(DataBaseManager *)defaultManager{
static DataBaseManager *manager = nil;
@synchronized(self) {
if (manager == nil) {
manager = [[DataBaseManager alloc]init];
//创建表
[manager createTable];
}
}
return manager;
}
//数据库操作对象
static sqlite3 *db = NULL;
//创建表
- (void)createTable{
//1.打开数据库
[self openDataBase];
//2.操作 - 建表
//sql语句
//autoincrement not null 主键自动增加,且不为空.
NSString *sqlStr = @"create table if not exists Note (note_id integer primary key
autoincrement not null
, note_title text, note_content text)";
//执行
int result = sqlite3_exec(db, sqlStr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
//3.关闭数据库
[self closeDataBase];
}
//数据库文件路径
- (NSString *)filePath{
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES).firstObject stringByAppendingPathComponent:@"note.sqlite"];
NSLog(@"%@",file);
return file;
}
//打开数据库
- (void)openDataBase{
int result = sqlite3_open([self filePath].UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"打开成功");
}else{
NSLog(@"打开失败");
}
}
//关闭数据库
- (void)closeDataBase{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"关闭成功");
}else{
NSLog(@"关闭失败");
}
}
//增加
-(void)insertIntoNoteWith:(Note *)note{
//1.打开数据库
[self openDataBase];
//2.执行 (为了不使用值绑定的麻烦方式,准备sql语句的时候,使用字符串格式化方法来使用占位符 使用%@需加上单引号,%ld时不需要加单引号)
//sql语句
NSString *sqlStr =[NSString stringWithFormat:@"insert into Note(note_title,note_content) values('%@','%@')",note.title,note.content];
int result = sqlite3_exec(db, sqlStr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败");
}
//3.关闭数据库
[self closeDataBase];
}
//删除 (根据主键id删除)
- (void)deleteFromNoteWithNoteID:(NSInteger)note_id{
//1.打开数据库
[self openDataBase];
//2.操作
NSString *sqlStr =[NSString stringWithFormat:@"delete from Note where note_id = %ld",note_id] ;
int result = sqlite3_exec(db, sqlStr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}
//3.关闭数据库
[self closeDataBase];
}
//更新(根据主键id更新内容)
- (void)updateWithNote:(Note *)note WithNoteID:(NSInteger)note_id{
//1.打开数据库
[self openDataBase];
//2.执行
NSString *sqlStr = [NSString stringWithFormat:@"update Note set note_title ='%@', note_content = '%@' where note_id = %ld",note.title,note.content,note.note_id];
char *error =nil ;
int result = sqlite3_exec(db, sqlStr.UTF8String, NULL, NULL, &error);
if (result == SQLITE_OK) {
NSLog(@"更新成功");
}else{
NSLog(@"%s",error);
}
//3.关闭数据库
[self closeDataBase];
}
//查询
- (NSMutableArray *)selecteFromNote{
NSMutableArray *array = [NSMutableArray array];
//1.打开数据库
[self openDataBase];
//2.执行 排序 order by 升序 asc 降序 desc
NSString *sqlStr = @"select * from Note order by note_id asc";
sqlite3_stmt *stmt = NULL;
int result = sqlite3_prepare_v2(db, sqlStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
//读取每个字段的信息
NSInteger note_id = sqlite3_column_int(stmt, 0);
//title
const unsigned char *title = sqlite3_column_text(stmt, 1);
NSString *note_title = [NSString stringWithUTF8String:(const char *)title];
//content
const unsigned char *content = sqlite3_column_text(stmt, 2);
NSString *note_content = [NSString stringWithUTF8String:(const char *)content];
Note *note = [[Note alloc]init];
note.note_id = note_id;
note.title = note_title;
note.content = note_content;
[array addObject:note];
}
}
//关闭指令集,释放资源
sqlite3_finalize(stmt);
//3.关闭数据库
[self closeDataBase];
return array;
}