系统自带可以储存字段的字典:
NSUserDefaults *user = [[NSUserDefaults alloc] init];
存 : [user setObject:@"YES" forKey:@"token"];
取:NSString* string=[user objectForKey:@"token"];
FMdb第三方使用 使用前导入第三方框架外还需要导入libsqlite3包
//创建模型
#import <Foundation/Foundation.h> @interface LoadModel : NSObject
@property (nonatomic,retain) NSString * username;
@property (nonatomic,retain) NSString * password;
@end #import "DataCenter.h"
#import "FMDatabase.h"
@implementation DataCenter
{
FMDatabase *_dataBase;
}
+(id)shareDataCenter{
static DataCenter *center = nil;
if(center == nil){
center = [[DataCenter alloc] init];
}
return center;
}
-(id)init{
self = [super init];
if(self){
[self createDB];
}
return self;
}
-(void)createDB{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbPath = [array[] stringByAppendingPathComponent:@"CB.db"];
NSLog(@"数据库地址:%@",dbPath);
_dataBase = [[FMDatabase alloc] initWithPath:dbPath];
[_dataBase open]; //用户登录
[self createUserLoadTable]; } //创建表 表名 CBUser 列: username password
-(void)createUserLoadTable{
NSString *sql = @"create table if not exists CBUser (ID integer primary key autoincrement,username text,password text);";
BOOL b = [_dataBase executeUpdate:sql];
if(!b){
NSLog(@"登录表创建失败");
}
} //储存数据 账号 密码
-(void)insertDataWithModel:(LoadModel *)model{
NSString *sql = @"insert into CBUser (username,password) values (?,?)";
BOOL b = [_dataBase executeUpdate:sql,model.username,model.password];
if(!b){
NSLog(@"等录数据加失败");
}
}
//查找数据
-(LoadModel *)selectDataWithModel:(NSString *)username{
NSString *sql = @"select * from CBUser where username = ?";
FMResultSet *set = [_dataBase executeQuery:sql,username];
LoadModel *model = [[LoadModel alloc] init];
while (set.next) {
//取出名字和密码
model.username = [set stringForColumn:@"username"];
model.password = [set stringForColumn:@"password"];
}
return model;
}
-(BOOL)selectDataBoolWithNum:(NSString *)username{
NSString *sql = @"select * from CBUser where username = ?";
FMResultSet *set = [_dataBase executeQuery:sql,username];
while (set.next) {
return YES;
}
return NO;
}
//删除表
-(void)deleteAllData{
NSString *sql = @"delete from CBUser";
[_dataBase executeUpdate:sql];
}
//获取全部数据
-(NSArray *)selectAllData{
NSString *sql = @"select * from CBUser";
FMResultSet *set = [_dataBase executeQuery:sql];
NSMutableArray *array = [[NSMutableArray alloc] init];
while (set.next) {
LoadModel *model = [[LoadModel alloc] init];
model.username = [set stringForColumn:@"username"];
model.password = [set stringForColumn:@"password"];
[array addObject:model];
}
return array;
}
//删除某一个数据
-(void)deleteDataWithName:(NSString *)username{
NSString *sql = @"delete from CBUser where username = ?";
[_dataBase executeUpdate:sql,username];
} @end //调用
DataCenter* _center=[DataCenter new];
LoadModel* model=[[LoadModel alloc]init];
model.username=@"";
model.password=@"";
[_center insertDataWithModel:model];