數據庫範例

//這是一個添加商品數據庫模型:sql

//咱們按照MVC分離的思想創建了Mmodel層)-Vview層)-Ccontroller層)----(這裏的代碼沒有徹底的MVC分離,只有在列表查看頁面是MVC分離的)數據庫

//model層的是數據庫信息,裏面是一個數據庫的建立和添加,查看三個方法ide

//View層裏是列表的實現,TableViewCell是手寫的自定義Cell文件TableView是手寫的查看商品實現環節oop

//Controller層是控制View動畫



// 關於[self presentViewController:將要跳轉的ViewController對象 animated:跳轉是否有動畫效果 completion:(這是一個代碼塊)完成其餘內容]; 是跳轉方法,有三個參數,冒號後是對參數的描述ui


#import "ViewController.h"atom

#import "addViewController.h"spa

#import "toTableViewViewController.h".net

@interface ViewController ()3d


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

}

相關文章
相關標籤/搜索