标签:
一.首先创建模型类
User.h
#import <Foundation/Foundation.h>
@interface User : NSObject
@property(nonatomic,assign)NSInteger ids;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *classes;
@property(nonatomic,strong)NSString *grade;
@end
二.创建数据库的单例类 FMDBData
FMDBData.h
@interface FMDBData : NSObject
//单例类
+(instancetype)sharedFMDBData;
//增
-(BOOL)insertUser:(User *)user;
//删
-(BOOL)deleteUser:(User *)user;
//改
-(BOOL)updateUser:(User *)user;
//查询
-(NSMutableArray *)getAll;
//查询单个数据
-(User *)getMessage:(NSString *)name;
@end
FMDBData.m
导入FMDB头文件
#import "FMDatabase.h"
//全局静态变量
//数据库
static FMDatabase *fmdb;
//对象
static FMDBData *DBdata;
@implementation FMDBData
+(instancetype)sharedFMDBData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
DBdata = [[FMDBData alloc]init];
[DBdata initDB];
});
return DBdata;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!DBdata) {
DBdata = [super allocWithZone:zone];
}
return DBdata;
}
-(id)copy
{
return self;
}
-(id)mutableCopy
{
return self;
}
-(void)initDB
{
//沙盒路径
NSString *doucmentPach = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//拼接路径
NSString *pach = [doucmentPach stringByAppendingString:@"user.sqlite"];
//根据路径创建数据库
fmdb = [[FMDatabase alloc]initWithPath:pach];
if ([fmdb open]) {
//创建数据表
[fmdb executeUpdate:@"create table user(ids integer primary key autoincrement, name text, classes text, grade text)"];
NSLog(@"创建成功");
[fmdb close];
}
else
{
NSLog(@"创建失败");
}
}
//增
-(BOOL)insertUser:(User *)user
{
[fmdb open];
NSLog(@"%@",user.name);
BOOL isb = [fmdb executeUpdate:@"insert into user values (null,?,?,?)",user.name,user.classes,user.grade];
if (isb) {
NSLog(@"插入成功");
}
else
{
NSLog(@"插入失败");
}
[fmdb close];
return isb;
}
//删
-(BOOL)deleteUser:(User *)user
{
[fmdb open];
NSLog(@"%@",user.name);
BOOL isb = [fmdb executeUpdate:@"delete from user where name = ?",user.name];
if (isb) {
NSLog(@"删除成功");
}
else
{
NSLog(@"删除失败");
}
[fmdb close];
return isb;
}
//改
-(BOOL)updateUser:(User *)user
{
[fmdb open];
NSLog(@"%@",user.name);
NSString *str = [NSString stringWithFormat:@"update user set name = ‘%@‘ , classes = ‘%@‘ , grade = ‘%@‘ where ids = %ld",user.name,user.classes,user.grade,user.ids];
BOOL isb = [fmdb executeUpdate:str];
if (isb) {
NSLog(@"修改成功");
}
else
{
NSLog(@"修改失败");
}
[fmdb close];
return isb;
}
//查询所有
-(NSMutableArray *)getAll
{
//打开数据库
[fmdb open];
//创建可变数组
NSMutableArray *arr = [NSMutableArray array];
//创建结果对象接受数据库的操作结果
FMResultSet *fmset = [fmdb executeQuery:@"select * from user"];
//通过循环
while ([fmset next])
{
User *user = [[User alloc] init];
//用模型类对象接受数据库内容
user.ids = [fmset intForColumn:@"ids"];
user.name = [fmset stringForColumn:@"name"];
user.classes = [fmset stringForColumn:@"classes"];
user.grade = [fmset stringForColumn:@"grade"];
NSLog(@"%@",user.classes);
//将内容加入数组
[arr addObject:user];
}
//关闭数据库
[fmdb close];
return arr;
}
//查询单个数据
-(User *)getMessage:(NSString *)name
{
[fmdb open];
User *user = [[User alloc] init];
FMResultSet *fmset = [fmdb executeQuery:@"select * from user where name = ?",name];
while ([fmset next])
{
user.ids = [fmset intForColumn:@"ids"];
user.name = [fmset stringForColumn:@"name"];
user.classes = [fmset stringForColumn:@"classes"];
user.grade = [fmset stringForColumn:@"grade"];
}
[fmdb close];
return user;
}
@end
三.单例类的使用
1.删除按钮
- (IBAction)DeleteBtn:(id)sender {
//初始化FMDB
FMDBData *handle = [FMDBData sharedFMDBData];
//初始化单例类
User *user = [[User alloc] init];
//为 单例类的name赋值
user.name = self.NameTF.text;
//执行删除事件
BOOL isb = [handle deleteUser:user];
// [table reloadData];
if (isb) {
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"删除成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
}else{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"删除失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
}
[table reloadData];
}
2.查询按钮
- (IBAction)SelectBtn:(id)sender {
FMDBData *handle = [FMDBData sharedFMDBData];
if ([self.NameTF.text isEqualToString:@""]) {
arr = [handle getAll];
}
else
{
User *user=[handle getMessage:self.NameTF.text];
arr=[[NSMutableArray alloc]initWithObjects:user,nil];
}
[table reloadData];
}
3.添加按钮
-(void)click
{
//数据库
FMDBData *handle = [FMDBData sharedFMDBData];
//单例类
User *user = [[User alloc] init];
//保存数据
user.name = self.tfname.text;
user.classes = self.tfclass.text;
user.grade = self.tfscore.text;
//执行数据添加方法
BOOL isb = [handle insertUser:user];
if (isb)
{
//
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
}
}
4更新按钮 (传参为数据模型体)
-(void)click
{
FMDBData *hander=[FMDBData sharedFMDBData];
self.user.name = self.tfname.text;
self.user.classes = self.tfclass.text;
self.user.grade = self.tfscore .text;
NSLog(@"id:%ld",self.user.ids);
BOOL isb = [hander updateUser:self.user];
if (isb) {
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"修改失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
}
}
标签:
原文地址:http://www.cnblogs.com/yevgeni/p/5599293.html