IOS-FMDB基础实例
- 项目中导入FMDB
- 1.手动添加,FMDB库文件。
- 2.添加编译参数,MRC和ARC混编。
- 3.导入系统依赖库,libsqlite3.tbd
- 4.在需要的地方添加,FMDatabase.h
- 数据库的基本使用
- 1.新建数据库
- 2.新建表
- 3.插入数据
- (1)executeUpdate,确定的参数用‘参数值’
- (2)executeUpdate,不确定的参数用?占位,后面的参数必须是oc对象,
- (3)executeUpdateWithFormat,不确定的参数用%@,%ld等格式占位,执行语句不区分大小写
- (4)参数是数组的使用方式,“”withArgumentsInArray:@[, , ,];
- 4.删除数据,删除指定行的数据,用?占位,后面的参数必须是oc对象,所以需要将ld包装成oc对象,@(idnum)
- 5.修改数据
- 6.查询数据
- (1)查询结果排序,降序desc,升序asc
- (2)条件查询,where
- (3)分页查询limit 从第几个开始取,取多少个
- (4)查询表中有多少条数据
- 7.删除表
项目中导入FMDB
1.手动添加,FMDB库文件。
2.添加编译参数,MRC和ARC混编。
FMDB的运行环境是在MRC下的,而大多数的项目是在ARC下的,项目Build Phases/complie Sources下,选择FMDB的几个.m文件,双击,添加编译参数,-fno-objc-arc.
如果不添加编译参数会出现如下错误:
添加后:
3.导入系统依赖库,libsqlite3.tbd
如图:
4.在需要的地方添加,FMDatabase.h
数据库的基本使用
1.新建数据库
_db=[FMDatabase databaseWithPath:_sqlName];//_sqlName是数据库的完整路径
if([_db open]){
NSLog(@“打开数据库成功”);
}else{
NSLog(@“打开数据库失败”);
}
2.新建表
NSString*sql=@“CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL, sex text NOT NULL)”;
BOOL result=[_db executeUpdate:sql];
if(result){
NSLog(@“创建表成功”);
}else{
NSLog(@“创建表失败”);
}
3.插入数据
(1)executeUpdate,确定的参数用‘参数值’
BOOL result=[_db executeUpdate:@“insert into t_student (name,age,sex) values (‘nn’,8,‘nan’)”];
(2)executeUpdate,不确定的参数用?占位,后面的参数必须是oc对象,
BOOL result=[_db executeUpdate:@“INSERT INTO t_student (id,name,age,sex) VALUES (?,?,?,?)”,@(mark),name,@(age),sex];
(3)executeUpdateWithFormat,不确定的参数用%@,%ld等格式占位,执行语句不区分大小写
BOOL result=[_db executeUpdateWithFormat:@“insert into t_student (name,age,sex) values (%@,%ld,%@)”,name,age,sex];
(4)参数是数组的使用方式,“”withArgumentsInArray:@[, , ,];
BOOL result=[_db executeUpdate:@"insert into t_student (name,age,sex) values(?,?,?)"withArgumentsInArray:@[name,@(age),sex]];
4.删除数据,删除指定行的数据,用?占位,后面的参数必须是oc对象,所以需要将ld包装成oc对象,@(idnum)
BOOL result=[_db executeUpdate:@“delete from t_student where id=?”,@(idnum)];
//BOOL result=[_db executeUpdate:@“delete from t_student”];//删除所有数据
5.修改数据
NSString *oldName=@“sanjin2”;
NSString *newOld=@“zhujiu”;
BOOL result=[_db executeUpdate:@“update t_student set name= ? where name=?”,newOld,oldName];
if(result){
NSLog(@“修改信息成功”);
}else{
NSLog(@“修改信息失败”);
}
}
6.查询数据
(1)查询结果排序,降序desc,升序asc
FMResultSet *resultSet =[_db executeQuery:@“select * from t_student s order by s.age desc”];
(2)条件查询,where
FMResultSet *resultSet=[_db executeQuery:@“select * from t_student where id<4”];
// FMResultSet *resultSet=[_db executeQuery:@“select * from t_student where id<=?”,@(3)];
(3)分页查询limit 从第几个开始取,取多少个
FMResultSet *resultSet =[_db executeQuery:@“select * from t_student limit 0,5”];
while ([resultSet next]) {
NSInteger mid= [resultSet intForColumn:@“id”];
NSString *name=[resultSet stringForColumn:@“name”];
NSInteger age=[resultSet intForColumn:@“age”];
NSString *sex=[resultSet stringForColumn:@“sex”];
NSLog(@“id=%ld,name=%@,age=%ld,sex=%@”,mid,name,age,sex);
}
(4)查询表中有多少条数据
int count = 0;
FMResultSet *resultSet=[_db executeQuery:@“select count( * ) from t_student”];
while ([resultSet next]) {
count=[resultSet intForColumnIndex:0];
}
NSLog(@"%d",count);
7.删除表
BOOL result=[_db executeUpdate:@“drop table if exists t_student”];