sqlite3 实际运用

User.h文件
#import <Foundation/Foundation.h>
#import "sqlite3.h"
@interface User : NSObject
{
    sqlite3 * linik;
    NSString * path;

}
//添加四个数据
-(NSInteger )addNewGoods:(NSString*)addGoodname withPrice:(NSString *)addwithPrice andStorage : (NSString * )addGoodsStorage;
-(NSMutableArray *) showNewGoodsInformation;
User.m文件
#import "User.h"

@implementation User

-(id)init{
    
    self =[super init];
    //根目录
    path =@"/Users/feifanchengxuyuan/Desktop/MicroMall.db";
    
    //打开数据库
    sqlite3_open([path UTF8String], &linik);
    
    //创建表
    NSString * createGoodsTable=@"create table if not exists goods(goods_id integer primary key autoincrement,goods_name varchar(40),goods_Price varchar(7),goods_storage varchar(8) )";
    
    
    //执行
    sqlite3_exec(linik, [createGoodsTable UTF8String], nil, nil, nil);
    
    
    return self;
    
}

//插数据
 -(NSInteger )addNewGoods:(NSString*)addGoodname withPrice:(NSString *)addwithPrice andStorage : (NSString * )addGoodsStorage{
    
     //辅助
    sqlite3_stmt * state;
     
    NSString * goodsId;
    
     //创建表
    NSString * insertGoods=[NSString stringWithFormat:@"insert into goods(goods_name,goods_Price,goods_storage) values(\"%@\",\"%@\",\"%@\")",addGoodname,addwithPrice,addGoodsStorage];
    
     //如果每一个表都插入成功  SQLITE_OK代表都插进了
    if (sqlite3_exec(linik, [insertGoods UTF8String], nil, nil, nil)==SQLITE_OK) {
        //查找新添加商品Id
        NSString * selectNewGoods=[NSString stringWithFormat:@"select goods_id from goods where goods_name=\"%@\" ",addGoodname];
        
        //预处理
        sqlite3_prepare_v2(linik, [selectNewGoods UTF8String], -1, &state, nil);
       // SQLITE_ROW代表每一行插进去了
        while (sqlite3_step(state)==SQLITE_ROW) {
            goodsId =[NSString stringWithUTF8String:( char *)sqlite3_column_text(state, 0)];
        }
        
    }
    
    NSLog(@"jdsfhk");
    return [goodsId integerValue];
    
}

//从数据库中提取字段

-(NSMutableArray *)showNewGoodsInformation{
    //辅助的
    sqlite3_stmt *state;
    
    
    //接收从数据库中拿出来的值
    NSString * goodsId;
    NSString * goodsName;
    NSString * goodsPrice;
    NSString * goodsStorge;
    
    
    //将值用可变数组接收
    NSMutableArray * muarray=[[NSMutableArray alloc]initWithCapacity:10];
    
    NSDictionary * dic;
    
    
    //查看商品表
    NSString * selectGoods=[[NSString alloc]initWithFormat:@"select * from goods order by goods_id desc;"];//order by goods_id desc 降序
    
    //预编译
    sqlite3_prepare_v2(linik, [selectGoods UTF8String], -1, &state, nil);
    
    //取出下一行0,1,2,3列字段所有的值
    while (sqlite3_step(state) == SQLITE_ROW) {
        //取出地一列的所有值
        goodsId =[[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 0) encoding:NSUTF8StringEncoding];
        
        //取出第二列的所有值
        goodsName = [[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 1) encoding:NSUTF8StringEncoding];
        //取出第三列的所有值
        
        goodsPrice =[[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 2) encoding:NSUTF8StringEncoding];
        //取出第四列的所有值
        
        goodsStorge =[[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 3) encoding:NSUTF8StringEncoding];
        
        
        //先用字典包装
        dic=@{     @"goodsId":goodsId,
                   @"goodsName":goodsName,
                   @"goodsPrice":goodsPrice,
                   @"goodsStorge":goodsStorge,
            };
        
        //再用数组包装
        [muarray addObject:dic];
        
    }
    
   return muarray;
    
}

@end
ViewController.h文件
#import <UIKit/UIKit.h>
#import "User.h"
#import "ChakanViewController.h"

@interface ViewController : UIViewController

@property(nonatomic,strong)UITextField * shangPinMingShuRuKuang;
@property(nonatomic,strong)UITextField *jiaGeShuRuKuang;
@property(nonatomic,strong)UITextField * kuCunShuRuKuang;
@property(nonatomic,strong)ChakanViewController * chakanjiemian;
@end
ViewController.m文件
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //商品名标签
    UILabel * mingzilabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 100, 80, 20)];
   mingzilabel.textColor=[UIColor redColor];
    mingzilabel.text=@"商品名:";
    [self.view addSubview:mingzilabel];
    
    //商品名输入框
 _shangPinMingShuRuKuang=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 25)];
    //设置边框宽度
    _shangPinMingShuRuKuang.layer.borderWidth=2;
    //设置边框颜色
    _shangPinMingShuRuKuang.layer.borderColor=[UIColor redColor].CGColor;
    [self.view addSubview:_shangPinMingShuRuKuang];
    
    
    //商品价格标签
    UILabel * jiagelabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 140,80 , 20)];
   jiagelabel.textColor=[UIColor redColor];
    jiagelabel.text=@"价格:";
   [self.view addSubview:jiagelabel];
    
    
    //商品价格输入框
    _jiaGeShuRuKuang=[[UITextField alloc]initWithFrame:CGRectMake(100, 140, 200, 25)];
    _jiaGeShuRuKuang.layer.borderColor=[UIColor redColor].CGColor;
    _jiaGeShuRuKuang.layer.borderWidth=2;
    [self.view addSubview:_jiaGeShuRuKuang];
    
    
    //库存标签
    UILabel * kuCunlabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 180,80 , 20)];
    kuCunlabel.textColor=[UIColor redColor];
    kuCunlabel.text=@"库存:";
    [self.view addSubview:kuCunlabel];
    
    
    
    //库存输入框
   _kuCunShuRuKuang=[[UITextField alloc]initWithFrame:CGRectMake(100, 180, 200,25 )];
    _kuCunShuRuKuang.layer.borderColor=[UIColor redColor].CGColor;
    _kuCunShuRuKuang.layer.borderWidth=2;
   [self.view addSubview:_kuCunShuRuKuang];
    
    
    //添加按钮
    UIButton * tianJiabutton=[[UIButton alloc]initWithFrame:CGRectMake(80, 260, 50, 20)];
     tianJiabutton.backgroundColor=[UIColor redColor];
    [tianJiabutton setTitle:@"添加" forState:UIControlStateNormal];
     [self.view addSubview:tianJiabutton];
    
    [tianJiabutton addTarget:self action:@selector(tianjia) forControlEvents:UIControlEventTouchUpInside];
    
    //查看按钮
    UIButton * chaKanbutton=[[UIButton alloc]initWithFrame:CGRectMake(230, 260, 50, 20)];
    chaKanbutton.backgroundColor=[UIColor redColor];
    [chaKanbutton setTitle:@"查看" forState:UIControlStateNormal];
    [self.view addSubview:chaKanbutton];
    
    [chaKanbutton addTarget:self action:@selector(chakan) forControlEvents:UIControlEventTouchUpInside];
    
   NSLog(@"添加"); 
}
-(void)tianjia{
    
    User *user=[[User alloc]init];
    
    //向数据库插值
    [user addNewGoods:_shangPinMingShuRuKuang.text withPrice:_jiaGeShuRuKuang.text andStorage:_kuCunShuRuKuang.text];
 
}

-(void)chakan{
    
    ChakanViewController * II=[[ChakanViewController alloc]init];
    [self presentViewController:II animated:YES completion:nil];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
ChakanViewController.h文件
#import <UIKit/UIKit.h>

@interface ChakanViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * tableview;

//定义一个数组去接收数据库中返回的值
@property(nonatomic,strong)NSMutableArray * muarray;




@end
ChakanViewController.m文件

#import "ChakanViewController.h"
#import "User.h"

@interface ChakanViewController ()

@end

@implementation ChakanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableview =[[UITableView alloc]initWithFrame:CGRectMake(0,40 , 400, 700)];
    self.view.layer.backgroundColor=[UIColor whiteColor].CGColor;
     _tableview.dataSource=self;
    _tableview.delegate=self;
    [self.view addSubview:_tableview];
    
    UIButton * fanhui=[[UIButton alloc]initWithFrame:CGRectMake(10, 10, 40, 20)];
    fanhui.layer.backgroundColor=[UIColor redColor].CGColor;
    
    [fanhui setTitle:@"返回" forState:UIControlStateNormal];
    
    [fanhui addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:fanhui];
    
    //实例化一个User 用它的方法
    User * user=[User new];
    
    //将User中的返回数据赋值给_muarray
    _muarray=[user showNewGoodsInformation];
    
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_muarray count];
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cell=@"cell";
    UITableViewCell * Cell=[tableView dequeueReusableCellWithIdentifier:cell];
    if (Cell ==nil) {
        Cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell];
    }
    
    //数组中的是字典  现在用字典将其接收

    NSDictionary * dic=_muarray[indexPath.row];
    
 
    NSString *s=[NSString stringWithFormat:@"数量:%@     价格: %@",dic[@"goodsStorge"], dic[@"goodsPrice"]    ];
    
    Cell.textLabel.text=dic[@"goodsName"];
    Cell.detailTextLabel.text=s;
    
    return Cell;
}

-(void)fanhui{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end


你可能感兴趣的:(sqlite3 实际运用)