//
// 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 = [[UIAlertVIEw alloc] 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;
}
return YES;
}
-(voID)registerWithname:(Nsstring *)name Password:(Nsstring *)pwd Phone:(Nsstring *)phone Address:(Nsstring *)address Email:(Nsstring *)email
{
if([self openDB])
{
if([_db executeUpdate:[Nsstring stringWithFormat:@"insert into member (name,password,phone,address,email) values ('%@','%@','%@' )",name,pwd,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 = [_db executequery:[Nsstring stringWithFormat:@"select * from member where name = '%@' and password = '%@'",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 = [_db executequery:[Nsstring stringWithFormat:@"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([_db executeUpdate:[Nsstring stringWithFormat:@"insert into message (message,memberID) values ('%@',%d)",message,mID]])
[self showAlert:@"吐槽成功"];
else
[self showAlert:@"吐槽失败"];
}
[_db close];
}
-(NSMutableArray *)returnAllMessage;
{
NSMutableArray *msgArray = [[[NSMutableArray alloc] 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 = [NSMutableDictionary dictionary];
[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([_db executeUpdate:[Nsstring stringWithFormat:@"update member set password = '%@' where memberID = %d",mID]])
[self showAlert:@"密码修改成功"];
else
{
[self showAlert:@"密码修改失败"];
return NO;
}
}
[_db close];
return YES;
}
-(NSMutableArray *)returnAllCar
{
NSMutableArray *cararray = [[[NSMutableArray alloc] init] autorelease];
if([self openDB])
{
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//从AppDelegate中的carDic中依次取出所有的key和value的值
//获取字典中的所有的Key的值:keyEnumerator;Value值:objectEnumerator
NSEnumerator *keyEnum = [appDlg.carDic keyEnumerator];
ID obj; //定义一个不确定类型的对象
//遍历字典输出所有的key
while (obj = [keyEnum nextObject])
{
NSLog(@"键值为:%@",obj);
//定义一个字典用于向数组中放入数据
NSMutableDictionary *car = [NSMutableDictionary dictionary];
//根据根据键值获取其对应的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中依次取出所有的key和value的值
//获取字典中的所有的Key的值:keyEnumerator;Value值: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([_db executeUpdate:[Nsstring stringWithFormat:@"insert into car (foodID,count,memberID) values (%d,%d,foodID,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
总结以上是内存溢出为你收集整理的草稿FMDB全部内容,希望文章能够帮你解决草稿FMDB所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)