数据库范例

//这是一个添加商品数据库模型:

//我们按照MVC分离的思想建立了Mmodel层)-Vview层)-Ccontroller层)----(这里的代码没有完全的MVC分离,只有在列表查看页面是MVC分离的)

//model层的是数据库信息,里面是一个数据库的创建和添加,查看三个方法

//View层里是列表的实现,TableViewCell是手写的自定义Cell文件TableView是手写的查看商品实现环节

//Controller层是控制View



// 关于[self presentViewController:将要跳转的ViewController对象 animated:跳转是否有动画效果 completion:(这是一个代码块)完成其他内容]; 是跳转方法,有三个参数,冒号后是对参数的描述


#import "ViewController.h"

#import "addViewController.h"

#import "toTableViewViewController.h"

@interface ViewController ()


@end


@implementation ViewController


- ( void ) viewDidLoad {

    [ super viewDidLoad ] ;

    UIButton * buttonAddGoods = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 40, 200, 90, 50  ) ] ;

    [ buttonAddGoods setTitle : @"添加" forState : UIControlStateNormal ] ;

    [ buttonAddGoods setTitleColor : [UIColor redColor] forState : UIControlStateNormal ] ;

    [ buttonAddGoods addTarget : self action : @selector(add:) forControlEvents : UIControlEventTouchUpInside ] ;

    [ self.view addSubview:buttonAddGoods ] ;

    

    

    UIButton * buttonSearch = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 200, 200, 90, 50 ) ] ;

    [ buttonSearch setTitle : @"查看" forState:UIControlStateNormal ] ;

    [ buttonSearch setTitleColor : [UIColor redColor] forState:UIControlStateNormal ] ;

    [ buttonSearch addTarget : self action : @selector(search:) forControlEvents : UIControlEventTouchUpInside ] ;

    [ self.view addSubview : buttonSearch ] ;

}

- ( void ) add : ( UIButton * ) sender {

    

    addViewController * addVc = [ [ addViewController alloc ] init ];//实例化跳转页面

    [ self presentViewController : addVc animated : NO completion : nil ]; //这是跳转方法

    

}

-( void ) search : ( UIButton * ) sender {

    toTableViewViewController * tableViewVC = [ [ toTableViewViewController alloc ] init ] ;

    [ self presentViewController : tableViewVC animated : NO completion : nil ] ; //这是跳转方法

}




@interface addViewController : UIViewController

@property ( nonatomic , strong ) UITextField  * nameLabel ;

@property ( nonatomic , strong ) UITextField  * priceLabel ;

@property ( nonatomic , strong ) UITextField  * numberLabel ;

@end


@implementation addViewController


- ( void ) viewDidLoad {

    [ super viewDidLoad ] ;

    self.view.backgroundColor = [ UIColor whiteColor ];

 

    //名称提示:

    UILabel * lableNa = [ [ UILabel alloc ]initWithFrame : CGRectMake (15 , 110 , 50 , 20) ] ;

    lableNa.font = [ UIFont fontWithName : @"Arial" size : 14 ];

    lableNa.textColor = [ UIColor blackColor ];

    lableNa.text = @"商品名" ;

    [ self.view addSubview :lableNa ] ;

    

    _nameLabel = [ [UITextField alloc] initWithFrame : CGRectMake (100, 110, 225, 20 ) ] ;

    _nameLabel.borderStyle = UITextBorderStyleRoundedRect ;

    _nameLabel.font = [ UIFont fontWithName : @"Arial" size:16 ] ;

    _nameLabel.textColor = [ UIColor blackColor ] ;

    [ self.view addSubview:_nameLabel ] ;

    

    

    //商品价格

    UILabel * lableP = [ [ UILabel alloc ]initWithFrame : CGRectMake ( 15, 132, 50, 20 ) ] ;

    lableP.font = [ UIFont fontWithName : @"Arial" size : 14 ];

    lableP.textColor = [ UIColor blackColor ] ;

    lableP.text = @"价格" ;

    [ self.view addSubview : lableP ] ;

    

    //商品价位

    _priceLabel = [ [ UITextField alloc ] initWithFrame : CGRectMake(49, 132, 70, 20) ] ;

    _priceLabel.borderStyle = UITextBorderStyleRoundedRect;

    _priceLabel.font = [ UIFont fontWithName:@"Arial" size:14 ];

    _priceLabel.textColor = [ UIColor blackColor ];

    [ self.view addSubview:_priceLabel ] ;

    

    //商品库存标签

    UILabel  * lableN=[ [ UILabel alloc ] initWithFrame:CGRectMake(169, 132, 50, 20) ] ;

    lableN.font = [UIFont fontWithName:@"Arial" size:14 ] ;

    lableN.textColor = [ UIColor blackColor ] ;

    lableN.text = @"库存";

    [ self.view addSubview:lableN ] ;

    

    //商品库存

    _numberLabel=[[UITextField alloc]initWithFrame:CGRectMake(215, 132, 55, 20)];

    _numberLabel.borderStyle = UITextBorderStyleRoundedRect;

    _numberLabel.font=[UIFont fontWithName:@"Arial" size:14];

    _numberLabel.textColor=[UIColor blackColor];

    [self.view addSubview:_numberLabel];

    

    

    UIButton *buttonAddGoods=[[UIButton alloc]initWithFrame:CGRectMake(40, 200, 90, 50)];

    [buttonAddGoods setTitle:@"添加" forState:UIControlStateNormal];

    [buttonAddGoods setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [buttonAddGoods addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonAddGoods];

    

    

    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(230, 200, 90, 50)];

    [button setTitle:@"返回" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];


}

-(void)add:(UIButton *)sender{

// 提问:在存入前需要检查数据真实性和存在性吗?怎么做?

    Goods *addGoods=[[Goods alloc]init];

    [addGoods addNewGoods:_nameLabel.text withPrice:_priceLabel.text andStorage:_numberLabel.text];//利用自己写的方法将数据存好

    

}

-(void)jump{

    [self dismissViewControllerAnimated:NO completion:nil];//使用present……的方法跳出的页面消失

}



#import <UIKit/UIKit.h>


@interface toTableViewViewController : UIViewController


@end


#import "toTableViewViewController.h"

#import "tableView.h"

#import "Goods.h"


@interface toTableViewViewController ()


@end


@implementation toTableViewViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    Goods *good=[[Goods alloc]init];

    tableView *table=[[tableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    table.arrayContent=[good showNewGoodsInformation];

    [self.view addSubview:table];

    UIButton *but=[[UIButton alloc]initWithFrame:CGRectMake(0, 30, 70, 20)];

    [but setTitle:@"返回" forState:UIControlStateNormal];

    [but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [but addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];

    [table addSubview:but];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)jump{

    [self dismissViewControllerAnimated:NO completion:nil];//使用present……的方法跳出的页面消失

}



#import <UIKit/UIKit.h>


@interface tableView : UIView<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong)UITableView *myListTableView;

@property (nonatomic, strong)NSString *myListSendState;

@property(nonatomic,strong)NSMutableArray *arrayContent;



@end


#import "tableView.h"


#import "TableViewCell.h"

@implementation tableView

- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    self.backgroundColor=[UIColor colorWithRed:0.922 green:0.922 blue:0.945 alpha:1.000];

       

    

    _arrayContent=[[NSMutableArray alloc]initWithCapacity:10];

    _myListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,50, frame.size.width, frame.size.height) style:UITableViewStyleGrouped];

    _myListTableView.dataSource = self;

    _myListTableView.delegate = self;

//        _myListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//设置列表是否有行线

    [self addSubview:_myListTableView];

    return self;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_arrayContent count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *reuse = @"myListTableViewCell";

   

    TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];

    if (cell==nil) {

        cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];

    }

    

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSDictionary *goodsDict=_arrayContent[indexPath.row];

    cell.nameLabel.text=goodsDict[@"goodsName"];

    cell.priceLabel.text = goodsDict[@"goodsPrice"];

    cell.numberLabel.text = goodsDict[@"goodsStorge"];

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;

}




#import <UIKit/UIKit.h>


@interface TableViewCell : UITableViewCell

@property(nonatomic,strong)UILabel * nameLabel;

@property(nonatomic,strong)UILabel * priceLabel;

@property(nonatomic,strong)UILabel * numberLabel;

@end


#import "TableViewCell.h"


@implementation TableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reusename{

    self=[super initWithStyle:style reuseIdentifier:reusename];

   //商品名显示标签

    _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 225, 20)];

    _nameLabel.font=[UIFont fontWithName:@"Arial" size:16];

    _nameLabel.textColor=[UIColor blackColor];

    [self addSubview:_nameLabel];

    

    

    //商品价格¥

    UILabel *lableP=[[UILabel alloc]initWithFrame:CGRectMake(15, 32, 50, 20)];

    lableP.font=[UIFont fontWithName:@"Arial" size:14];

    lableP.textColor=[UIColor blackColor];

    lableP.text=@"价格:";

    [self addSubview:lableP];

    

    //商品价位

    _priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(49, 32, 70, 20)];

    _priceLabel.font=[UIFont fontWithName:@"Arial" size:14];

    _priceLabel.textColor=[UIColor colorWithRed:1.000 green:0.311 blue:0.249 alpha:1.000];

    [self addSubview:_priceLabel];

    

    //商品库存标签

    UILabel *lableN=[[UILabel alloc]initWithFrame:CGRectMake(169, 32, 36, 20)];

    lableN.font=[UIFont fontWithName:@"Arial" size:14];

    lableN.textColor=[UIColor blackColor];

    lableN.text=@"库存:";

    [self addSubview:lableN];

    

    //商品库存

    _numberLabel=[[UILabel alloc]initWithFrame:CGRectMake(215, 32, 55, 20)];

    _numberLabel.font=[UIFont fontWithName:@"Arial" size:14];

    _numberLabel.textColor=[UIColor blackColor];

    [self addSubview:_numberLabel];

    

    return self;

}



#import <Foundation/Foundation.h>

#import "sqlite3.h"

@interface Goods : NSObject

{

    sqlite3 *link;

    NSString *path;

}


//添加一条新商品数据

//4个参数:0.商品Id 1.商品名  2.商品价格  3.商品库存


- (NSInteger) addNewGoods: (NSString*)addGoodsName withPrice : (NSString*) addGoodsPrice andStorage : (NSString*) addGoodsStorage ;

//显示新添加的商品数据

-(NSMutableArray*) showNewGoodsInformation;

@end


#import "Goods.h"


@implementation Goods

-(id)init{

    self = [super init];

    //确定数据库文件,完成数据库链接

    path = @"/Users/feifanchengxuyuan/Desktop/MicroMall.db";

    sqlite3_open([path UTF8String], &link);

    

    //新建商品Goods: //4个参数:0.商品Id 1.商品名  2.商品价格  3.商品库存

    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(link, [createGoodsTable UTF8String], nil, nil, nil);

    

    return self;

}


//添加一条新商品数据

- (NSInteger) addNewGoods: (NSString*)addGoodsName withPrice : (NSString*) addGoodsPrice andStorage : (NSString*) addGoodsStorage{

    sqlite3_stmt *state;

    NSString *goodsId;

    NSString *insertGoods = [NSString stringWithFormat:@"insert into goods(goods_name,goods_Price,goods_storage)values(\"%@\",\"%@\",\"%@\")",addGoodsName,addGoodsPrice,addGoodsStorage];

    

    

    if(sqlite3_exec(link, [insertGoods UTF8String], nil, nil, nil) == SQLITE_OK){

        //查找新添加商品的商品Id

        NSString *selectNewGoods = [NSString stringWithFormat: @"select goods_id from goods where goods_name = \"%@\" ",addGoodsName];

        sqlite3_prepare_v2(link, [selectNewGoods UTF8String], -1, &state, nil);

        while (sqlite3_step(state) == SQLITE_ROW) {

            goodsId = [NSString stringWithUTF8String:(char*)sqlite3_column_text(state, 0)];

        }

      }

    NSLog(@"Success");

    return [goodsId integerValue];

}

//显示新添加的商品数据

-(NSMutableArray*) showNewGoodsInformation{

    sqlite3_stmt *state;

    NSString *goodsId;

    NSString *goodsName;

    NSString *goodsPrice;

    NSString *goodsStorge;

    NSDictionary *showNewGoodsInforDict;

    NSMutableArray *showTheUserGoodslistArray=[[NSMutableArray alloc]initWithCapacity:10];

    //根据商品Id查找商品信息

    NSString *selectGoods = [NSString stringWithFormat: @"select * from goods"];

    sqlite3_prepare_v2(link, [selectGoods UTF8String], -1, &state, nil);

    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];

        showNewGoodsInforDict = @{@"goodsId": goodsId, @"goodsName": goodsName,  @"goodsPrice": goodsPrice, @"goodsStorge": goodsStorge};

        [showTheUserGoodslistArray addObject:showNewGoodsInforDict];


    }

   

    return showTheUserGoodslistArray;

}





#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>


#define USER_FILE "user.dat"


typedef int (*MENU) (void);

typedef int (*ONMENU) (void);


typedef struct tag_User {

char name[256];

char passwd[256];

} USER;


void menuLoop (MENU menu, ONMENU onMenu[], size_t menus) {

for (;;) {

int idMenu = menu ();

if (idMenu < 0 || menus <= idMenu)

printf ("鏃犳晥閫夋嫨锛乗n");

else if (onMenu[idMenu] () < 0)

break;

}

}


int menuLogin (void) {

printf ("------------\n");

printf ("瀛︾敓绠$悊绯荤粺\n");

printf ("------------\n");

printf ("[1] 娉ㄥ唽    \n");

printf ("[2] 鐧诲綍    \n");

printf ("[0] 閫€鍑?   \n");

printf ("------------\n");

printf ("璇烽€夋嫨锛?);


int idMenu = -1;

if (scanf ("%d", &idMenu) != 1)

scanf ("%*[^\n]");


return idMenu;

}


int menuStudent (void) {

printf ("------------\n");

printf ("瀛︾敓绠$悊绯荤粺\n");

printf ("------------\n");

printf ("[1] 澧炲姞瀛︾敓\n");

printf ("[2] 鍒犻櫎瀛︾敓\n");

printf ("[3] 娴忚瀛︾敓\n");

printf ("[0] 娉ㄩ攢    \n");

printf ("------------\n");

printf ("璇烽€夋嫨锛?);


int idMenu = -1;

if (scanf ("%d", &idMenu) != 1)

scanf ("%*[^\n]");


return idMenu;

}


int onRegister (void) {

USER userNew;

printf ("鐢ㄦ埛鍚嶏細");

scanf ("%s", userNew.name);

printf ("瀵嗙爜锛?);

scanf ("%s", userNew.passwd);


int fd = open (USER_FILE, O_RDWR | O_CREAT, 0644);

if (fd == -1) {

perror ("open");

return -1;

}


USER userOld;

ssize_t bytes;


while ((bytes = read (fd, &userOld, sizeof (userOld))) > 0)

if (! strcmp (userOld.name, userNew.name)) {

printf ("鐢ㄦ埛鍚嶅凡瀛樺湪锛屾敞鍐屽け璐ワ紒\n");

close (fd);

return 0;

}


if (bytes == -1) {

perror ("read");

close (fd);

return -1;

}


if (write (fd, &userNew, sizeof (userNew)) == -1) {

perror ("write");

close (fd);

return -1;

}


close (fd);


printf ("娉ㄥ唽鎴愬姛锛乗n");


return 0;

}


int onAdd (void) {

printf ("褰曞叆瀛︾敓淇℃伅...\n");

return 0;

}


int onDel (void) {

printf ("鍒犻櫎瀛︾敓淇℃伅...\n");

return 0;

}


int onBrowse (void) {

printf ("娴忚瀛︾敓淇℃伅...\n");

return 0;

}


int onLogout (void) {

return -1;

}


int onLogin (void) {

USER userLog;

printf ("鐢ㄦ埛鍚嶏細");

scanf ("%s", userLog.name);

printf ("瀵嗙爜锛?);

scanf ("%s", userLog.passwd);


int fd = open (USER_FILE, O_RDONLY | O_CREAT, 0644);

if (fd == -1) {

perror ("open");

return -1;

}


USER userOld;

ssize_t bytes;


while ((bytes = read (fd, &userOld, sizeof (userOld))) > 0)

if (! strcmp (userOld.name, userLog.name))

if (strcmp (userOld.passwd, userLog.passwd)) {

printf ("瀵嗙爜閿欒锛岀櫥褰曞け璐ワ紒\n");

close (fd);

return 0;

}

else

break;


if (bytes == -1) {

perror ("read");

close (fd);

return -1;

}


if (bytes == 0) {

printf ("鐢ㄦ埛鍚嶉敊璇紝鐧诲綍澶辫触锛乗n");

close (fd);

return 0;

}


close (fd);


ONMENU onMenu[] = {onLogout, onAdd, onDel, onBrowse};

menuLoop (menuStudent, onMenu, sizeof (onMenu) / sizeof (onMenu[0]));


return 0;

}


int onQuit (void) {

return -1;

}


int main (void) {

ONMENU onMenu[] = {onQuit, onRegister, onLogin};

menuLoop (menuLogin, onMenu, sizeof (onMenu) / sizeof (onMenu[0]));

return 0;

}


转载于:https://my.oschina.net/u/2501648/blog/533434

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值