zoukankan      html  css  js  c++  java
  • FMDB

    //

    //  UserManager.m

    //  ChiHuo

    //

    //  Created by administrator on 12-8-28.

    //  Copyright (c) 2012 lxn. All rights reserved.

    //


    #import "UserManager.h"

    #import "AppDelegate.h"

    #import "User.h"


    @implementation UserManager


    @synthesize db = _db;


    -(void)showAlert:(NSString *)msg

    {

        UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"提示"

                                                        message:msg

                                                       delegate:nil

                                              cancelButtonTitle:@"OK"

                                              otherButtonTitles:nil];

        [alert show];

        [alert release];

    }


    -(BOOL)openDB

    {

        NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/chihuo.sqlite"];

        

        self.db = [FMDatabase databaseWithPath:dataPath];

        if(![_db open])

        {

            [self showAlert:@"数据库打开失败"];

            return NO;

        }

        

        returnYES;

    }


    -(void)registerWithName:(NSString *)name Password:(NSString *)pwd Phone:(NSString *)phone Address:(NSString *)address Email:(NSString *)email

    {

        if([self openDB])

        {

            if([_dbexecuteUpdate:[NSStringstringWithFormat:@"insert into member (name, password, phone, address, email) values ('%@', '%@', '%@', '%@', '%@' )", name, pwd, phone, address, email]])

                [self showAlert:@"注册成功"];

            else

                [self showAlert:@"注册失败"];

        }

    }


    -(User *)loginUserByName:(NSString *)name AndPassword:(NSString *)pwd

    {

        User *resUser = [[[User alloc] init] autorelease];

        resUser.userID = 0;

        if([self openDB])

        {

            FMResultSet *fResult = [_dbexecuteQuery:[NSStringstringWithFormat:@"select * from member where name = '%@' and password = '%@'", name, pwd ]];

            if([fResult next])

            {

                resUser.userID = [fResult intForColumn:@"memberID"];

                resUser.name = [ fResult stringForColumn:@"name"];

                resUser.pwd = [fResult stringForColumn:@"password"];

                resUser.phone = [fResult stringForColumn:@"phone"];

                resUser.address = [fResult stringForColumn:@"address"];

                resUser.email = [fResult stringForColumn:@"email"];

            }

            [fResult close];

        }

        

        [_db close];

        return resUser;

    }


    -(User *)getUserByID:(int)uid

    {

        User *resUser = [[[User alloc] init] autorelease];

        resUser.userID = 0;

        if([self openDB])

        {

            FMResultSet *fResult = [_dbexecuteQuery:[NSStringstringWithFormat:@"select * from member where memberID = %d",uid ]];

            if([fResult next])

            {

                resUser.userID = [fResult intForColumn:@"memberID"];

                resUser.name = [ fResult stringForColumn:@"name"];

                resUser.pwd = [fResult stringForColumn:@"password"];

                resUser.phone = [fResult stringForColumn:@"phone"];

                resUser.address = [fResult stringForColumn:@"address"];

                resUser.email = [fResult stringForColumn:@"email"];

            }

            [fResult close];

        }

        

        [_db close];

        return resUser;

    }


    -(void)leaveMessage:(NSString *)message andMemberID:(int)mid

    {

        if([self openDB])

        {

            if([_dbexecuteUpdate:[NSStringstringWithFormat:@"insert into message (message, memberID) values ('%@', %d)", message, mid]])

                [self showAlert:@"吐槽成功"];

            else

                [self showAlert:@"吐槽失败"];

        }

        [_db close];

    }


    -(NSMutableArray *)returnAllMessage;

    {

        NSMutableArray *msgArray = [[[NSMutableArrayalloc] init] autorelease];

        

        if([self openDB])

        {

            FMResultSet *fResult = [_db executeQuery:@"select * from message"];

            while([fResult next])

            {

                //根据留言者ID获取留言人信息

                int mid = [fResult intForColumn:@"memberID"];

                User * msgUser = [self getUserByID:mid];

                

                NSMutableDictionary *msgDic = [NSMutableDictionarydictionary];

                

                [msgDic setObject:[fResult stringForColumn:@"message"] forKey:@"message"];

                [msgDic setObject:msgUser.name  forKey:@"name"];

                

                [msgArray addObject:msgDic];

    //            

    //            [msgDic release];

            }

            [fResult close];

        }

        

        [_db close];

        return msgArray;

    }


    -(BOOL)modifyPwdByUserID:(int)mid withNewPwd:(NSString *)pwd

    {

        if([self openDB])

        {

            if([_dbexecuteUpdate:[NSStringstringWithFormat:@"update member set password = '%@' where memberID = %d", pwd, mid]])

                [self showAlert:@"密码修改成功"];

            else

            {

                [self showAlert:@"密码修改失败"];

                return NO;

            }

        }

        [_db close];

        returnYES;

    }


    -(NSMutableArray *)returnAllCar

    {

        NSMutableArray *carArray = [[[NSMutableArrayalloc] init] autorelease];

        

        if([self openDB])

        {

            AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];

            //AppDelegate中的carDic中依次取出所有的keyvalue的值

            //获取字典中的所有的Key的值:keyEnumeratorValue值:objectEnumerator

            NSEnumerator *keyEnum = [appDlg.carDic keyEnumerator];

           

            id obj; //定义一个不确定类型的对象

            //遍历字典输出所有的key

            while (obj = [keyEnum nextObject])

            {

                NSLog(@"键值为:%@",obj);

                

                //定义一个字典用于向数组中放入数据

                NSMutableDictionary *car = [NSMutableDictionarydictionary];


                //根据根据键值获取其对应的value(表示此菜的数目)的值

                id objValue = [appDlg.carDic objectForKey:obj];

                [car setObject:obj forKey:@"foodID"];    //存入食物的ID

                [car setObject:objValue forKey:@"count"];//存入食物的数量count

                

                //根据食物的ID获取食物的信息

                FMResultSet *foodResult = [_db executeQuery:[NSString stringWithFormat:@"select * from food where foodID = %@", obj]];

                if([foodResult next])

                {

                    //存入食物的名称name

                    //存入食物价格price

                    //存入食物的图片image

                    [car setObject:[foodResult stringForColumn:@"name"] forKey:@"name"];

                    [car setObject:[foodResult stringForColumn:@"price"] forKey:@"price"];

                    [car setObject:[foodResult stringForColumn:@"image"] forKey:@"image"];

                }

                [foodResult close];

                [carArray addObject:car];

            }

        }

        [_db close];

        return carArray;

    }


    -(float)returnMoney

    {

                float money = 0;

        if([self openDB])

        {

            AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];

            //AppDelegate中的carDic中依次取出所有的keyvalue的值

            //获取字典中的所有的Key的值:keyEnumeratorValue值:objectEnumerator

            NSEnumerator *keyEnum = [appDlg.carDic keyEnumerator];

            

            id obj; //定义一个不确定类型的对象

            //遍历字典输出所有的key

            while (obj = [keyEnum nextObject])

            {

                int foodID = [obj integerValue];  //存入食物的ID

                int count = [[appDlg.carDic objectForKey:obj] integerValue]; //存入食物的数量

                int memberID = appDlg.user.userID;

                NSLog(@"键值为:%@",obj);

                

                //将数据存入数据库中

                if([_dbexecuteUpdate:[NSStringstringWithFormat:@"insert into car (foodID, count, memberID) values (%d, %d, %d)", foodID, count, memberID]])

                {

                    //根据食物的ID获取食物的信息

                    FMResultSet *foodResult = [_db executeQuery:[NSString stringWithFormat:@"select * from food where foodID = %@", obj]];

                    if([foodResult next])

                    {

                        int oldNum = [[foodResult stringForColumn:@"sale"] integerValue];

                        //计算所有总订单的价钱

                        money = money + [[foodResult stringForColumn:@"price"] floatValue] * count;

                        

                        [_db executeUpdate:[NSString stringWithFormat:@"update food set sale = %d where foodID = %d",count + oldNum,foodID]];

                    }

                    [foodResult close];

                }

                else

                    [self showAlert:@"订单提交失败"];

                

                //根据食物的ID获取食物的信息

                FMResultSet *foodResult = [_db executeQuery:[NSString stringWithFormat:@"select * from food where foodID = %@", obj]];

                if([foodResult next])

                {

                    //计算所有总订单的价钱

                    money = money + [[foodResult stringForColumn:@"price"] floatValue] * count;

                }

                [foodResult close];

            }

        }

        [_db close];

        [self showAlert:@"订单提交成功"];

        return money;

    }


    @end

  • 相关阅读:
    深入浅出Mybatis系列(八)---objectFactory、plugins、mappers
    深入浅出Mybatis系列(七)---TypeHandler简介
    深入浅出Mybatis系列(六)---配置详解之typeAliases别名
    深入浅出Mybatis系列(四)---配置详解之properties与environments
    fastjson 的使用总结
    Java 中 Gson的使用
    Lombok介绍、使用方法和总结
    Jackson的使用和定制
    深入浅出Mybatis系列(三)---配置简介(mybatis源码篇)
    深入浅出Mybatis系列(二)---Mybatis入门
  • 原文地址:https://www.cnblogs.com/alihaiseyao/p/3363600.html
Copyright © 2011-2022 走看看