#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;
}