IOS之数据库的查找,删除,添加,更新

来源:未知 责任编辑:责任编辑 发表时间:2013-12-22 14:54 点击:

DB类之.h文件
#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface DB : NSObject
+(sqlite3 *)openDB;//打开数据库
-(void)closeDB;//关闭数据库

@end

DB类之.m文件
#import "DB.h"
#import <sqlite3.h>
static sqlite3 *db = nil;
@implementation DB
+(sqlite3 *)openDB
{
    if(db)
    {
        return db;
    }
    //目标路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES)objectAtIndex:0];
    //原始路径
    NSString *filePath = [docPath stringByAppendingPathComponent:@"db.sqlite"];
   
    NSFileManager *fm = [NSFileManager defaultManager];
   
    if ([fm fileExistsAtPath:filePath] == NO)//如果doc下没有数据库,从bundle里面拷贝过来
 
    {
        NSString *bundle = [[NSBundle mainBundle]pathForResource:@"classDB" ofType:@"sqlite"];
       
        NSError *err = nil;
       
        if ([fm copyItemAtPath:bundle toPath:filePath error:&err] == NO) //如果拷贝失败
        {
            NSLog(@"%@",[err localizedDescription]);
        }
       
       
    }
    sqlite3_open([filePath UTF8String], &db);
    return db;  
}
-(void)closeDB
{
    if (db)
    {
        sqlite3_close(db);  
    }
   
}
@end
Person类.h文件
#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(nonatomic,retain)NSString *name,*phone;
@property(nonatomic,assign)int age,ID;

-(id)initWithName:(NSString *)name phone:(NSString *)phone age:(int)age ID:(int)ID;

+(NSMutableArray *)findAll;
+(int)count;
+(Person *)findByID:(int)ID;
+(NSMutableArray *)findByname:(NSString *)name;
+(void)addName:(NSString *)name  phone:(NSString *)phone age:(int)age;
+(void)deleteByID:(int)ID;
+(void)updataName:(NSString *)name phone:(NSString *)phone age:(int)age forID:(int)ID;
@end
Person类.m文件
#import "Person.h"
#import "DB.h"

@implementation Person

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Lexical or Preprocessor Issue 'xxx.h
  • ios学习笔记(二)xcode 4.3.2下实现基本交互
  • ios版本的helloworld
  • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
  • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
  • IOS类似iphone通讯录TableView的完整demo【附源码】
  • UITableView一些方法
  • [iPhone中级]iPhone团购信息客户端的开发 (二)
  • 如何为Iphone应用创建启动界面
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1