您的位置:首页 > 数据库

iOS-sqlite3&FMDB使用代码示范

2015-12-03 15:03 288 查看
数据库操作是我们使用十分频繁的一份操作,在iOS中如何使用数据库,使用什么数据库,是我们不得不考虑的一个问题。

小型数据我们可以使用plist文件,或者NSUserDefaults存储。数据量比较多得情况下,我们可以使用sqlite或者Core Data.

在此先介绍一下sqlite的系统API,然后介绍一下第三方库FMDB,使用第三方库比使用系统的sqlite简单方便。

对数据库的操作,我们可以简单理解为增删改查,下面的具体直接使用代码实现增删改查,不一具体介绍。

*********************************************************
**  DatabaseTool
**

#import <Foundation/Foundation.h>
#import "FMDB.h"
@interface DatabaseTool : NSObject
+(FMDatabase *)shareDatabase;
+(BOOL)close;
@end

#import "DatabaseTool.h"
static FMDatabase *_db = nil;
@implementation DatabaseTool
+(FMDatabase *)shareDatabase
{
if (_db == nil)
{
_db = [[FMDatabase alloc]initWithPath:[self getFilePath]];
}
[self open];
return _db;
}
+(NSString *)getFilePath
{
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentPath stringByAppendingPathComponent:@"file.db"];
return path;
}
+(BOOL)open
{
if ([_db open] == NO)
{
[_db close];
NSAssert(NO, @"数据库打开失败");
}
//设置数据库缓存机制
[_db setShouldCacheStatements:YES];
return YES;
}
+(BOOL)close
{
if ([_db close] == NO)
{
NSAssert(NO, @"数据库关闭失败");
}
return YES;
}
@end
*************************************************************
**   Contact
**
#import <Foundation/Foundation.h>
//modal 模型
@interface Contact : NSObject
{
int _cid;
NSString *_name;
NSString *_phone;
}
@property(nonatomic,assign) int cid;
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *phone;
//自定义初始化方法
-(id)initWithID:(int)ID name:(NSString *)aName phone:(NSString *)aPhone;
@end

#import "Contact.h"

@implementation Contact
@synthesize cid = _cid;
@synthesize name = _name;
@synthesize phone = _phone;
-(id)initWithID:(int)ID name:(NSString *)aName phone:(NSString *)aPhone
{
if (self = [super init])
{
self.cid = ID;
self.name = aName;
self.phone = aPhone;
}
return self;
}
-(void)dealloc
{
[_name release];
[_phone release];
[super dealloc];
}
@end
*************************************************************
**  ContactDAO
**
#import <Foundation/Foundation.h>
#import "DatabaseTool.h"
//连接  数据模型和数据库对象 主要完成表的创建,增删改查得功能
@interface ContactDAO : NSObject
+(void)createContactTable;
+(void)insertData;
+(NSMutableArray *)queryData;
+(void)updateDataWithID:(int)cid;
+(void)deleteDataWithID:(int)cid;
@end

#import "ContactDAO.h"
#import "Contact.h"
@implementation ContactDAO
+(void)createContactTable
{
FMDatabase *database = [DatabaseTool shareDatabase];
if ([database tableExists:@"contact"] == NO)
{
[database executeUpdate:@"create table contact(id integer primary key autoincrement not null,name text,phone text)"];
}
[DatabaseTool close];
}
+(void)insertData
{
FMDatabase *database = [DatabaseTool shareDatabase];
[database executeUpdate:@"insert into contact(name,phone)values(?,?)",@"wyg",@"1992"];
[DatabaseTool close];
}
+(NSMutableArray *)queryData
{
NSMutableArray *array = [[NSMutableArray alloc]init];
FMDatabase *database = [DatabaseTool shareDatabase];
FMResultSet *set = [database executeQuery:@"select *from contact"];
while ([set next])
{
int cid = [set intForColumn:@"id"];
NSString *name = [set stringForColumn:@"name"];
NSString *phone = [set stringForColumn:@"phone"];
Contact *c = [[Contact alloc]initWithID:cid name:name phone:phone];
[array addObject:c];
[c release];
}
[set close];
[DatabaseTool close];
return array;
}
+(void)updateDataWithID:(int)cid
{
NSNumber *num = [NSNumber numberWithInt:cid];
FMDatabase *database = [DatabaseTool shareDatabase];
[database executeUpdate:@"update contact set name = 'www' where id = ?",num];
[DatabaseTool close];
}
+(void)deleteDataWithID:(int)cid
{
NSNumber *num = [NSNumber numberWithInt:cid];
FMDatabase *database = [DatabaseTool shareDatabase];
[database executeUpdate:@"delete from contact where id = ?",num];
[DatabaseTool close];
}
@end


FMDB
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: