1>添加sqlite3动态库:libsqlite3.dylib,CoreGraphics.framework,UIKit.framework,Foundation.framework
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
SQLite3在存储和检索大量的数据非常有效,结构化查询语言能够对数据进行复杂的聚合,与使用对象执行这些操作相比,获得的结果的速度更快。
假设app需要计算其中所有对象的特色字段的总和或者需要只符合特定条件的对象的总和,SQLite3可以不需要将所有对象加载到内存中就获取到这些信息。
SQLite3获取聚合比所有对象加载到内存,然后计算它们值的总和要快。
#import "ViewController.h"
#import <sqlite3.h>
@interface ViewController ()
@property(strong,nonatomic)IBOutletCollection(UITextField)NSArray *lineFields;
@end
@implementation ViewController
-(NSString *)dataFilePath{
//查找Document目录并在其后附加数据文件的文件名,这样就得到了数据文件的完整的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"data.sqlite"];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//打开数据库,如果在打开时遇到问题则关闭它并抛出一个断言错误
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String],&database)!= SQLITE_OK ){
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
//创建一个CREATE 表来保存我们的数据,通过IF NOT EXISTS 可以防止数据库覆盖现有数据,如果有相同名称 的表此命令会直接退出不执行任何操作
NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(ROW INTEGER PRIMARY KEY,FIELD_DATA TEXT);";
char *errorMsg;
if (sqlite3_exec (database,[createSQL UTF8String],NULL,NULL,&errorMsg)!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, @"Error creating table:%s",errorMsg);
}
//使用SELECT语句加载数据,请求所有行,告诉sqlite3按行号排序各行,以便我们以相同顺序获取它们
NSString *query = @"SELECT ROW,FIELD_DATA FROW FIELDS ORDER BY ROW";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statement,nil) == SQLITE_OK) {
//遍历返回的每一行
while (sqlite3_step(statement)== SQLITE_ROW) {
//抓取行号并将它存储在一个int变量中,然后抓取字段数据保存到C语言字符串中
int row = sqlite3_column_int(statement,0);
char *rowData = (char *)sqlite3_column_int(statement,1);
//利用数据库获取的值设置相应的字段
NSString *fileValue = [[NSString alloc]initWithUTF8String:rowData];
UITextField *theField = self.lineFields[row];
theField.text = fileValue;
}
//关闭数据库连接,操作到此结束
sqlite3_finalize(statement);
}
sqlite3_close(database);
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
}
//应用在终止运行或者进去后台之前保存数据
-(void)applicationWillResignActive:(NSNotification *)notification{
//再次打开数据库
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String],&database)!= SQLITE_OK ){
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
for (int i =0; i< 4; i++) {
UITextField *theField = self.lineFields[i];
//生成4条独立命令来更新数据库中的每一行
char *update = "INSERT OR REPLACE INTO FIELDS (ROW,FIELD_DATA)""VALUED(?,?);";
char *errorMsg = NULL;
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database,update,-1,&stmt,nil) == SQLITE_OK) {
sqlite3_bind_int(stmt,1,i);
sqlite3_bind_text(stmt,2,[theField.text UTF8String],-1,NULL);
}
//step语句执行更新。
if (sqlite3_step(stmt)!= SQLITE_DONE){
NSAssert(0, @"Error update table:%s",errorMsg);
sqlite3_finalize(stmt);
}
}
sqlite3_close(database);
}
@end