fmdb是ios比较常用的一种数据库的框架,现在的数据库有好多种,fmdb,直接使用sqlite,realm,coredate等等,今天我就简单介绍一下fmdb的使用
1.其实平时简单的使用不过就是增删改改查,我们看一下代码
数据库的创建:
//创建
- (void)creatFmdb {
//1.获得数据库文件的路径
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
NSString *fileName = [doc stringByAppendingPathComponent:@"student.db"];
//创建数据库
self.db = [FMDatabase databaseWithPath:fileName];
[self.db open];
NSString * sqlStr = @"create table if not exists Student_Messge (id integer primary key autoincrement, name text not null, age integer not null, sex text not null);";
[self.db setUserVersion:1];
NSLog(@" 数据库版本号 %d",self.db.userVersion);
BOOL result = [self.db executeUpdate:sqlStr];
//成功与否
if (!result) {
NSLog(@"创建表失败");
}
[self.db close];
}
以上所用的语言就是sqlite语言相信用过的人应该都很了解的
2.添加数据
//增
- (void)addDataToFmdb:(StudentModel *)student {
//打开失败
if (![self.db open]) {
return;
}
BOOL sucessce = [self.db executeUpdate:@"insert into Student_Messge (name, age, sex, adress) values (?,?,?,?);",student.name,@(student.age),student.sex,student.adress];
//如果还没有添加地址的话
if (!sucessce) {
sucessce = [self.db executeUpdate:@"insert into Student_Messge (name, age, sex) values (?,?,?);",student.name,@(student.age),student.sex];
if (!sucessce) {
NSLog(@" 添加信息失败 ");
}
}
[self.db close];
}
其主要的就是一句sqlite的语言,以下同样
3.删除数据
//删
- (void)deleteDataToFmdb:(NSString *)name {
if (![self.db open]) {
self.db = nil;
return;
}
//1.不确定的参数用?来占位 (后面参数必须是oc对象,需要将int包装成OC对象)
BOOL sucessce = [self.db executeUpdate:@"delete from Student_Messge where name = ?;",name];
if (!sucessce) {
NSLog(@" 数据库删除失败 ");
}
[self.db close];
// //2.不确定的参数用%@,%d等来占位
// [self.db executeUpdateWithFormat:@"delete from Student_Messge where name = %@;",@"apple_name"];
}
4.改数据
//改
- (void)chengeDataToFmdb:(NSString *)name newName:(NSString *)newName {
if (![self.db open]) {
self.db = nil;
return;
}
//修改学生的名字
BOOL sucessce = [self.db executeUpdate:@"update Student_Messge set name = ? where name = ?",newName,name];
if (!sucessce) {
NSLog(@" 名字修改失败 ");
}
[self.db close];
}
5.查数据
//查
- (NSMutableArray *)querayDataToFmdb {
if (![self.db open]) {
self.db = nil;
return nil;
}
//根据条件查询 现在查询的是所有的信息 条件自己添加
FMResultSet *resultSet = [self.db executeQuery:@"select * from Student_Messge;"];
NSMutableArray * mutbaleArray = [NSMutableArray array];
//遍历结果集合
while ([resultSet next]) {
NSString * name = [resultSet objectForColumnName:@"name"];
NSString * sex = [resultSet objectForColumnName:@"sex"];
int age = [resultSet intForColumn:@"age"];
NSString * adress = [resultSet objectForColumnName:@"adress"];
StudentModel * model = [[StudentModel alloc] init];
model.name = name;
model.sex = sex;
model.age = age;
model.adress = adress;
[mutbaleArray addObject:model];
}
return mutbaleArray;
}
6.数据库添加新的字段
//
- (void)addNewfFieldToSqlite:(NSString *)newField data:(NSString *)data{
if (![self.db open]) {
NSLog(@"数据库打开失败");
}
if (![self.db columnExists:newField inTableWithName:@"Student_Messge"]) {
NSString *sql = [NSString stringWithFormat:@"alter table Student_Messge ADD %@ text",newField];//这是给一张F名为人的表增加一个age字段
if ([self.db executeUpdate:sql]) {
NSLog(@"添加成功");
}
}
}
附上一个简单的demo https://github.com/shuaikun/FMDBDemo