FMDB
//导入三方数据库FMDB
//创建一个数据类其中要导入"FMDatabase.h"声明一个+方法获得数据库路径
//类私有
//静态的全局变量。静态变量只初始化一次,节省内存空间。
static FMDatabase *db =nil;
- (void)dealloc {
[db release];
[super dealloc];
}
+ (FMDatabase *)getDataBase {
NSString * filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/dataBase.sqlite"];
NSLog(@"%@",filePath);
//!db (db == nil)
if(db==nil) {
db= [[FMDatabase alloc] initWithPath:filePath];
}
return db;
}
//创建一个处理表格操作的类DAO(增,删,改,查)
+ (void)createTable {
//获得数据库
FMDatabase *db = [DataBaseUtil getDataBase];
//打开数据库直接open
if([db open] == YES) {
NSLog(@"db open success");
//判断表格是否存在
if([db tableExists:@"studentTB"] == NO) {
//executeUpdate
BOOL isYES = [db executeUpdate:@"create table studentTB (student_id integer ,student_name text not null,student_age text not null)"];
NSLog(@"%@",isYES ==YES?@"创建表格成功":@"创建表格失败");
}
}
//关闭数据库
[db close];
}
//插入数据的方法
+ (void)insertTableWith:(student *)student {
/*
*获得数据库---打开数据裤裤---执行操作---关闭数据库
*/
FMDatabase *db = [DataBaseUtil getDataBase];
if([db open] == YES) {
NSLog(@"open OK");
NSNumber *ID = [NSNumber numberWithInt:student.ID];
BOOL isYES = [db executeUpdate:@"insert into studentTB (student_id,student_name ,student_age) values (?,?,?)",ID, student.name,student.age];
NSLog(@"%@",isYES ==YES?@"插入数据成功":@"插入数据失败");
}
[db close];
}
//删除
+ (void)deleteTableWith:(student *)student {
FMDatabase *db = [DataBaseUtil getDataBase];
if([db open] == YES) {
//所有传给executeUpdate方法中的数字参数都必须封装为number对象
// NSNumber * ID = [NSNumber numberWithInt:student.ID];
BOOL isYES = [db executeUpdate:@"delete from studentTB where student_name like 'renew'",student.name];
NSLog(@"%@",isYES ==YES?@"删除成功":@"删除失败");
}
[db close];
}
//修改
+ (void)updateTableWith:(student *)student {
FMDatabase *db = [DataBaseUtil getDataBase];
if([db open] == YES) {
NSNumber *ID = [NSNumber numberWithInt:student.ID];
BOOL isYES = [db executeUpdate:@"update studentTB set student_name = ? where student_id = ?",student.name,ID];
NSLog(@"%@",isYES ==YES?@"修改成功":@"修改失败");
}
[db close];
}
//查询
+ (NSMutableArray *)getAllData {
NSMutableArray *dataArray = nil;
FMDatabase *db = [DataBaseUtil getDataBase];
if([db open] == YES) {
//数据库打开成功再初始化
dataArray = [NSMutableArray array];
//executeQuery返回一个结果集合
FMResultSet *set = [db executeQuery:@"select * from studentTB "];
//在结果集合中
while([set next]) {
//解析数据类型要跟声明是一致
int ID = [set intForColumn:@"student_id"];
NSString *name = [set stringForColumn:@"student_name"];
NSString *age = [set stringForColumn:@"student_age"];
NSLog(@"%d---%@---%@",ID,name,age);
//模型接收数据
student *stu = [[student alloc] init];
stu.ID= ID;
stu.name= name;
stu.age= age;
//添加模型
[dataArray addObject:stu];
}
NSLog(@"%@",dataArray);
//集合关闭/销毁容器
[set close];
}
[db close];
return dataArray;
}
//创建一个模型类声明各种属性存储数据
//模型存储数据
@interface student :NSObject
@property (nonatomic,copy)NSString * name;
@property (nonatomic,copy)NSString * age;
@property (nonatomic,assign)intID;
//在viewDidLoad进行结果数据操作
student * stu = [[studentalloc]init];
stu.name = @"who";
stu.ID = 12345;
//修改数据的时候重写的数据内容
stu.ID= 54321;
stu.name=@"you";
stu.age=@"22";
//[StudentDAO createTable];
//[StudentDAO insertTableWith:stu];
//[StudentDAO deleteTableWith:stu];
//[StudentDAO updateTableWith:stu];
[StudentDAOgetAllData];
//出了方法区域会释放运行会出现崩溃retain一下就不会
array= [[StudentDAOgetAllData]retain];