iOS:面向对象的思想使用sqlite数据库
? ? *_db自己定義一個sqlite3的成員變量.進行增刪改查時要用
? 4.sqlite3_exec(_db, sql, NULL, NULL,&error); ?//不帶結(jié)果集的語句,只是對表做操作,不會返回出結(jié)果 *該函數(shù)可進行insert,delete,update操作. 5.查詢操作select.?//帶結(jié)果集的查詢語句,會返回出結(jié)果,從表中查詢到的數(shù)據(jù)都會放到stmt結(jié)構(gòu)體中 *sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);做查詢前準備,檢測SQL語句是否正確. *sqlite3_step(stmt) 提取查詢到的數(shù)據(jù),一次提取一條.//通過循環(huán)可以取出所有數(shù)據(jù) *sqlite3_column_text(stmt, 0)取出第0列的數(shù)據(jù). 6.關(guān)閉數(shù)據(jù)庫?sqlite3_close(sqlite3?*); ———————————————————————————————————————————————————— ———————————————————————————————————————————————————— 執(zhí)行帶參數(shù)的SQL語句NSString *sqlStr = @"INSERT OR REPLACE INTO note (cdate,content) VALUES (?,?)";
sqlite3_stmt *statement;
//預(yù)處理過程,產(chǎn)生結(jié)果集
if (sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statement,?
NULL) == SQLITE_OK)
?{
?NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
? NSString *nsdate = [dateFormatter stringFromDate:model.date];?
? //綁定參數(shù)開始
? sqlite3_bind_text(statement, 1, [nsdate UTF8String], -1, NULL); ?sqlite3_bind_text(statement, 2, [model.content UTF8String],?
-1, ? ?NULL);
? //執(zhí)行插入
if (sqlite3_step(statement) != SQLITE_DONE)
?{ ?
NSAssert(NO, @"插入數(shù)據(jù)失敗。"); }?
?}?
}
//清理結(jié)果集,防止內(nèi)存泄露
?sqlite3_finalize(statement);
?
單例模式:(這里主要用來保證初始化的數(shù)據(jù)庫是唯一的,只要創(chuàng)建了一次,那么它就不會被再創(chuàng)建)
+ (NoteDAO*)sharedManager?
{?
? static?dispatch_once_t?once;?
??dispatch_once(&once, ^{?
??sharedManager?= [[self?alloc]?init];?
? [sharedManager???createEditableCopyOfDatabaseIfNeeded];?}
??);
? ?return?sharedManager;
?}
?
舉例代碼如下:
Student.h
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject 4 @property (assign,nonatomic)NSInteger ID; 5 @property (copy,nonatomic)NSString *name; 6 @property (assign,nonatomic)NSInteger age; 7 @property (assign,nonatomic)char gender; 8 @property (assign,nonatomic)float math; 9 @property (assign,nonatomic)float english; 10 @property (assign,nonatomic)float chinese; 11 @end?
Student.m
1 #import "Student.h" 2 3 @implementation Student 4 -(NSString*)description 5 { 6 return [NSString stringWithFormat:@"ID:%ld,name:%@,age:%ld,gender:%c,math:%.2lf,english:%.2lf,chinese:%.2lf",_ID,_name,_age,_gender,_math,_english,_chinese]; 7 } 8 @end?
StudentDAO.h(對每一個實體類的數(shù)據(jù)庫封裝起來,就采用這種XXXDAO.h/m的命名風格)
1 #import <Foundation/Foundation.h> 2 #import <sqlite3.h> 3 @class Student; 4 @interface StudentDAO : NSObject 5 { 6 sqlite3 *_db; 7 } 8 //單例的方法 9 +(StudentDAO*)shareManager; 10 11 //初始化數(shù)據(jù)庫:創(chuàng)建表,添加數(shù)據(jù) 12 -(void)initDb; 13 14 //添加學生記錄 15 -(BOOL)addStudent:(Student *)stu; 16 17 //刪除學生記錄 18 -(BOOL)deleteStudentByName:(NSString *)name; 19 20 //修改學生記錄 21 -(BOOL)updateStudnet:(Student*)stu; 22 23 //查詢學生記錄 24 //1.查詢?nèi)?/span> 25 -(NSArray *)queryStudentAll; 26 //2.查詢符合條件的 27 -(Student *)queryStudentByName:(NSString*)name; 28 29 @end?
StudentDAO.m
1 #import "StudentDAO.h" 2 #import "Student.h" 3 4 5 static StudentDAO *instance = nil; 6 @implementation StudentDAO 7 8 //對象只創(chuàng)建一次 9 +(StudentDAO*)shareManager 10 { 11 static dispatch_once_t once; 12 dispatch_once(&once, ^{ 13 //只執(zhí)行一次 14 instance = [StudentDAO new]; 15 16 [instance initDb]; 17 }); 18 19 return instance; 20 } 21 22 //執(zhí)行不返回結(jié)果集的sql語句 23 -(BOOL)execSql:(NSString *)sql 24 { 25 char *errmsg = NULL; 26 //增刪改操作 27 sqlite3_exec(_db, [sql UTF8String], NULL, NULL, &errmsg); 28 29 if(errmsg) 30 { 31 return NO; 32 } 33 else 34 { 35 return YES; 36 } 37 } 38 //創(chuàng)建數(shù)據(jù)庫文件路徑 39 -(NSString*)pathForDB 40 { 41 NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 42 NSString *documentPath = [dirs lastObject]; 43 44 //NSLog(@"%@",documentPath); 45 46 return [documentPath stringByAppendingPathComponent:@"student.db"]; 47 } 48 49 //初始化數(shù)據(jù)庫:創(chuàng)建表,添加數(shù)據(jù) 50 -(void)initDb 51 { 52 //打開數(shù)據(jù)庫 53 //sqlite3_open(fileName.UTF8String,&_db);打開或創(chuàng)建一個數(shù)據(jù) 54 //*_db自己定義一個sqlite3的成員變量.進行增刪改查時要用 55 if(sqlite3_open([[self pathForDB] UTF8String], &_db) == SQLITE_OK) 56 { 57 //創(chuàng)建學生表 58 if([self execSql:@"create table student (id integer primary key autoincrement,name text,age integer,gender integer,math real,english real,chinese real)"]) 59 { 60 //表中添加測試數(shù)據(jù) 61 for(int i=0; i<5; i++) 62 { 63 Student *stu = [Student new]; 64 stu.name = [NSString stringWithFormat:@"stu%d",i+1]; 65 stu.age = 20+i; 66 stu.gender = 'M'; 67 stu.math = 80+i; 68 stu.english = 90+i; 69 stu.chinese = 80+i; 70 71 [self addStudent:stu]; 72 } 73 } 74 //關(guān)閉數(shù)據(jù)庫 75 sqlite3_close(_db); 76 } 77 } 78 79 //添加學生記錄 80 -(BOOL)addStudent:(Student *)stu 81 { 82 NSString *insertSql = @"insert into student (name,age,gender,math,english,chinese) values(?,?,?,?,?,?)"; 83 84 //打開數(shù)據(jù)庫 85 if(sqlite3_open([[self pathForDB]UTF8String], &_db) == SQLITE_OK) 86 { 87 //準備結(jié)果集 88 //提取數(shù)據(jù)一次一條 89 sqlite3_stmt *Pstmt = NULL; 90 //查詢前準備,看是否正確 91 if(sqlite3_prepare_v2(_db, [insertSql UTF8String], -1, &Pstmt, NULL) == SQLITE_OK) 92 { 93 //綁定參數(shù) 94 sqlite3_bind_text(Pstmt, 1, [stu.name UTF8String], -1, NULL); 95 sqlite3_bind_int(Pstmt, 2, (int)stu.age); 96 sqlite3_bind_int(Pstmt, 3, stu.gender); 97 sqlite3_bind_double(Pstmt, 4, stu.math); 98 sqlite3_bind_double(Pstmt, 5, stu.english); 99 sqlite3_bind_double(Pstmt, 6, stu.chinese); 100 101 //執(zhí)行SQL語句 102 if(sqlite3_step(Pstmt) == SQLITE_DONE) 103 { 104 return YES; 105 } 106 } 107 //清理結(jié)果集 108 sqlite3_finalize(Pstmt); 109 110 //關(guān)閉數(shù)據(jù)庫 111 sqlite3_close(_db); 112 } 113 return NO; 114 } 115 116 //刪除學生記錄 117 -(BOOL)deleteStudentByName:(NSString *)name 118 { 119 NSString *sql = @"delete from student where name = ?"; 120 121 //打開數(shù)據(jù)庫 122 if(sqlite3_open([[self pathForDB]UTF8String], &_db) == SQLITE_OK) 123 { 124 //準備結(jié)果集 125 sqlite3_stmt *Pstmt = NULL; 126 if(sqlite3_prepare_v2(_db, [sql UTF8String], -1, &Pstmt, NULL)== SQLITE_OK) 127 { 128 //綁定參數(shù) 129 sqlite3_bind_text(Pstmt, 1, [name UTF8String], -1, NULL); 130 131 132 //執(zhí)行SQL語句 133 if(sqlite3_step(Pstmt) == SQLITE_DONE) 134 { 135 return YES; 136 } 137 } 138 //清理結(jié)果集 139 sqlite3_finalize(Pstmt); 140 141 //關(guān)閉數(shù)據(jù)庫 142 sqlite3_close(_db); 143 } 144 return NO; 145 } 146 147 //修改學生記錄 148 -(BOOL)updateStudnet:(Student*)stu 149 { 150 NSString *sql = @"update student set math = ? where name = ?"; 151 152 //打開數(shù)據(jù)庫 153 if(sqlite3_open([[self pathForDB]UTF8String], &_db) == SQLITE_OK) 154 { 155 //準備結(jié)果集 156 sqlite3_stmt *Pstmt = NULL; 157 if(sqlite3_prepare_v2(_db, [sql UTF8String], -1, &Pstmt, NULL) == SQLITE_OK) 158 { 159 //綁定參數(shù) 160 sqlite3_bind_double(Pstmt,1,stu.math); 161 sqlite3_bind_text(Pstmt, 2, [stu.name UTF8String], -1, NULL); 162 163 //執(zhí)行SQL語句 164 if(sqlite3_step(Pstmt) == SQLITE_DONE) 165 { 166 return YES; 167 } 168 } 169 //清理結(jié)果集 170 sqlite3_finalize(Pstmt); 171 172 //關(guān)閉數(shù)據(jù)庫 173 sqlite3_close(_db); 174 } 175 return NO; 176 } 177 178 //查詢學生記錄 179 //1.查詢?nèi)?/span> 180 -(NSArray *)queryStudentAll 181 { 182 NSMutableArray *arrayM = [NSMutableArray array]; 183 184 //打開數(shù)據(jù)庫 185 if(sqlite3_open([[self pathForDB]UTF8String], &_db) == SQLITE_OK) 186 { 187 //準備結(jié)果集 188 sqlite3_stmt *Pstmt = NULL; 189 if(sqlite3_prepare_v2(_db, "select * from student", -1, &Pstmt, NULL) == SQLITE_OK) 190 { 191 //遍歷結(jié)果集 192 while (sqlite3_step(Pstmt) == SQLITE_ROW) 193 { 194 Student *stu = [Student new]; 195 196 stu.ID = sqlite3_column_int(Pstmt, 0); 197 stu.name = [NSString stringWithFormat:@"%s",sqlite3_column_text(Pstmt, 1)]; 198 stu.age = sqlite3_column_int(Pstmt, 2); 199 stu.gender = sqlite3_column_int(Pstmt, 3); 200 stu.math = sqlite3_column_double(Pstmt, 4); 201 stu.english = sqlite3_column_double(Pstmt, 5); 202 stu.chinese = sqlite3_column_double(Pstmt, 6); 203 204 [arrayM addObject:stu]; 205 } 206 } 207 //清理結(jié)果集 208 sqlite3_finalize(Pstmt); 209 210 //關(guān)閉數(shù)據(jù)庫 211 sqlite3_close(_db); 212 } 213 return arrayM; 214 } 215 //2.查詢符合條件的 216 -(Student *)queryStudentByName:(NSString*)name 217 { 218 Student *stu; 219 NSString *sql = @"select * from student where name = ?"; 220 221 //打開數(shù)據(jù)庫 222 if(sqlite3_open([[self pathForDB]UTF8String], &_db) == SQLITE_OK) 223 { 224 //準備結(jié)果集 225 sqlite3_stmt *Pstmt = NULL; 226 if(sqlite3_prepare_v2(_db, [sql UTF8String], -1, &Pstmt, NULL) == SQLITE_OK) 227 { 228 //綁定參數(shù) 229 sqlite3_bind_text(Pstmt, 1, [name UTF8String], -1, NULL); 230 231 //執(zhí)行SQL語句 232 if(sqlite3_step(Pstmt) == SQLITE_ROW) 233 { 234 stu = [Student new]; 235 236 stu.ID = sqlite3_column_int(Pstmt, 0); 237 stu.name = [NSString stringWithFormat:@"%s",sqlite3_column_text(Pstmt, 1)]; 238 stu.age = sqlite3_column_int(Pstmt, 2); 239 stu.gender = sqlite3_column_int(Pstmt, 3); 240 stu.math = sqlite3_column_double(Pstmt, 4); 241 stu.english = sqlite3_column_double(Pstmt, 5); 242 stu.chinese = sqlite3_column_double(Pstmt, 6); 243 } 244 } 245 246 //清理結(jié)果集 247 sqlite3_finalize(Pstmt); 248 249 //關(guān)閉數(shù)據(jù)庫 250 sqlite3_close(_db); 251 } 252 return stu;; 253 } 254 @end?
ViewController.m
1 #import "ViewController.h" 2 #import "Student.h" 3 #import "StudentDAO.h" 4 5 @interface ViewController () 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 //測試stuDAO 14 //創(chuàng)建單例對象 15 StudentDAO *stuDAO = [StudentDAO shareManager]; 16 17 18 //查詢所有的記錄 19 NSArray *students = [stuDAO queryStudentAll]; 20 NSLog(@"%@",students); 21 22 //刪除一條記錄 23 [stuDAO deleteStudentByName:@"stu3"]; 24 25 //修改記錄 26 Student *stu = [Student new]; 27 stu.name = @"stu4"; 28 stu.math = 94.5f; 29 [stuDAO updateStudnet:stu]; 30 31 32 //通過名字查詢學生 33 Student *stu2 = [stuDAO queryStudentByName:@"stu5"]; 34 NSLog(@"%@",stu2); 35 36 //查詢所有的記錄 37 students = [stuDAO queryStudentAll]; 38 NSLog(@"%@",students); 39 } 40 @end?
?
轉(zhuǎn)載于:https://www.cnblogs.com/XYQ-208910/p/4824713.html
總結(jié)
以上是生活随笔為你收集整理的iOS:面向对象的思想使用sqlite数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hibernate映射-单向n-n关联关
- 下一篇: Java9都快发布了,Java8的十大新