很多程序都需要用到一些数据存储,简单的可以存在沙盒,有的会很麻烦,需要存数据库,这里简单介绍一下FMDB的使用。
首先需要下载FMDB的文件,然后扔到你的程序,先给一个下载地址。下载之后还需要配置一个库,添加libsqlite3.tbd
库到程序。
以上是最基本的配置,
下边写一些简单的数据库的创建以及增删改查。
在JXFMDBTool.h
中
#import
@interface JXFMDBTool : NSObject
// 根据一个数据库名字,在沙盒中创建并打开数据库
- (id)initWithName:(NSString *)name;
// 主动传入一个创建数据库的路径,创建并打开数据库
- (id)initWithPath:(NSString *)path;
// 根据键创建一个table
- (void)createTableWithTableName:(NSString *)name keys:(NSDictionary *)info;
//向table中添加数据
- (void)insertItemInTable:(NSString *)name withInfo:(NSDictionary *)info;
// 根据表名,返回所有的数据(array+dic)
- (NSArray *)selectFromTable:(NSString *)name;
// 根据某一项内容,降序返回所有的数据(array+dic)
- (NSArray *)selectFromTable:(NSString *)tableName withInfo:(NSString *)string;
// 根据某一项内容的值,降序返回所有的数据(array+dic)
/*
根据key = value 这一条件对string进行降序操作
*/
- (NSArray *)selectFromTable:(NSString *)tableName withKey:(NSString *)key andValue:(NSString *)value toInfo:(NSString *)string;
// 根据某两项内容的值,降序返回所有的数据(array+dic)
- (NSArray *)selectFromTable:(NSString *)tableName withOneKey:(NSString *)oneKey andOneValue:(NSString *)oneValue withTwoKey:(NSString *)twoKey andTwoValue:(NSString *)twoValue toInfo:(NSString *)string;
// 删除表中的某一项内容,根据键值(只能是string/number)
- (void)deleteItemInTable:(NSString *)name where:(NSString *)key is:(id)value;
/*
修改的
@param value 修改后
@param key 修改的值
@param index id 的下标
@param name 表名
*/
- (void)update:(id)value at:(id)key Index:(int)index inName:(NSString *)name;
// 关闭数据库
- (void)closeTheDB;
@end
在JXFMDBTool.m
中
#import "JXFMDBTool.h"
#import "FMDB.h"
@interface JXFMDBTool ()
@property (strong,nonatomic)FMDatabase *dataBase;
@end
@implementation JXFMDBTool
-(id)initWithName:(NSString *)name
{
if (self = [super init]) {
// 获取数据库路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pathFile = [NSString stringWithFormat:@"%@/%@",doc,name];
_dataBase = [[FMDatabase alloc] initWithPath:pathFile];
// 要操作数据库必须要把数据库打开
if ([self isFMDBOpen]) {
NSLog(@"打开数据库成功");
}else {
NSLog(@"打开数据库失败");
}
// [_dataBase close];// 不操作的时候就要把数据库关掉
}
return self;
}
-(id)initWithPath:(NSString *)path{
if (self = [super init]) {
_dataBase = [[FMDatabase alloc] initWithPath:path];
// 要操作数据库必须要把数据库打开
if ([self isFMDBOpen]) {
NSLog(@"打开数据库成功");
}else {
NSLog(@"打开数据库失败");
}
// [_dataBase close];// 不操作的时候就要把数据库关掉
}
return self;
}
//
-(void)createTableWithTableName:(NSString *)name keys:(NSDictionary *)info{
if (![self isFMDBOpen]){
return;
}
NSMutableString *create = [NSMutableString string];
[create appendString:@"create table if not exists "];
[create appendString:name];
[create appendString:@" ("];
[create appendString:@"id integer primary key autoincrement"];
for (NSString *key in info) {
[create appendString:@","];
[create appendString:key];
[create appendString:@" "];
[create appendString:info[key]];
}
[create appendString:@")"];
if ([_dataBase executeUpdate:create]) {
NSLog(@"创建table成功");
}else{
NSLog(@"创建失败");
}
}
- (void)insertItemInTable:(NSString *)name withInfo:(NSDictionary *)info{
if (![self isFMDBOpen]){
return;
}
// NSString *insert = @"insert into tableuu(username,password,email) values(?,?,?)";
NSMutableString *insert = [NSMutableString string];
[insert appendString:@"insert into "];
[insert appendString:name];
[insert appendString:@"("];
NSMutableString *tmp = [NSMutableString string];
NSMutableArray *tmpArr = [NSMutableArray array];
for (NSString *key in info) {
[insert appendString:key];
[insert appendString:@","];
[tmpArr addObject:info[key]];
[tmp appendString:@"?"];
[tmp appendString:@","];
}
NSString *a = [insert substringToIndex:[insert length] - 1];
NSString *b = [tmp substringToIndex:[tmp length] - 1];
NSString *insertString = [NSString stringWithFormat:@"%@) values(%@)",a,b];
// NSLog(@"%@",insertString);
[_dataBase executeUpdate:insertString withArgumentsInArray:tmpArr];
}
-(NSArray *)selectFromTable:(NSString *)name{
if (![self isFMDBOpen]){
return nil;
}
NSString *select = [NSString stringWithFormat:@"select * from %@",name];//全部遍历
FMResultSet *set = [_dataBase executeQuery:select];
NSMutableArray *array = [NSMutableArray array];
while ([set next]) {
[array addObject:[set resultDictionary]];
}
return array;
}
// 根据某一项内容,降序返回所有的数据(array+dic)
- (NSArray *)selectFromTable:(NSString *)tableName withInfo:(NSString *)string
{
if (![self isFMDBOpen]){
return nil;
}
NSString *select = [NSString stringWithFormat:@"select * from %@ order by %@ desc ",tableName,string];
FMResultSet *set = [_dataBase executeQuery:select];
NSMutableArray *array = [NSMutableArray array];
while ([set next]) {
[array addObject:[set resultDictionary]];
}
return array;
}
// 根据某一项内容的值,降序返回所有的数据(array+dic)
- (NSArray *)selectFromTable:(NSString *)tableName withKey:(NSString *)key andValue:(NSString *)value toInfo:(NSString *)string
{
if (![self isFMDBOpen]){
return nil;
}
NSString *select = [NSString stringWithFormat:@"select * from %@ where %@ = %@ order by %@ desc ",tableName,key,value,string];
FMResultSet *set = [_dataBase executeQuery:select];
NSMutableArray *array = [NSMutableArray array];
while ([set next]) {
[array addObject:[set resultDictionary]];
}
return array;
}
// 根据某两项内容的值,降序返回所有的数据(array+dic)
- (NSArray *)selectFromTable:(NSString *)tableName withOneKey:(NSString *)oneKey andOneValue:(NSString *)oneValue withTwoKey:(NSString *)twoKey andTwoValue:(NSString *)twoValue toInfo:(NSString *)string
{
if (![self isFMDBOpen]){
return nil;
}
NSString *select = [NSString stringWithFormat:@"select * from %@ where %@ = %@ and %@ = %@ order by %@ desc ",tableName, oneKey, oneValue, twoKey, twoValue,string];
FMResultSet *set = [_dataBase executeQuery:select];
NSMutableArray *array = [NSMutableArray array];
while ([set next]) {
[array addObject:[set resultDictionary]];
}
return array;
}
-(void)deleteItemInTable:(NSString *)name where:(NSString *)key is:(id)value{
if (![self isFMDBOpen]){
return;
}
NSString *delete = [NSString stringWithFormat:@"delete from %@ where %@ = %@",name,key,value];
[_dataBase executeUpdate:delete];
NSLog(@"删除成功");
}
- (void)update:(id)value at:(id)key Index:(int)index inName:(NSString *)name
{
if (![self isFMDBOpen]){
return;
}
NSString *update = [NSString stringWithFormat:@"update %@ set %@ = ? where id = %d",name,key,index];
if ([_dataBase executeUpdate:update,value]) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
}
- (void)closeTheDB
{
[_dataBase close];// 不操作的时候就要把数据库关掉
}
- (BOOL)isFMDBOpen
{
if ([_dataBase open]) {
return YES;
}
return NO;
}
@end
以上的JXFMDBTool
就是FMDB的简单操作的封装。
具体的实例,这里也给一个。如下:
在ViewController.m
中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 实例化
JXFMDBTool *dbTool = [[JXFMDBTool alloc] init];
// 创建表格
// [dbTool initWithPath:@"/Users/mac01/Desktop/FMDBText.db"];
[dbTool initWithName:@"FMDB.db"];
// 根据键创建一个table
[dbTool createTableWithTableName:@"text1" keys:@{@"username":@"varchar(128)",@"password":@"varchar(256)",@"email":@"varchar(256)"}];//char星型的需要字节数
//向table中添加数据
[dbTool insertItemInTable:@"text1" withInfo:@{@"username":@"yidong",@"password":@"yidongbudong",@"email":@"[email protected]"}];
// 删除表中的某一项内容,根据键值(只能是string/number)
// [dbTool deleteItemInTable:@"text1" where:@"id" is:@2];
/*
修改的
@param value 修改后
@param key 修改的值
@param index id 的下标text
@param name 表名
*/
// [dbTool update:@"hahahha" at:@"password" Index:3 inName:@"text1"];
// 根据表名,返回所有的数据(array+dic)
NSArray *array = [dbTool selectFromTable:@"text1"];
NSLog(@"array = %@",array);
// 获取数据库路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"doc = %@",doc);
}
这里再给一个打开DB的程序链接,能直观的看到我们创建的数据库表单内容——Navicat Premium
本篇文章只是简单的数据库操作, 没有涉及线程什么的,想要了解更多可以看看——iOS FMDB使用与缓存数据或者FMDB官方使用文档-GCD的使用-提高性能(翻译)