本文主要是介绍Oracle10G--PROC编程篇(Linux),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
PROC编程相信在Linux下工作的朋友应该不陌生吧!下面就来具体介绍些Proc编程相关的东西,此文相对简单,请高手们绕行。PROC编程篇
(此文仅供Oracle初学者参考只用)
/**********************************************
** 文章名称:Oracle10G--PROC编程篇(Linux)
** 版权信息:可以任意转载,但要注明出处及作者信息
***********************************************/
一、简单取得系统时间
#include
#include
#include
EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE oraca;
main( int argc, char *argv[] )
{
EXEC SQL BEGIN DECLARE SECTION;
varchar vcBuffer[32];
EXEC SQL END DECLARE SECTION;
/* Connect To Oracle Datebaase */
if(OU_ConnectDatabase("weigw","weigw") != 0)
{
printf("Connect Database failed!\n");
return -1;
}
memset(vcBuffer.arr, 0x00, sizeof(vcBuffer.arr));
EXEC SQL select sysdate into :vcBuffer from dual;
printf("SYSDATE:\n", vcBuffer.arr);
EXEC SQL COMMIT WORK RELEASE;
return 0;
}
/*
**
** 功能说明:连接数据库
** 输入参数:
** 输入参数:
** 输出参数:
** 返回值说明:0-成功 其他-失败
*/
int OU_ConnectDatabase(char *con_name,char *con_passwd)
{
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[20];
VARCHAR pwd[20];
EXEC SQL END DECLARE SECTION;
strcpy(uid.arr,con_name); /*strcpy(uid.arr,"weigw");*/
uid.len=strlen(uid.arr);
strcpy(pwd.arr,con_passwd); /*strcpy(pwd.arr,"weigw");*/
pwd.len=strlen(pwd.arr);
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd ;
return sqlca.sqlcode;
}
二、多库用户操作(排他锁)
#include
#include
#include
EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE oraca;
main( int argc, char *argv[] )
{
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR vcBuffer[32];
int ID;
const char *dbname1 = "dbname1";
const char *dbname2 = "dbname2";
EXEC SQL END DECLARE SECTION;
/* Connect To Oracle Datebaase dbname1 */
if(OU_ConnectDatabase("weigw","weigw","orcl","dbname1") != 0)
{
printf("Connect Database failed!\n");
return -1;
}
/* Connect To Oracle Datebaase dbname2 */
if(OU_ConnectDatabase("weigw","weigw","orcl","dbname2") != 0)
{
printf("Connect Database dbname 2 failed!\n");
return -1;
}
ID = 0;
memset(vcBuffer.arr, 0x00, sizeof(vcBuffer.arr));
vcBuffer.len = 0;
EXEC SQL AT :dbname1 SELECT COUNT(*) INTO :ID FROM WEIGWTEST;
ID += 1;
strncpy(vcBuffer.arr, "dbname1", sizeof(vcBuffer.arr) - 1);
vcBuffer.len = strlen(vcBuffer.arr);
EXEC SQL AT :dbname1 INSERT INTO WEIGWTEST VALUES(:ID, :vcBuffer);
EXEC SQL AT :dbname2 INSERT INTO WEIGWTEST VALUES(:ID, :vcBuffer);
EXEC SQL AT :dbname1 COMMIT;
EXEC SQL AT :dbname1 ROLLBACK;
EXEC SQL COMMIT WORK RELEASE;
return 0;
}
/*
**
** 功能说明:连接数据库
** 输入参数:
** 输入参数:
** 输出参数:
** 返回值说明:0-成功 其他-失败
*/
int
OU_ConnectDatabase(
char *con_name,
char *con_passwd,
char *con_string,
char *con_dbname
) {
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[20];
VARCHAR pwd[20];
VARCHAR sid[20];
VARCHAR dbname[20];
EXEC SQL END DECLARE SECTION;
memset(uid.arr, 0x00, sizeof(uid.arr));
uid.len = 0;
memset(pwd.arr, 0x00, sizeof(pwd.arr));
pwd.len = 0;
memset(sid.arr, 0x00, sizeof(sid.arr));
sid.len = 0;
memset(dbname.arr, 0x00, sizeof(dbname.arr));
dbname.len = 0;
strcpy(uid.arr,con_name);
uid.len=strlen(uid.arr);
strcpy(pwd.arr,con_passwd);
pwd.len=strlen(pwd.arr);
strcpy(sid.arr,con_string);
sid.len=strlen(sid.arr);
strcpy(dbname.arr,con_dbname);
dbname.len=strlen(dbname.arr);
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd AT :dbname USING :dbname;
return sqlca.sqlcode;
}
这篇关于Oracle10G--PROC编程篇(Linux)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!