IOS Sqlite用户界面增删改查案例
1.案例简介
2.项目工程目录
// // UserDB.h // SqliteDemo // // Created by 赵超 on 14-8-27. // Copyright (c) 2014年 赵超. All rights reserved. // #import "BaseDB.h" #import "UserModel.h" @interface UserDB : BaseDB{ } //单例 +(id)shareUserDB; //创建User表 -(void)creatTableWithDataBaseName:(NSString*) dbName; //增加一个UserModel -(BOOL)addUser:(UserModel*)userModel dbName:(NSString*)dbName; //修改一个UserModel -(BOOL)updateUser:(UserModel*)userModel dbName:(NSString*)dbName; //查询所有 -(NSArray*)findAllUser:(NSString*)dbName; //删除一个对象 -(BOOL) deleteUser:(UserModel*)userModel dbName:(NSString*)dbName; @endUserDB.m
// // UserDB.m // SqliteDemo // // Created by 赵超 on 14-8-27. // Copyright (c) 2014年 赵超. All rights reserved. // #import "UserDB.h" @implementation UserDB static UserDB * db; +(id) shareUserDB{ if (db==nil) { db= [[UserDB alloc] init]; } return db; } -(void)creatTableWithDataBaseName:(NSString*) dbName{ NSString *sql=@"create table user( userName text primary key,passWord text, userEmail text)"; [self createTable:sql dataBaseName:dbName]; } -(BOOL) deleteUser:(UserModel*)userModel dbName:(NSString*)dbName{ NSString *sql=@"delete from user where userName =?"; NSArray *params=@[ userModel.userName]; return [self execSql:sql parmas:params dataBaseName:dbName]; } -(BOOL)addUser:(UserModel *)userModel dbName:(NSString *)dbName{ NSString *sql=@"insert into user (userName,passWord,userEmail) values (?,?,?)"; NSArray *params=@[userModel.userName,userModel.passWord,userModel.userEmail]; return [self execSql:sql parmas:params dataBaseName:dbName]; } -(NSArray*) findAllUser:(NSString *)dbName{ NSString *sql=@"select userName,passWord,userEmail from user"; NSArray *result= [self selectSql:sql parmas:nil dataBaseName:dbName]; NSMutableArray *users=[NSMutableArray array]; for (NSDictionary *dic in result) { UserModel *user=[[UserModel alloc] init]; user.userName=[dic objectForKey:@"userName"]; user.passWord=[dic objectForKey:@"passWord"]; user.userEmail=[dic objectForKey:@"userEmail"]; [users addObject:user]; } return users; } -(BOOL)updateUser:(UserModel *)userModel dbName:(NSString *)dbName{ NSString *sql=@"update user set userName=?,passWord=?,userEmail=? where userName=?"; NSArray *params=@[userModel.userName,userModel.passWord,userModel.userEmail,userModel.userName]; return [self execSql:sql parmas:params dataBaseName:dbName]; } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。