Database articles - students basic information into the system

Students basic information into the system

A. The main interface structures

  • The main interface built in storyboard
    Here Insert Picture Description
  • The main interface to add navigation controller is used to push interface
    Here Insert Picture Description
  • The associated object
    Here Insert Picture Description
  • Related events
    Here Insert Picture Description

II. On the package file operation class (SqliteOperation)

Here Insert Picture Description

  • Singleton, an object of this class so that the current management object has always existed
+ (SqliteOperation *)sharedOperaion;
static SqliteOperation *instance = nil;

@implementation SqliteOperation

+ (SqliteOperation *)sharedOperaion{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [SqliteOperation new];
    });
    return instance;
}

@end
  • Open a database, if you can open the table is created
- (BOOL)openSql;
- (BOOL)openSql{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"stu.sql"];
    NSLog(@"%@",path);
    if (sqlite3_open([path UTF8String], &_sql) == SQLITE_OK) {
        NSLog(@"打开数据库成功");
        //创建表
        NSString *createTableSql = @"create table if not exists stu(id integer primary key autoincrement, name text, age text, icon blob)";
        return [self executeSql:createTableSql];
    }else{
        return NO;
    }
}

//创建表的执行语句
- (BOOL)executeSql:(NSString *)sql{
    if(sqlite3_exec(_sql, sql.UTF8String, NULL, NULL, NULL) == SQLITE_OK){
        NSLog(@"执行语句成功:%@",sql);
        return YES;
    }else{
        NSLog(@"执行语句失败:%@",sql);
        return NO;
    }
}
  • We should save data, all the information the students are first encapsulated into a model
    Here Insert Picture Description
@interface StudentModel : NSObject

/** 图片 */
@property(nonatomic, strong) UIImage *icon;
/** 姓名 */
@property(nonatomic, strong) NSString *name;
/** 年龄 */
@property(nonatomic, assign) NSString *age;

@end
  • Write a function to save the student model, the model information stored in the database
- (void)save:(StudentModel *)model{
    NSString *insertSql = @"insert into stu(name,age,icon) values(?,?,?)";
    
    sqlite3_stmt *stmt = NULL;
    if(sqlite3_prepare(_sql, insertSql.UTF8String, -1, &stmt, NULL) != SQLITE_OK){
        NSLog(@"预处理失败");
        return;
    }
    
    NSData *data = UIImagePNGRepresentation(model.icon);
    
    sqlite3_bind_text(stmt, 1, model.name.UTF8String, -1, NULL);
    sqlite3_bind_text(stmt, 2, model.age.UTF8String, -1, NULL);
    sqlite3_bind_blob(stmt, 3, data.bytes, (int)data.length, NULL);
    
    sqlite3_step(stmt);
    
    sqlite3_finalize(stmt);
}
  • Write a loading function is used to read the student information database to display to the cell, the function returns an array of model
@property (nonatomic, strong) NSMutableArray *dataArray;
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSArray *)loadData{
    NSString *selectSql = [NSString stringWithFormat:@"select * from stu limit %ld,2", self.dataArray.count];
    
    sqlite3_stmt *stmt = NULL;
    if(sqlite3_prepare(_sql, selectSql.UTF8String, -1, &stmt, NULL) != SQLITE_OK){
        return nil;
    }
    
    //id name age icon
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        StudentModel *model = [StudentModel new];
        model.name = [NSString stringWithCString:(const char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
        model.age = [NSString stringWithCString:(const char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];
        NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(stmt, 3) length:sqlite3_column_bytes(stmt, 3)];
        model.icon = [UIImage imageWithData:imageData];
        
        [self.dataArray addObject:model];
    }
    
    return self.dataArray;
}

III. Information display categories and custom cell

1.ViewController class

  • Setting information input box prompt text
_nameTextField.placeholder = @"Name";
_ageTextFeild.placeholder = @"Age";
  • Click the + button to complete the reading of a photograph from the system function
    Here Insert Picture Description
  • Obey UIImagePickerController agent
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
  • Two proxy method to achieve
    Here Insert Picture Description
  • Click the submit button, the data is encapsulated into a model and then model the data saved to the database
    Here Insert Picture Description
  • Click the show button to enter the display interface, display student information database
    Here Insert Picture Description

2.ShowViewController class (inherited from UIViewController)

  • Define a variable attribute tableview
@property(nonatomic, strong) UITableView *tableView;

Here Insert Picture Description

  • Obey agency agreement
@interface ShowViewController ()<UITableViewDataSource,UITableViewDelegate>
  • Receiving model data read from the database using a data
@property(nonatomic, strong) NSArray *dataArray;
  • Pull down to refresh, to read data from the database model
    Here Insert Picture Description
  • Implementing a proxy method
    Here Insert Picture Description

3. Customize the cell, ShowTableViewCell class (inherited from UITableViewCell)

  • Custom follows xib
    Here Insert Picture Description
  • Association class
    Here Insert Picture Description
  • Set cellID
    Here Insert Picture Description
  • The associated object
    Here Insert Picture Description
  • .h file, add a member variable attribute model
    Here Insert Picture Description
  • Set the value of the model
    Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43733988/article/details/90345258