📄 example.pc
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sqlca.h>
/*#include <sqlcpr.h>*/
#include <sqlda.h>
void connect();
void disconnect();
void sql_error(char*);
void select();
void insert();
void delete();
extern sqlglm(char *,int *,int *);
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
void main()
{
EXEC SQL WHENEVER SQLERROR do sql_error("Oracle错误--\n");
connect();
/* select();*/
insert();
/* delete();*/
disconnect();
}
void connect()
{
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR username[10],password[10],server[10];
EXEC SQL END DECLARE SECTION;
printf("\n Please enter the name of the user:");
gets(username.arr);
username.len=(unsigned short)strlen((char*)username.arr);
printf("\n Please enter the password:");
gets(password.arr);
password.len=(unsigned short)strlen((char*)password.arr);
printf("\n Please enter the name of the server:");
gets(server.arr);
server.len=(unsigned short)strlen((char*)server.arr);
EXEC SQL CONNECT : username IDENTIFIED BY :password using :server;
printf("\n %s connect %s successfully ! \n",username.arr,server.arr);
}
void disconnect()
{
char temp;
printf("\n disconnect? (Y/N)");
scanf("%c",&temp);
fflush(stdin);
if(temp!='Y'&&temp!='y')
{
EXEC SQL ROLLBACK WORK RELEASE;
printf("\n Donn't commit the order and disconnect !\n\n");
}
else
{
EXEC SQL COMMIT WORK RELEASE;
printf("\n Commit the order and disconnect !\n\n");
exit(1);
}
}
void select()
{
EXEC SQL BEGIN DECLARE SECTION;
char custname[200],phone[20];
/* int custno;*/
EXEC SQL END DECLARE SECTION;
printf("\n Please input the name of the customer: ");
gets(custname);
EXEC SQL SELECT phone INTO :phone FROM customer_info WHERE custname=:custname;
printf("\n The phone number of %s is %s !",custname,phone);
}
void insert()
{
EXEC SQL BEGIN DECLARE SECTION;
int custno;
char custname[200],phone[20];
EXEC SQL END DECLARE SECTION;
/* time_t t1 = time(NULL);
char *begin_time="";g
begin_time=ctime(&t1);
printf("\n The begin time is %s \n",begin_time);*/
clock_t begin,end;
printf( " \n To insert 10000 lines cost ");
begin=clock();
for(custno=1;custno<10000;custno++)
{
sprintf(custname, "%d", custno) ;
sprintf(phone, "%d", custno) ;
EXEC SQL INSERT INTO customer_info(custno, custname,phone)
VALUES(:custno,:custname,:phone);
}
end=clock();
printf( " %lf seconds! \n", (double)(end-begin)/CLOCKS_PER_SEC);
/* system("pause");*/
if(custno==10000)
printf("\nInsert successfully !\n");
else
printf("\nInsert unsuccessfully !\n");
}
void sql_error(char *msg)
{
char err_msg[128];
size_t buf_len,msg_len;
EXEC SQL WHENEVER SQLERROR continue;
printf("\n%s\n",msg);
buf_len=sizeof(err_msg);
sqlglm(err_msg,&buf_len,&msg_len);
printf("%.*s", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE ;
exit(EXIT_FAILURE);
}
void delete()
{
clock_t begin,end;
printf( " \n To delete 10000 lines cost ");
begin=clock();
EXEC SQL delete from customer_info;
end=clock();
printf( " %lf seconds! \n", (double)(end-begin)/CLOCKS_PER_SEC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -