三方库FMDatabase使用与SQLite类似,使用Objective-C 进行封装,使用更灵活。
1.引入FMDatabase库
#import "FMDatabase.h"
2.创建FMDatabase对象
@interface
{FMDatabase *fdb;}
@end
3.初始化对象

初始化路径,调用open 方法,打开或创建数据仓库,使用executeUpdate 创建表格,其参数为sql语句“create table if not exists user (userNumber text primary key,name text,age integer)” userNumber 为主键,即唯一,如使用相同userNumber,则会报error,text 为char*类型,integer 为整型。
4.修改操作
1)插入

使用executeUpdate 方法,传入NSString类型的sql语句,例如“insert into 表名 values(对应的表数据,且一定要按对应顺序)。
2)删除

sql语句为”delete from 表名 where 条件“
3)修改

sql语句”update 表名 set 修改的数据 where 条件“
5.查询

使用executeQuery方法,sql语句”select * from 表名“,星号代表全部,也可使用对应的列名。其中对应的字符串内容,使用stringForColumnIndex:(int)方法进行获取。列名可使用columnNameForIndex:(int)方法,其参数为对应的列下标,从0开始。
网友评论