当前位置: 代码迷 >> SQL >> Iphone联接sqlite3代码
  详细解决方案

Iphone联接sqlite3代码

热度:110   发布时间:2016-05-05 15:17:08.0
Iphone连接sqlite3代码

//
//? SqlConn.h
//? ButtonInterface
//
//? Created by chenwenhao on 10-7-26.
//? Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "sqlite3.h"
#define kFilename @"interface.sqlite3"

@interface SqlConn : NSObject {

}

-(NSString *)dataFilePath;
-(sqlite3 *)openSqllite;
-(void)closeSqllite:(sqlite3 *)database;
-(sqlite3_stmt *)getRs:(sqlite3 *)database sqlQuery:(NSString *)sql;
@end



//
//? SqlConn.m
//? ButtonInterface
//
//? Created by chenwenhao on 10-7-26.
//? Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SqlConn.h"


@implementation SqlConn

-(NSString *)dataFilePath {
//??? NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//??? NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
return [documentsDirectory stringByAppendingPathComponent:kFilename];

}

-(sqlite3 *)openSqllite {
sqlite3? *sqlite3database;
if (sqlite3_open([[self dataFilePath] UTF8String], &sqlite3database) != SQLITE_OK) {
sqlite3_close(sqlite3database);
NSAssert(0, @"Failed to open database");

}
return sqlite3database;

}

-(void)closeSqllite:(sqlite3 *)database {
sqlite3_close(database);
}

-(sqlite3_stmt *) getRs:(sqlite3 *)database sqlQuery:(NSString *)sql {
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, nil) != SQLITE_OK) {
NSAssert(0,@"Failed return database statement!");
}
return statement;
}

@end