每次更新用户数据用插入,在插入前删除数据,登出把数据删了(登录获取用户数据)。
#import
@interface UserModel : NSObject
///用户ID
@property (nonatomic , assign) NSInteger identifie;
///姓名
@property (nonatomic , strong) NSString * name;
///性别
@property (nonatomic , strong) NSString * sex;
///年龄
@property (nonatomic , assign) NSInteger age;
///手机
@property (nonatomic , strong) NSString * mobile;
///头像ID
@property (nonatomic , assign) NSInteger image_id;
///头像路径
@property (nonatomic , strong) NSString * image_path;
@end
UserDataManger.h
#import "FMDB.h"
#import "UserModel.h"
@interface UserDataManger : NSObject
#pragma mark ----单例----
+(UserDataManger *)shareInstance;
#pragma mark ---打开数据库---
-(void)openDB;
#pragma mark ---在数据库中创建表---
-(void)createTableWithTableName;
#pragma mark ---添加数据---
-(void)insertUserData:(UserModel *)usermodel ;
#pragma mark ---查询数据---
-(UserModel *)getUserInfor;
@end
UserDataManger.m
#import "UserDataManger.h"
static FMDatabase *db = nil;
@implementation UserDataManger
+(UserDataManger *)shareInstance
{
static dispatch_once_t onceToken;
static UserDataManger * manger = nil;
dispatch_once(&onceToken, ^{
manger = [[UserDataManger alloc]init];
[manger openDB];
[manger createTableWithTableName];
});
return manger;
}
-(NSString *)createSqliteWithSqliteName
{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"user.sqlite"];
}
#pragma mark ---打开数据库---
-(void)openDB
{
if (db != nil) {
return;
}
NSString *path = [self createSqliteWithSqliteName];
NSLog(@"path ==== %@",path);
db = [FMDatabase databaseWithPath:path];
if ([db open]) {
NSLog(@"数据库打开成功");
}
else {
NSLog(@"数据库打开失败");
}
}
#pragma mark ---在数据库中创建表---表名--user
-(void)createTableWithTableName
{
[db open];
NSString *string = [NSString stringWithFormat:@"create table if not exists user (ID integer primary key autoincrement, identifie integer, name text, sex text, age integer, mobile text, image_id integer, image_path text)"];
BOOL flag = [db executeUpdate:string];
NSLog(@"创建表flag ==== %d",flag);
}
#pragma mark ---添加数据---
-(void)insertUserData:(UserModel *)usermodel
{
UserModel * model = usermodel;
[db open];
//这样设计适合存个人数据
NSInteger count =[[db stringForQuery:@"select count(*) from user"] integerValue];
if (count > 0) {
[db executeUpdate:@"DELETE FROM user WHERE 1=1"];
}
NSString *string = [NSString stringWithFormat:@"insert into user (identifie, name, sex, age, mobile, image_id, image_path) values (?, ?, ?, ?, ?, ?, ?)"];
NSString *identifie = [NSString stringWithFormat:@"%ld",(long)model.identifie];
NSString *age = [NSString stringWithFormat:@"%ld",(long)model.age];
NSString *image_id = [NSString stringWithFormat:@"%ld",(long)model.image_id];
BOOL flag = [db executeUpdate:string,identifie,model.name,model.sex,age,model.mobile,image_id,model.image_path];
NSLog(@"插入数据flag ==== %d",flag);
[db close];
}
#pragma mark ---查询数据---
-(UserModel *)getUserInfor{
UserModel *model = [[UserModel alloc]init];
[db open];
NSString *string = [NSString stringWithFormat:@"select * from user"];
FMResultSet *result = [db executeQuery:string];
while ([result next]) {
model.identifie = [result intForColumn:@"identifie"];
model.name = [result stringForColumn:@"name"];
model.sex = [result stringForColumn:@"age"];
model.age = [result intForColumn:@"age"];
model.mobile = [result stringForColumn:@"mobile"];
model.image_id = [result intForColumn:@"image_id"];
model.image_path = [result stringForColumn:@"image_path"];
}
[db close];
return model;
}
@end
vc
- (IBAction)addUserInfor:(UIButton *)sender {
UserModel * model = [[UserModel alloc]init];
model.identifie = 1111;
model.name = @"刘哥哥";
model.sex = @"男";
model.age = 23;
model.mobile = @"13612345678";
model.image_id = 123;
model.image_path = @"http://ww2.sinaimg.cn/mw690/95a6ddc2gw1f9pl2eeycpj20j60l7mz7.jpg";
[[UserDataManger shareInstance]insertUserData:model];;
}
- (IBAction)search:(UIButton *)sender {
UserModel * model = [[UserDataManger shareInstance]getUserInfor];
NSLog(@"%@",model.name);
}