FMDB的使用方法
1.FMDB是iOS中存储比较大的数据方法,是sqlite的封装类,很好用
2.可以去Githud上面下面FMDB的封装类
3.FMDB 的使用一些步骤:
<1>将下载好的文件导入到项目工程中,然后添加sqlite3.0的库
<2>新建一个Mydbhepler 类,用于数据的操作:(使用单例模式)
<3>在Mydbhepler.h的文件:
#import <Foundation/Foundation.h>
#import "Message.h"
#import "SocketValue.h"
#import "Goods.h"
#import "Contact.h"
@interface MyDBHelper : NSObject
+(MyDBHelper *) getInstance;
-(void) createDatabaseAndTable ;
-(void) addMessage : (Message *) msg;
-(NSMutableArray *) getAllMessages;
-(void) deleteAllMesssages;
-(void) addSocketValue :(SocketValue *) sv;
-(NSMutableArray *)getAllSocketValues;
-(void) updateSocketValue:(int) identify : (int) serverID;
-(void) deleteAllSocketValue;
-(void) deleteSocketValueById:(int) identify;
-(NSMutableArray *) getAllCustomStatus;
-(void) addCustomStatus:(NSString *)address;
-(void) deleteCustomStatus:(NSString *) identify;
-(void) deleteAllStatuses;
<4>在Mydbhelper.m 的文件的实现
#import "MyDBHelper.h"
#import "FMDatabase.h"
#import "StatusItem.h"
#import "SocketValue.h"
#import "Contact.h"
@implementation MyDBHelper
static MyDBHelper *helper = nil;
+(MyDBHelper *) getInstance{
@synchronized(self)
{
if (helper == nil)
{ helper = [[self alloc] init];
}
}
return helper;
}
//创建数据库
-(void) createDatabaseAndTable{
NSString *home = NSHomeDirectory();
NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/ise.db", home];
NSLog(@"dbPath:%@" ,dbPath);
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if(![db open]){
NSLog(@"could not open");
}
[db executeUpdate:@"create table if not exists message (id integer primary key autoincrement , address text, datestr text , event integer , url text , textmsg text , direction integer)"];
[db executeUpdate:@"create table if not exists status ( id integer primary key autoincrement , address text)"];
[db executeUpdate:@"create table if not exists socket ( id integer primary key autoincrement , socketid text , socketvalue text , type text , socketdate text, max2min text, serverID integer )"];
[db executeUpdate:@"create table if not exists cart (id integer primary key autoincrement , goodsid text,count integer)"];
[db executeUpdate:@"create table if not exists contact (id integer primary key autoincrement , name text ,cellphone text,address text ,zipcode text)"];
[db close];
}
-(void) deleteAllSocketValue{
NSString *home = NSHomeDirectory();
NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/ise.db", home];
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if(![db open]){
NSLog(@"could not open");
}
[db executeUpdate:@"delete from socket"];
[db close];
}
-(void) deleteSocketValueById:(int)identify{
NSString *home = NSHomeDirectory();
NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/ise.db", home];
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if(![db open]){
NSLog(@"could not open");
}
NSString *sql = [NSString stringWithFormat:@"delete from socket where id = %d" , identify];
NSLog(@"sql = %@" ,sql);
[db executeUpdate:sql];
[db close];
}
-(void) addSocketValue :(SocketValue *) sv{
NSString *home = NSHomeDirectory();
NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/ise.db", home];
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if(![db open]){
NSLog(@"could not open");
}
NSString *sql = [NSString stringWithFormat:@"insert into socket values(null , \'%@\' , \'%@\' , \'%@\', \'%@\' , \'%@\' , %d )" , sv.socketid , sv.socketvalue , sv.type , sv.socketdate , sv.max2min , sv.serverID ];
NSLog(@"insert sql = %@", sql);
[db executeUpdate:sql];
[db close];
}
-(NSMutableArray *)getAllSocketValues{
NSString *home = NSHomeDirectory();
NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/ise.db", home];
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if(![db open]){
NSLog(@"could not open");
}
NSMutableArray *array = [[NSMutableArray alloc] init];
FMResultSet *rs= [db executeQuery:@"select * from socket"];
while ([rs next]){
SocketValue *sv = [[SocketValue alloc]init];
sv.identify = [rs intForColumn:@"id"];
sv.socketid = [rs stringForColumn:@"socketid"];
sv.socketvalue = [rs stringForColumn:@"socketvalue"];
sv.type = [rs stringForColumn:@"type"];
sv.socketdate = [rs stringForColumn:@"socketdate"];
sv.max2min = [rs stringForColumn:@"max2min"];
sv.serverID = [rs intForColumn:@"serverID"];
[array addObject: sv];
}
[rs close];
return array;
}