学生名单治理界面实现数据库的增删改操作

数据库操作最根本的无非就是减少,删除,改变,查问四个性能

批改数据分为下图中的几个步骤

第一步 sqlite3_open 关上数据库
第二步 sqlite3_prepare_v2 预处理 SQL 语句操作
第三步 sqlite3_bind_text 函数绑定参数
第四步 sqlite3_step 函数执行 SQL 语句
第五步 sqlite3_finalize 和 sqlite3_close 开释资源

咱们新建一个SQLManager的治理类,把所有根本数据库操作的办法进行封装解决。下图是Demo工程的全副文件。

以下是代码局部:

SQLManager.h文件

#import <Foundation/Foundation.h>#import "sqlite3.h"#import "StudentModel.h"@interface SQLManager : NSObject{    sqlite3 * db;}+(SQLManager *)shareManager;//查问- (StudentModel *)searchWithIDNum:(StudentModel *)model;//插入-(int)insert:(StudentModel *)model;//删除-(void)remove:(StudentModel *)model;//批改-(void)modify:(StudentModel *)model;@end

SQLManager.m文件

#import "SQLManager.h"@implementation SQLManager//定义宏的目标在于:咱们操作数据库就相当于对本地文件的一个解决,首先要获取文件门路,去拼接它的名字,所以为了不便当前咱们调用这个文件,先把文件名取好#define kNameFile (@"Student.sqlite")//创立单例static SQLManager * manager = nil;+(SQLManager *)shareManager{    static dispatch_once_t once;    dispatch_once(&once,^{        manager = [[self alloc] init];        [manager createDataBaseTableIfNeeded];    });    return manager;}//获取数据库的残缺门路-(NSString *)applicationDocumentsDirectoryFile{    NSArray * paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString * documentDirectory = [paths firstObject];    NSString * filePath = [documentDirectory stringByAppendingPathComponent:kNameFile];    return filePath;}//再定义一个函数,接下来的操作是创立数据库,在进行数据库操作之前,确保数据库存在,不存在的时候必须要进行创立- (void)createDataBaseTableIfNeeded{    //尽管说咱们的数据库是SQLite内置了,然而仍属于第三方,开发时如果想要应用它的类库,须要再进行配置增加    //NameList配置文件-> TARGETS -> General -> LinkedFrameworks and Libraries,点击+号,搜书sqlit,抉择libsqlite3.tbd。而后能力持续后续的操作。    NSString * writetablePath =[self applicationDocumentsDirectoryFile];    NSLog(@"数据库的地址是:%@",writetablePath);    //关上数据库    //第一个参数数据库文件所在的残缺门路    //第二个参数是数据库 DataBase 对象    if (sqlite3_open([writetablePath UTF8String], &db) != SQLITE_OK) {        //SQLITE_OK 是苹果为咱们定义的一个常量如果是OK的话,就代表咱们的数据库关上是胜利了        //失败        //数据库敞开        sqlite3_close(db);        NSAssert(NO, @"数据库关上失败!");//抛出错误信息    }else{        //胜利        char * err;        NSString * createSQL =[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS StudentName (idNum TEXT PRIMARYKEY, name TEXT);"];        //SQLite的执行函数        /*         第一个参数 db对象         第二个参数 语句         第三个和第四个参数 回调函数和回调函数参数         第五个参数 是一个错误信息         */        if (sqlite3_exec(db, [createSQL UTF8String], NULL, NULL, &err) !=SQLITE_OK) {            //失败            //数据库敞开            sqlite3_close(db);            NSAssert1(NO, @"建表失败!%s", err);//抛出错误信息        }        sqlite3_close(db);    }}//查问数据/* 1.应用sqlite3_prepare_v2函数预处理SQL语句 2.应用sqlite3_bind_text函数绑定参数 3.应用sqlite3_step函数执行SQL语句,遍历后果集 4.应用sqlite3_column_text等函数提取字段数据 */- (StudentModel *)searchWithIDNum:(StudentModel *)model{    NSString * path =[self applicationDocumentsDirectoryFile];    if (sqlite3_open([path UTF8String], &db) != SQLITE_OK) {        sqlite3_close(db);        NSAssert(NO, @"关上数据库失败!");    }else{        NSString * qsql =@"SELECT idNum,name FROM StudentName where idNum = ?";        sqlite3_stmt * statement;//语句对象        //第一个参数:数据库对象        //第二个参数:SQL语句        //第三个参数:执行语句的长度 -1是指全副长度        //第四个参数:语句对象        //第五个参数:没有执行的语句局部 NULL        //预处理        if(sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statement, NULL) ==SQLITE_OK){            //进行 按主键查询数据库            NSString * idNum = model.idNum;            //第一个参数 语句对象            //第二个参数 参数开始执行的序号            //第三个参数 咱们要绑定的值            //第四个参数 绑定的字符串的长度            //第五个参数 指针 NULL            //绑定            sqlite3_bind_text(statement, 1, [idNum UTF8String], -1, NULL);            //遍历后果集            /*             有一个返回值 SQLITE_ROW常量代表查出来了             */            if (sqlite3_step(statement) == SQLITE_ROW) {                //提取数据                /*                 第一个:语句对象                 第二个:字段的索引                 */                char * idNum = (char *)sqlite3_column_text(statement, 0);                //数据转化                NSString * idNumStr =[[NSString alloc]initWithUTF8String:idNum];                char * name =(char *)sqlite3_column_text(statement, 1);                NSString * nameStr =[[NSString alloc]initWithUTF8String:name];                StudentModel * model =[[StudentModel alloc] init];                model.idNum = idNumStr;                model.name = nameStr;                //开释                sqlite3_finalize(statement);                sqlite3_close(db);                return model;            }        }        sqlite3_finalize(statement);        sqlite3_close(db);    }    return nil;}//批改-(int)insert:(StudentModel *)model{    NSString * path =[self applicationDocumentsDirectoryFile];    if (sqlite3_open([path UTF8String], &db) != SQLITE_OK) {        sqlite3_close(db);        NSAssert(NO, @"数据库关上失败!");    }else{        NSString * sql =@"INSERT OR REPLACE INTO StudentName (idNum, name) VALUES (?,?)";        sqlite3_stmt * statement;        //预处理过程        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, NULL) ==SQLITE_OK) {            sqlite3_bind_text(statement, 1, [model.idNum UTF8String], -1, NULL);            sqlite3_bind_text(statement, 2, [model.name UTF8String], -1, NULL);            if (sqlite3_step(statement) != SQLITE_DONE) {                NSAssert(NO, @"插入数据失败!");            }            sqlite3_finalize(statement);            sqlite3_close(db);        }    }    return 0;}//删除-(void)remove:(StudentModel *)model{    /*     第一步 sqlite3_open 关上数据库     第二步 sqlite3_prepare_v2 预处理 SQL 语句操作     第三步 sqlite3_bind_text 函数绑定参数     第四步 sqlite3_step 函数执行 SQL 语句     第五步 sqlite3_finalize 和 sqlite3_close 开释资源     */     NSString * path =[self applicationDocumentsDirectoryFile];    if (sqlite3_open([path UTF8String], &db) != SQLITE_OK) {        sqlite3_close(db);        NSAssert(NO, @"数据库关上失败");    }else{        NSString * sql =@"DELETE FROM StudentName where idNum = ?";        sqlite3_stmt * statement;        //预处理        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, NULL) ==SQLITE_OK) {            sqlite3_bind_text(statement, 1, [model.idNum UTF8String], -1, NULL);            sqlite3_bind_text(statement, 2, [model.name UTF8String], -1, NULL);            if (sqlite3_step(statement) != SQLITE_DONE) {                NSAssert(NO, @"删除数据失败!");            }            sqlite3_finalize(statement);            sqlite3_close(db);        }    }}

StudentModel.h文件

#import <Foundation/Foundation.h>@interface StudentModel : NSObject@property(nonatomic,strong)NSString * idNum;//学号@property(nonatomic,strong)NSString * name; //学生姓名@end

HomeViewController.m文件

#import "HomeViewController.h"#import "StudentModel.h"#import "SQLManager.h"@interface HomeViewController ()@property (nonatomic,strong) NSMutableArray * studentArray; //数据源 —— 模型@end#define HomeCellIdentifier (@"StudentCell")@implementation HomeViewController- (void)viewDidLoad {    [super viewDidLoad];    self.studentArray =[[NSMutableArray alloc]init];    NSLog(@"%@",self.studentArray);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    if (self.studentArray.count >0) {         return self.studentArray.count;    }else{        return 1;    }}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HomeCellIdentifier forIndexPath:indexPath];    //期待给cell赋值    if(self.studentArray.count > 0){        StudentModel * model =[self.studentArray objectAtIndex:indexPath.row];        cell.textLabel.text = model.name;        cell.detailTextLabel.text = model.idNum;    }    return cell;}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    //反对编辑    return YES;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 50;}-(IBAction)addUserDone:(UIStoryboardSegue *)sender{    StudentModel * model =[[StudentModel alloc]init];    model.idNum = @"100";    StudentModel * result = [[SQLManager shareManager] searchWithIDNum:model];    NSLog(@"%@",result);    [self.studentArray addObject:result];    [self.tableView reloadData];}

AddViewController.h文件

#import <UIKit/UIKit.h>@interface AddViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *idNumTextField;@property (weak, nonatomic) IBOutlet UITextField *nameTextField;@end

AddViewController.m文件

#import "AddViewController.h"#import "SQLManager.h"#import "StudentModel.h"@interface AddViewController ()@end@implementation AddViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if ([segue.identifier isEqualToString:@"AddUser"]) {        //写入数据库        StudentModel * model =[[StudentModel alloc]init];        model.idNum = self.idNumTextField.text;        model.name = self.nameTextField.text;        [[SQLManager shareManager] insert:model];    }}@end
最初举荐个我的iOS交换群:642363427 有一个独特的圈子很重要,结识人脉!外面都是iOS开发,全栈倒退,欢送入驻,共同进步!(群内会收费提供一些群主珍藏的收费学习书籍材料以及整顿好的几百道面试题和答案文档!)