sqlite3.0调用

调用单例类datamanager
修改单例类增删改查所需要的数据
导入sqlite3库
写Model

#import "ViewController.h"
#import "TWO.h"
#import "dataManager.h"
#import "Student.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tbv;
@property(nonatomic,strong)NSMutableArray * dataSource;

@end

@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //打开数据库
    [[dataManager shareManager]open];
    
    //查询
    self.dataSource = [NSMutableArray arrayWithArray:[[dataManager shareManager]select]];
    
    //关闭数据库
    [[dataManager shareManager]close];
    [self.tbv reloadData];

}


- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setNav];
    [self tbv];
    [self.view addSubview:self.tbv];


}
-(UITableView *)tbv{
    if (!_tbv)
    {
        _tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    }
    _tbv.delegate=self;
    _tbv.dataSource=self;
    return _tbv;
}
-(void)setNav{
    
    self.title = @"sqlite深入";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add)];
    
}

-(void)Add{
    
    TWO *tw=[[TWO alloc]init];
    tw.typeID =1;//代表我是添加数据
    [self.navigationController pushViewController:tw animated:YES];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"cellID";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
 Student * std = self.dataSource[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"SID = %ld age = %@ name = %@",std.SID,std.name,std.age];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TWO *two = [TWO new];
    two.student = self.dataSource[indexPath.row];
    
    [self.navigationController pushViewController:two animated:YES];
    
    
    
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [[dataManager shareManager]open];
    Student * s =_dataSource[indexPath.row];
    NSInteger ID= s.SID;
    
    [[dataManager shareManager]deleteData:ID];
    
    [_dataSource removeObject:_dataSource[indexPath.row]];
    
    [self.tbv reloadData];
}


@end

Two.h.m.xib

#import "TWO.h"
#import "Student.h"
#import "dataManager.h"
@interface TWO ()

@end

@implementation TWO

- (void)viewDidLoad {
    [super viewDidLoad];


    [self setNav];
    //打开数据库
    [[dataManager shareManager] open];
    [[dataManager shareManager]create];





}
-(void)setNav{
    
    if (self.typeID ==1)
    {
        //添加数据
        self.title =@"添加数据";
        
        
    }else{
        //更新数据
        self.title =@"更新数据";
        
        self.sid.text=[NSString stringWithFormat:@"%ld",self.student.SID];
        self.name.text=self.student.name;
        self.age.text =self.age.text;
        
    }
    
    
   self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveAdd)];
    
    
    
}
//保存数据
-(void)saveAdd{
    
     //实例化学生
    
    Student * std =[Student new];
    std.SID=self.student.SID;
    std.SID = [_sid.text integerValue];
    std.name =_name.text;
    std.age =_age.text;
    
    
    if (self.typeID ==1)
    {
        //添加数据的保存按钮
        [[dataManager shareManager]insert:std];
        
    }else{
        //更新数据的保存
        [[dataManager shareManager]update:std];
        
        
        
    }
    
    [[dataManager shareManager]close];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

@end

#import <UIKit/UIKit.h>
#import "Student.h"
@interface TWO : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *sid;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *age;
@property(nonatomic,assign) NSInteger typeID;


@property(nonatomic,strong) Student * student;
@end

猜你喜欢

转载自blog.csdn.net/chuck_phonics/article/details/85039706