您的位置:首页 > 数据库 > Oracle

编写PRO*C程序实现对oracle数据库常用操作并导出文本文件

2013-03-13 10:06 465 查看
编写proc程序实现对account账户,trans_record交易记录进行相应操作。
account (id,name,balance)
trans_record (id, trans_date,record,balance)
添加新账户,查询,更新,查询一段时间内交易记录。
将trans_record内数据导出为文本文档。
#########################################################################
以下程序为本人初学oracle编写的第一个proc程序,有很多瑕疵,望见谅!
#########################################################################
修改:

进一步修改程序游标无法跳出的问题,以及导出为文本时无法将全部数据导出的问题。2013-13-14 15:17

EXEC SQL INCLUDE SQLCA.H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**************************************************************************
############### Main ##############
*************************************************************************/
int main(int argc, char **argv)
{
EXEC SQL BEGIN DECLARE SECTION;

char username[100];
char password[100];
char servername[100];
char sysdate[100];
int n = 0,m = 0,i = 0,z = 0,r = 0;
char bdate[10],edate[10];
char user_name[10];
int user_balance = 0;
char user_record[20];
char user_trans_date[10];
struct myaccount{
int id;
char name[10];
int balance;
};
struct myaccount myaccount;
struct record{
int id;
char date[10];
char rec[20];
int balance;
};
struct record myrecord;
EXEC SQL END DECLARE SECTION;

if (argc<4) {
printf("dbtest username passowrd servicename \n");
return (-1);
}

sprintf(username, argv[1]);
sprintf(password, argv[2]);
sprintf(servername, argv[3]);

EXEC SQL CONNECT :username IDENTIFIED BY :password USING :servername;
if(sqlca.sqlcode != 0) {
printf("connect database %s/%s@%s error \n", username, password, servername);
printf("sqlca.sqlcode=[%d]\n",sqlca.sqlcode); \
printf("sqlerr=[%s]\n",sqlca.sqlerrm.sqlerrmc); \
printf("filename=[%s]\n",__FILE__);\
printf("fileline=[%d]\n",__LINE__);\
fflush(stdout);
return -1;
}

/********************************************************************
create account&&trans_record.
******************************************************************/
EXEC SQL CREATE TABLE trans_record(id NUMBER ,trans_date DATE,
record VARCHAR2(50) ,balance NUMBER );
if (sqlca.sqlcode == 0) {
printf("create table trans_record ok \n");
}
else {
printf("create table trans_record FAILED \n");
}
EXEC SQL CREATE TABLE account(id NUMBER ,name VARCHAR2(20) ,balance NUMBER);
if (sqlca.sqlcode == 0) {
printf("create table account ok \n");
}
else {
printf("create table account FAILED \n");
}
/********************************************************************
insert into account&&trans_record
******************************************************************/
EXEC SQL INSERT INTO account VALUES(1,'xiaoming',3000);
EXEC SQL INSERT INTO account VALUES(2,'xiaohua',4000);
EXEC SQL INSERT INTO account VALUES(3,'xiaolei',5000);
EXEC SQL INSERT INTO account VALUES(4,'xiaozhan',6000);
if (sqlca.sqlcode == 0) {
printf("insert table account ok \n");
}
else {
printf("insert table account FAILED \n");
}
EXEC SQL INSERT INTO trans_record VALUES(2,to_date('2012-11-12','yyyy-mm-dd'),'expense1000',3000);
EXEC SQL INSERT INTO trans_record VALUES(1,to_date('2012-11-12','yyyy-mm-dd'),'expense1000',3000);
if (sqlca.sqlcode == 0) {
printf("insert table trans_record ok \n");
}
else {
printf("insert table  trans_record FAILED \n");
}

EXEC SQL COMMIT ;
/********************************************************************
Select account&&trans_record
******************************************************************/
printf("please input the number which you want select!\n");
scanf("%d",&n);
EXEC SQL DECLARE mycursor2 CURSOR FOR SELECT * FROM account \
WHERE id = :n;
if (sqlca.sqlcode == 0) {
printf("select account ok \n");
}
else {
printf("sqlca.sqlcode=[%d]\n",sqlca.sqlcode); \
printf("sqlerr=[%s]\n",sqlca.sqlerrm.sqlerrmc); \
printf("filename=[%s]\n",__FILE__);\
printf("fileline=[%d]\n",__LINE__);\
fflush(stdout);
}
EXEC SQL OPEN mycursor2;
EXEC SQL WHENEVER NOT FOUND DO break;
while(1){
EXEC SQL FETCH mycursor2 INTO :myaccount.id,:myaccount.name, \
:myaccount.balance;
printf("ID=%d,NAME=%s,BALANCE=%d\n",myaccount.id,myaccount.name, \
myaccount.balance);
}
EXEC SQL CLOSE mycursor2;

printf("Select record from trans_record!\n");
printf("please input the number which you want select!\n");
scanf("%d",&m);

printf("please input the begin date(example:20120101): \n");
for(i = 0; i < 9; i++)
scanf("%c",&bdate[i]);
bdate[9]='\0';
//	printf("%s\n",to_date(bdate,'yyyy-mm-dd'));
printf("please input the end  date(example:20121222): \n");
for(i = 0; i < 9; i++)
scanf("%c",&edate[i]);
edate[9]='\0';
//	printf("%s\n",to_date(edate,'yyyy-mm-dd'));

EXEC SQL DECLARE mycursor CURSOR FOR SELECT id,to_char(trans_date,'yyyy-mm-dd'),record,balance FROM trans_record WHERE id = :m  AND trans_date > to_date(:bdate,'yyyy-mm-dd') AND trans_date < to_date(:edate,'yyyy-mm-dd');
if (sqlca.sqlcode == 0) {
printf("select account ok .\n");
}
else {
printf("sqlca.sqlcode=[%d]\n",sqlca.sqlcode); \
printf("sqlerr=[%s]\n",sqlca.sqlerrm.sqlerrmc); \
printf("filename=[%s]\n",__FILE__);\
printf("fileline=[%d]\n",__LINE__);\
fflush(stdout);
}
EXEC SQL OPEN mycursor;
EXEC SQL WHENEVER NOT FOUND DO break;
while(1)
{
EXEC SQL FETCH mycursor INTO :myrecord.id,:myrecord.date, \
:myrecord.rec,:myrecord.balance;
printf("ID=%d,TRANS_DATE=%s,RECORD=%s,BALANCE=%d\n",myrecord.id,myrecord.date,myrecord.rec,myrecord.balance);
}
EXEC SQL CLOSE mycursor;

/*
EXEC SQL SELECT trans_date,record,balance INTO :user_trans_date,user_record,:user_balance FROM trans_record WHERE id = :m AND trans_date > to_date(:bdate,'yyyy-mm-dd') AND trans_date < to_date(:edate,'yyyy-mm-dd');

printf("ID=%d,TRANS_DATE=%s,RECORD=%s,BALANCE=%d\n",m,user_trans_date,user_record,user_balance);
if (sqlca.sqlcode == 0) {
printf("select account ok .\n");
}
else {
printf("sqlca.sqlcode=[%d]\n",sqlca.sqlcode); \
printf("sqlerr=[%s]\n",sqlca.sqlerrm.sqlerrmc); \
printf("filename=[%s]\n",__FILE__);\
printf("fileline=[%d]\n",__LINE__);\
fflush(stdout);
}
*/
/********************************************************************
Update account&&trans_record
******************************************************************/
/*
printf("please input the number which you want update!\n");
scanf("%d",&z);

EXEC SQL UPDATE account SET name = 'xxx' WHERE id = :z;
if (sqlca.sqlcode == 0) {
printf("update table account OK .\n");
}
else {
printf("update table account FAILED .\n");
}
*/
/********************************************************************
import&&export  trans_record
******************************************************************/
/********************* import ***************************/
FILE *fp = fopen("/home/oracle/recfile.txt", "w+t");
if (fp == NULL) {
perror("Open file recfile");
exit(1);
}
EXEC SQL DECLARE mycursor1 CURSOR FOR SELECT id,to_char(trans_date,'yyyy-mm-dd'),record,balance FROM trans_record;
if (sqlca.sqlcode == 0) {
printf("select trans_record for write is ok.\n");
}
else {
printf("sqlca.sqlcode=[%d]\n",sqlca.sqlcode); \
printf("sqlerr=[%s]\n",sqlca.sqlerrm.sqlerrmc); \
printf("filename=[%s]\n",__FILE__);\
printf("fileline=[%d]\n",__LINE__);\
fflush(stdout);
}
EXEC SQL OPEN mycursor1;
EXEC SQL WHENEVER NOT FOUND DO break;
while(1)
{
EXEC SQL FETCH mycursor1 INTO :myrecord.id,:myrecord.date, \
:myrecord.rec,:myrecord.balance;
r += fwrite(&myrecord, sizeof(struct record), 3, fp);
fwrite( "\n", 1, 1, fp );

}
fclose(fp);
EXEC SQL CLOSE mycursor1;
printf("write line : %d\n",r);

EXEC SQL TRUNCATE TABLE trans_record;
if (sqlca.sqlcode == 0) {
printf("truncate table trans_record ok \n");
}
else {
printf("truncate  table  trans_record FAILED \n");
}

/********************* export ***************************/
fp = fopen("/home/oracle/recfile.txt", "r+t");
if (fp == NULL) {
perror("Open file recfile");
exit(1);
}
i = 0;
while(i < r)
{
fread(&myrecord, sizeof(struct record), r, fp);
EXEC SQL INSERT INTO trans_record VALUES(:myrecord.id, \
to_date(:myrecord.date,'yyyy-mm-dd'),:myrecord.rec, \
:myrecord.balance);
if (sqlca.sqlcode == 0) {
printf("insert table trans_record ok \n");
}
else {
printf("insert table  trans_record FAILED \n");
}
printf("%d,%s,%s,%d\n",myrecord.id,myrecord.date,myrecord.rec,\
myrecord.balance);
i++;
}
fclose(fp);
EXEC SQL COMMIT;

/********************************************************************
Drop account&&trans_record
******************************************************************/
printf("check data and drop the table!\n");
int q;
scanf("%d",&q);

EXEC SQL DROP TABLE account;
if (sqlca.sqlcode == 0) {
printf("drop table account OK .\n");
}
else {
printf("drop table account FAILED .\n");
}

EXEC SQL DROP TABLE trans_record;
if (sqlca.sqlcode == 0) {
printf("drop table trans_record OK .\n");
}
else {
printf("drop table trans_record FAILED .\n");
}

EXEC SQL COMMIT WORK RELEASE;

return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐