【非凡程序员】基于MVC实现注册查询功能

      所谓的MVC也就是它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器;它们各自处理自己的任务。而在实际的项目中,为了能清晰的区分MVC 可以通过分组的形式实现;

     建一个IOS中的single View 工程 后,其中的ViewController属于controller层,而需要在Model组中建一对cocoa class来实现数据的插入和查询,比如类名为StudentInfoModel;    

    在 StudentInfoModel.h中的代码是:

 #imoport "sqlite3.h"
@interface StudentInfoModel :NSObject
{
   sqlite3 *_basePath;
   NSString *_baseLink;
}
-( void )insertInfo : (NSString *)name :(NSString *)CardId;
-( NSArray )selectInfo : (NSString *)CardId;
@end

在StudentInfoModel.m中的代码是:

 -(id)init
{
  self=[super init];
  _basePath=@"/Users/feifanchengxuyuan/Desktop/student.db";
  sqlite3_open([_basePath UTF8String],&_baseLink);
  NSString  table studentInfo(studentName varchar(30),studentId integer primary key)";
  sqllite3_exec(_baseLink,[CreateSQL UTF8String],nil,nil,nil);
}
-( void )insertInfo : (NSString *)name :(NSString *)CardId
{
   NSString *insertSQL=[NSString stringWithFormat:@"insert into studentInfo(name,studentId) values (\"%@\",\"];
   sqllite3_exec(_baseLink,[insertSQL UTF8String],nil,nil,nil);
   sqlite3_close(_baseLink);
}
-( NSArray )selectInfo : (NSString *)CardId
{
    NSArray *studentinfo;
    sqlite3_stmt stment;
    NSString *selectSQL=[NSString stringWithFormat:@"select * from studentInfo where studentId= \"",name,CardId];
    sqlite_prepare_V2(_baseLink,[selectSQL UTF8String],-1,&stment,nil);
    while(sqlite3_step(stment)==SQLITE_ROW)
    {
       NSString *name=[NSString stringWithFormat:@"%s",sqlite3_column_text(stment,0)];
       NSString *studentId=[NSString stringWithFormat:@"%s",sqlite3_column_text(stment,1)];
       studentinfo=[NSArray arrayWithObjects:name,studentId,nil];
    }
    
    return studentinfo;
}

然后在ViewController.h中的代码是:

@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *personId;
- (IBAction)selectInfo:(id)sender;
- (IBAction)register:(id)sender;

在ViewController.m中的代码是:

 - (IBAction)selectInfo:(id)sender
{
    StudentInfoModel *student=[[StudentInfoModel alloc]init];
     [student insertInfo:_name.text : _personId.text];
}
- (IBAction)register:(id)sender
{
   StudentInfoModel *student=[[StudentInfoModel alloc]init];
   [_name setText:[student selectInfo: _personId.text][0]];
}

 这样就实现了基本的信息交互。

你可能感兴趣的:(【非凡程序员】基于MVC实现注册查询功能)