标签:
1>添加sqlite3动态库:libsqlite3.dylib
2>导入头文件:#import<sqlite3.h>
3>利用C函数创建/打开数据库,编写SQL语句
自定义一个类DataBaseHandle继承NSObject
#import <Foundation/Foundation.h>
#import "Strategy.h"
@interface DataBaseHandle : NSObject
+(DataBaseHandle *)sharedInstance;
//打开数据库
-(void)openDB;
//关闭数据库
-(void)closeDB;
//插入新的数据
-(void)insertNewModel:(NSString *)name Data:(NSData *)data;
//删除某个保存的数据
-(void)deleteModelWithName:(NSString *)name;
//获取某个保存的数据
-(NSData *)selectModelWithName:(NSString *)name;
//获取所有的数据
-(NSArray *)selectAllModel;
//判断数据是否被保存
-(BOOL)isLoadModelWithName:(NSString *)name;
@end
下面是m文件的实现:
DataBaseHandle.m
#import "DataBaseHandle.h"
#import <sqlite3.h>
@implementation DataBaseHandle
static DataBaseHandle * handle = nil;
+(DataBaseHandle *)sharedInstance
{
if (handle == nil) {
handle = [[DataBaseHandle alloc] init];
}
return handle;
}
static sqlite3 *db=nil;
//打开数据库
-(void)openDB
{
if (db!=nil) {
return;
}
//存放数据库的路径和文件
NSString *path=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *filePath=[path stringByAppendingPathComponent:@"Strategy.sqlite"];
// NSLog(@"db filePath=%@",filePath);
//打开数据库
int result=sqlite3_open([filePath UTF8String], &db);
if (result==SQLITE_OK) {
// NSLog(@"打开数据库成功");
NSString * createActivitySql = @"CREATE TABLE StrategyList (NAME TEXT PRIMARY KEY , data BLOB)";
//执行sql语句
sqlite3_exec(db, [createActivitySql UTF8String], NULL, NULL, NULL);
}
}
//关闭数据库
-(void)closeDB
{
int result=sqlite3_close(db);
if (result==SQLITE_OK) {
// NSLog(@"关闭数据库成功");
db=nil;
}
}
//插入新的数据
-(void)insertNewModel:(NSString *)name Data:(NSData *)data
{
[self openDB];
sqlite3_stmt *stmt=nil;
NSString *sql= @"insert into StrategyList (NAME,data) values (?,?)";
//验证sql语句
int result=sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
if (result==SQLITE_OK) {
//绑定数据
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil);
sqlite3_bind_blob(stmt, 2, [data bytes], (int)[data length], nil);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
//删除某个保存的数据
-(void)deleteModelWithName:(NSString *)name
{
[self openDB];
sqlite3_stmt *stmt=nil;
NSString *sql= @"delete from StrategyList where NAME = ?";
//验证sql语句
int result=sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
if (result==SQLITE_OK) {
//绑定数据
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
//获取某个保存的数据
-(NSData *)selectModelWithName:(NSString *)name
{
[self openDB];
sqlite3_stmt * stmt = nil;
NSString * sql = @"select data from StrategyList where NAME = ?";
int result = sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL);
NSData * data = nil;
if (result == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, NULL);
if (sqlite3_step(stmt) == SQLITE_ROW) {
//bytes length
data = [NSData dataWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
}
}
sqlite3_finalize(stmt);
return data;
}
//获取所有的数据
-(NSArray *)selectAllModel
{
[self openDB];
sqlite3_stmt * stmt = nil;
NSString * sql = @"select NAME from StrategyList";
int result = sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL);
NSMutableArray * travelArray = [NSMutableArray array];
if (result == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
NSString * name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
[travelArray addObject:name];
}
}
sqlite3_finalize(stmt);
return travelArray;
}
//判断数据是否被保存
-(BOOL)isLoadModelWithName:(NSString *)name
{
NSData * data = [self selectModelWithName:name];
if (data == nil) {
return NO;
}
return YES;
}
@end
标签:
原文地址:http://www.cnblogs.com/linxiu-0925/p/5404419.html