Student.h @interface Student : NSObject @property(nonatomic,assign)NSInteger number; @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *gender; @property(nonatomic,assign)NSInteger age; @end Student.m @implementation Student - (void)dealloc{ self.name = nil; self.gender = nil; [super dealloc]; } - (NSString *)description { return [NSString stringWithFormat:@%ld %@ %@ %ld, self.number,self.name,self.gender,self.age]; } @end通過單例寫接口並實現方法:
DataBaseHandle.h @class Student; @interface DataBaseHandle : NSObject //創建單例的方法 + (DataBaseHandle *)shareDataBaseHandle; //打開數據庫的方法 - (void)openDataBase; //關閉數據庫的方法 - (void)closeDataBase; //插入學生對象的接口 - (void)insertStudent : (Student *)student; //返回表格中所有學生的接口 - (NSMutableArray *)selectAllStudent; //根據唯一標識學號刪除學生 - (void)deleteOneStudentByNumber : (NSInteger )number; //根據唯一標識學號修改學生的姓名 - (void)updateStudentGender : (NSString *)gender ByNumber : (NSInteger )number; //根據學號查找學生 - (Student *)selectOneStudentByNumber : (NSInteger)number; @end
#import DataBaseHandle.h #import#import Student.h @implementation DataBaseHandle static DataBaseHandle *handle = nil; //創建單例的方法 + (DataBaseHandle *)shareDataBaseHandle{ @synchronized(self){ if (handle == nil) { handle = [[DataBaseHandle alloc]init]; //讓單例對象一創建後就可以訪問數據庫 [handle openDataBase]; } } return handle; } //返回數據庫路徑 - (NSString *)dataBasePath{ //將數據庫文件放到Documents文件夾下 student.sqlite return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@student.sqlite]; }
定義一個全局且在靜態區的數據庫指針
static sqlite3 *db = nil;
打開數據庫的方法
- (void)openDataBase{ //1.先獲取數據文件的路徑 NSString *dbPath = [self dataBasePath]; //使用sql之前一定要導入libsqlite3.0動態鏈接類庫,其中libsqlite.3.0是快捷方式,導入快捷方式的好處,當版本更新的時候,不再導入新的實體類庫了,因為快捷方式永遠指向的是最新的實體類庫(記得導入頭文件) //2.使用sql語句打開數據庫
//[dbPath UTF8String] 將OC字符串轉化為C語言字符串 //sqlite3 就是數據庫指針 //創建數據庫指針db // sqlite3 *db = nil; //方法執行完,內不會對數據庫指針db進行初始化,方法執行完,就會存在數據庫文件 //此方法會先檢測文件路徑中有沒有對應的數據庫文件,沒有則創建,有的話直接打開 int result = sqlite3_open([dbPath UTF8String], &db); //SQLITE_OK 說明sql是成功 if (result == SQLITE_OK) { // NSLog(@數據庫打開成功); //創建表格 //准備sql語句 NSString *sqlString = @create table if not exists Student(stu_number integer primary key autoincrement,stu_name text,stu_gender text,stu_age integer); //執行sql語句 sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, NULL); }else{ NSLog(@數據庫打開失敗); } }
關閉數據庫的方法
- (void)closeDataBase{ int result = sqlite3_close(db); NSLog(@%@,(result == SQLITE_OK) ? @關閉成功:@關閉失敗); }
插入學生對象的接口——增
- (void)insertStudent : (Student *)student{ //1.打開數據庫 [self openDataBase]; //2.准備插入的sql語句 NSString *sqlString = @insert into Student(stu_name,stu_gender,stu_age)values(?,?,?); //3.創建數據庫管理指針(數據庫管理指令集) sqlite3_stmt *stmt = nil; //4.驗證sql語句是否正確 //參數1:數據庫指針, //參數2:sql語句 //參數3:sql語句的長度寫成-1,自動計算sql語句的最大長度,否則要自己計算長度 //參數4:sql語句的管理指針 //參數5:預留參數,未來使用 //5.拿驗證的結果,判斷是否執行參數綁定的操作 int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL); if (result == SQLITE_OK) { NSLog(@插入成功); //參數1:sql語句管理指針 //參數2:上面sql語句中 ?的位置,?的下標從1開始 //參數3:要綁定的數據 //參數4:數據的長度 //綁定字段stu_name 的數據 sqlite3_bind_text(stmt, 1, [student.name UTF8String], -1, NULL); //綁定字段stu_gender 的數據 sqlite3_bind_text(stmt, 2, [student.gender UTF8String], -1, NULL); //綁定字段stu_age 的數據 sqlite3_bind_int(stmt, 3, (int)student.age); //6.讓sql語句執行 sqlite3_step(stmt); } //7.釋放掉管理指針 sqlite3_finalize(stmt); //8.關閉數據庫 [self closeDataBase]; }
返回表格中所有學生的接口——查1
- (NSMutableArray *)selectAllStudent{ //1.打開數據庫 [self openDataBase]; //2.准備sql語句 NSString *sqlString = @select * from Student; //3.創建管家指針 sqlite3_stmt *stmt = nil; //4.驗證sql語句是否正確 int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL); if (SQLITE_OK == result) { NSLog(@查找全部語句成功); //創建可變數組存儲找到所有的學生對象 NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; //SQLITE_ROW 如果等於row說明下一行是有數據的,循環繼續,如果不等於SQLITE_ROW ,說明下一行沒有數據了,循環結束 while (sqlite3_step(stmt) == SQLITE_ROW) { //依次讀出字段的數據 //列的編號是從零開始的 //第一列 int number = sqlite3_column_int(stmt, 0); //第二列 NSString *name = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1) ]; //第三列 NSString *gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)]; //第四列 int age = sqlite3_column_int(stmt, 3); //5.創建學生對象並賦值 Student *stu = [[Student alloc]init]; stu.number = number; stu.name = name; stu.gender = gender; stu.age = age; //添加到數組 [array addObject:stu]; //釋放 [stu release]; } //6.釋放管理指針 sqlite3_finalize(stmt); //關閉數據庫 [self closeDataBase]; return array; }else{ sqlite3_finalize(stmt); [self closeDataBase]; return nil; } }
根據學號查找學生——查2
- (Student *)selectOneStudentByNumber : (NSInteger)number{ //1.打開數據庫 [self openDataBase]; //2.准備sql語句 NSString *sqlString = @select * from Student where stu_number = ?; //3.創建管理指針 sqlite3_stmt *stmt = nil; //4.驗證sql語句是否正確 int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL); //5.根據驗證結果決定執行什麼操作 if (result == SQLITE_OK) { //6.綁定參數 sqlite3_bind_int(stmt, 1, (int)number); //7.遍歷表格中的數據 //創建model學生對象存儲找到的學生信息 Student *stu = [[Student alloc]init]; while (sqlite3_step(stmt) == SQLITE_ROW) { //根據找到的數據給學生的屬性賦值 stu.number = number; stu.name =[NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)]; stu.gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)]; stu.age = sqlite3_column_int(stmt, 3); } //8.釋放管理指針 sqlite3_finalize(stmt); //9.關閉數據庫 [self closeDataBase]; return [stu autorelease]; }else{ //10.釋放管理指針 sqlite3_finalize(stmt); //11.關閉數據庫 [self closeDataBase]; } return nil; }
根據唯一標識學號刪除學生——刪
- (void)deleteOneStudentByNumber : (NSInteger )number{ //1.打開數據庫 [self openDataBase]; //2.准備刪除sql語句 NSString *sqlString = @delete from Student where stu_number = ?; //3.創建數據庫管理指針 sqlite3_stmt *stmt = nil; //4.驗證sql語句是否正確 int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL); //5.判斷是否執行參數綁定的操作 if (result == SQLITE_OK) { //6.綁定傳過來的參數 sqlite3_bind_int(stmt, 1, (int)number); //7.執行sql語句 sqlite3_step(stmt); } //8.釋放掉管理指針 sqlite3_finalize(stmt); //9.關閉數據庫 [self closeDataBase]; }
根據唯一標識學號修改學生的姓名——改
- (void)updateStudentGender : (NSString *)gender ByNumber : (NSInteger )number{ //1.打開數據庫 [self openDataBase]; //2.准備sql語句 NSString *sqlString = @update Student set stu_gender = ? where stu_number = ?; //3.創建數據庫管理指針 sqlite3_stmt *stmt = nil; //4.驗證sql語句 int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL); //5.根據驗證的結果決定執行的操作 if (result == SQLITE_OK) { //6.綁定參數 sqlite3_bind_text(stmt, 1, [gender UTF8String], -1, NULL); //7.綁定學號 //第二個參數:是 ? 在sql語句中位置,位置從1開始 sqlite3_bind_int(stmt, 2, (int)number); //8.執行sql語句 sqlite3_step(stmt); } //9.釋放管理指針 sqlite3_finalize(stmt); //10.關閉數據庫 [self closeDataBase]; }
方法的調用:
ViewController.m
#import ViewController.h #import DataBaseHandle.h #import Student.h @interface ViewController () @property (retain, nonatomic) IBOutlet UITextField *numberField;//學號 @property (retain, nonatomic) IBOutlet UITextField *nameField;//姓名 @property (retain, nonatomic) IBOutlet UITextField *genderField;//性別 @property (retain, nonatomic) IBOutlet UITextField *ageField;//年齡 @end1、添加:增
- (IBAction)insertStudent:(UIButton *)sender { //判斷有一個控件中輸入的內容為空,就不讓它插入數據 if (0 == self.nameField.text.length || 0 == self.genderField.text.length || 0 == self.ageField.text.length) { return; } //創建學生Student 對象,存儲空間中輸入的內容 Student *stu = [[Student alloc]init]; stu.name = self.nameField.text; stu.gender = self.genderField.text; stu.age = [self.ageField.text integerValue]; [[DataBaseHandle shareDataBaseHandle]insertStudent:stu]; [stu release]; }
- (IBAction)updateStudent:(UIButton *)sender { //獲取輸入框的內容 NSInteger number = [self.numberField.text integerValue]; NSString *gender = self.genderField.text; //調用根據學號修改gender的方法 [[DataBaseHandle shareDataBaseHandle]updateStudentGender:gender ByNumber:number]; }
3、刪除學生:刪
- (IBAction)deleteStudent:(UIButton *)sender { NSInteger number = [self.numberField.text integerValue]; //調用根據學號刪除學生的方法 [[DataBaseHandle shareDataBaseHandle]deleteOneStudentByNumber:number]; }
4、查找全部學生:查1
- (IBAction)selectAllStudent:(UIButton *)sender { //調用尋找所有學生 NSMutableArray *contentArray = [[DataBaseHandle shareDataBaseHandle]selectAllStudent]; for (Student *stu in contentArray) { NSLog(@%@,stu); } }
5、查找某個學生:查2
- (IBAction)selectOneStudent:(id)sender { //獲取學號輸入框的內容 NSInteger number = [self.numberField.text integerValue]; //調用根據學號獲取學生對象 Student *stu = [[DataBaseHandle shareDataBaseHandle]selectOneStudentByNumber:number]; NSLog(@%@,stu); }
- (void)dealloc { [_numberField release]; [_nameField release]; [_genderField release]; [_ageField release]; [super dealloc]; }