iOS系统下的Oracle数据库环境(ios oracle)

在移动应用开发中,数据库是一个十分重要的组成部分。在iOS系统下,使用Oracle数据库环境可以帮助我们实现更加高效、可靠的数据存储。本文将介绍如何在iOS系统下配置Oracle数据库环境,以及使用Objective-C语言连接和操作数据库。

一、配置Oracle数据库环境

1.下载Oracle Instant Client

在Oracle官网上下载适合iOS系统的Oracle Instant Client,解压后将包含以下文件和文件夹:

– instantclient-basiclite-macos.x64-19.10.0.0.0dbru.zip

– instantclient-sdk-macos.x64-19.10.0.0.0dbru.zip

– sqlplus-macos.x64-19.10.0.0.0dbru.zip

2.将Oracle Instant Client复制到iOS项目中

将Oracle Instant Client中的所有文件复制到iOS项目中。可以选择将文件夹直接放在项目根目录下,或者将其放在项目的Frameworks目录下。

3.添加头文件和库文件

在iOS项目中的Build Phases选项卡中,将以下头文件和库文件添加到项目中:

– Header Search Paths: $(PROJECT_DIR)/instantclient_19_10/

– Other Linker Flags: -lclntsh -lodbc

4.配置环境变量

在iOS项目的AppDelegate.m文件中,添加以下代码来配置环境变量:

#import “AppDelegate.h”

#import

@interface AppDelegate ()

@end

@implementation AppDelegate

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

setenv(“DYLD_LIBRARY_PATH”, [[[NSBundle mnBundle] resourcePath] stringByAppendingPathComponent:@”/instantclient_19_10″].UTF8String, 1);

return YES;

}

@end

二、连接Oracle数据库

1.创建数据库连接

在iOS项目中创建一个新的Objective-C类,命名为OracleDatabaseManager。在该类中,创建一个connection属性,用于存储数据库连接对象。

#import

@interface OracleDatabaseManager : NSObject

@property(nonatomic, strong) OCIEnv *env;

@property(nonatomic, strong) OCIError *error;

@property(nonatomic, strong) OCISvcCtx *svcCtx;

@property(nonatomic, strong) OCIStmt *stmt;

@property(nonatomic, strong) OCIDefine *define;

@property(nonatomic, strong) OCIBind *bind;

@property(nonatomic, assign) OCIParam *param;

@property(nonatomic, strong) OCILobLocator *lobLocator;

@property(nonatomic, strong) OCIInterval *interval;

@property(nonatomic, strong) OCIObject *object;

@property(nonatomic, strong) OCIRef *ref;

@property(nonatomic, strong) OCIArray *array;

@property(nonatomic, strong) NSString *username;

@property(nonatomic, strong) NSString *password;

@property(nonatomic, strong) NSString *host;

@property(nonatomic, strong) NSString *port;

@property(nonatomic, strong) NSString *serviceName;

@property(nonatomic, assign) boolean isLoginSuccess;

@property(nonatomic, strong) NSString *result;

-(id)initWithUserName:(NSString*)username password:(NSString*)password host:(NSString*)host port:(NSString*)port serviceName:(NSString*)serviceName;

– (void)login;

@end

2.实现数据库连接方法

在OracleDatabaseManager.m文件中,实现login方法,用于连接到Oracle数据库。

#import “OracleDatabaseManager.h”

@implementation OracleDatabaseManager

-(id)initWithUserName:(NSString *)username password:(NSString *)password host:(NSString *)host port:(NSString *)port serviceName:(NSString *)serviceName {

self = [super init];

if (self) {

self.username = username;

self.password = password;

self.host = host;

self.port = port;

self.serviceName = serviceName;

}

return self;

}

– (void)login {

self.isLoginSuccess = NO;

sword status = OCIEnvCreate(&_env, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);

if(status != OCI_SUCCESS) {

self.result = [NSString stringWithFormat:@”OCIEnvCreate fled, status: %d”, status];

return;

}

status = OCIHandleAlloc(_env, (dvoid **)&_error, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);

if(status != OCI_SUCCESS) {

self.result = [NSString stringWithFormat:@”OCIHandleAlloc fled, status: %d”, status];

return;

}

status = OCIHandleAlloc(_env, (dvoid **)&_svcCtx, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0);

if(status != OCI_SUCCESS) {

self.result = [NSString stringWithFormat:@”OCIHandleAlloc fled, status: %d”, status];

return;

}

OCIServerAttach(_svcCtx, _error, [self.host UTF8String], (sb4)[self.host length], OCI_DEFAULT);

OCILogon2(_svcCtx, _error, &_svcCtx, [self.username UTF8String], (ub4)[self.username length], [self.password UTF8String], (ub4)[self.password length], [self.serviceName UTF8String], (ub4)[self.serviceName length], OCI_DEFAULT);

if(OCI_SUCCESS == status) {

self.isLoginSuccess = YES;

} else {

self.isLoginSuccess = NO;

self.result = [NSString stringWithFormat:@”OCILogon2 fled, status: %d”, status];

}

}

@end

三、操作Oracle数据库

1.执行SQL语句

在OracleDatabaseManager.m文件中,实现execute方法,用于执行SQL语句。

– (void)execute:(NSString*)sql {

NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];

NSString *sqlTrim = [sql stringByTrimmingCharactersInSet:set];

sword status = OCIHandleAlloc(_env, (dvoid **)&_stmt, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0);

OCIStmtPrepare(_stmt, _error, (OraText*)[sqlTrim UTF8String], (ub4)[sqlTrim length], OCI_NTV_SYNTAX, OCI_DEFAULT);

if(!OCIStmtExecute(_svcCtx, _stmt, _error, 1, 0, NULL, NULL, OCI_STMT_SCROLLABLE_READONLY)) {

self.result = [NSString stringWithFormat:@”OCIStmtExecute fled, status: %d”, status];

}

int columnCount = 0;

OCIAttrGet(_stmt, OCI_HTYPE_STMT, &columnCount, 0, OCI_ATTR_PARAM_COUNT, _error);

for (int i = 0; i

OCIParam *param;

NSString *columnName = @””;

ub2 columnNameLen = 0;

OCIAttrGet(_stmt, OCI_HTYPE_STMT, &param, 0, OCI_ATTR_PARAM, _error);

OCIAttrGet(param, OCI_DTYPE_PARAM, &columnName, &columnNameLen, OCI_ATTR_NAME, _error);

NSLog(@”columnName: %@, columnNameLen: %d”,columnName,columnNameLen);

}

OCIHandleFree(_stmt, OCI_HTYPE_STMT);

}

2.查询数据

在OracleDatabaseManager.m文件中,实现select方法,用于查询数据。

– (NSMutableArray*)select:(NSString*)sql {

NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];

NSString *sqlTrim = [sql stringByTrimmingCharactersInSet:set];

sword status = OCIHandleAlloc(_env, (dvoid **)&_stmt, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0);

OCIStmtPrepare(_stmt, _error, (OraText*)[sqlTrim UTF8String], (ub4)[sqlTrim length], OCI_NTV_SYNTAX, OCI_DEFAULT);

if(!OCIStmtExecute(_svcCtx, _stmt, _error, 1, 0, NULL, NULL, OCI_STMT_SCROLLABLE_READONLY)) {

self.result = [NSString stringWithFormat:@”OCIStmtExecute fled, status: %d”, status];

}

NSMutableArray *dataArray = [[NSMutableArray alloc] init];

int columnCount = 0;

OCIAttrGet(_stmt, OCI_HTYPE_STMT, &columnCount, 0, OCI_ATTR_PARAM_COUNT, _error);

while (OCIStmtFetch(_stmt, _error, 1, OCI_FETCH_NEXT, OCI_DEFAULT) == OCI_SUCCESS) {

NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] init];

for (int i = 0; i

char* columnName = NULL;

ub2 columnNameLen = 0;

ub2 dataType = 0;

void *value = NULL;

int valueSize = 0;

OCIParam *param;

OCIStmtGetPieceInfo(_stmt,_error,&columnCount,&dataType,&value,&valueSize,OCI_HTYPE_STMT,OCI_DEFAULT);

OCIAttrGet(_stmt, OCI_HTYPE_STMT, &param, -1, OCI_ATTR_PARAM, _error);

OCIAttrGet(param, OCI_DTYPE_PARAM, &columnName, &columnNameLen, OCI_ATTR_NAME, _error);

if(columnName != NULL) {

char *p = (char *)value;

NSString *valueStr = @””;

if(p != NULL) {

valueStr = [NSString stringWithCString:p encoding:NSUTF8StringEncoding];


数据运维技术 » iOS系统下的Oracle数据库环境(ios oracle)