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