2012年2月13日星期一
Pro*C 第一次练习
***********************************
proc test.pc
gcc -o test test.c $ORACLE_HOME/lib/libclntsh.so
./test
************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE ORACA;
EXEC SQL INCLUDE SQLDA;
EXEC SQL INCLUDE SQLCPR;
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR usr[20], pass[20], server[20];
EXEC SQL END DECLARE SECTION;
void
update()
{
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[20];
char job[20];
EXEC SQL END DECLARE SECTION;
char slID[8];
printf("\nplease input the employee number\n");
gets(slID);
id=atoi(slID);
printf("\nplease input the employee name\n");
gets(name);
printf("\nplease input the employee job\n");
gets(job);
printf("\nthe id is %d, the name is %s, the job is %s",id, name, job);
EXEC SQL UPDATE emp
SET emp_name=:name, emp_job=:job
WHERE emp_id=:id;
printf("\n\nemployee %d update.\n",id);
EXEC SQL COMMIT;
return;
}
void
delete(void)
{
EXEC SQL BEGIN DECLARE SECTION;
int id;
EXEC SQL END DECLARE SECTION;
char clID[8];
printf("\nplease input the id\n");
gets(clID);
id = atoi(clID);
EXEC SQL DELETE
FROM EMP
WHERE emp_id=:id;
printf("\nemployee %d is deleted",id);
EXEC SQL COMMIT;
return;
}
void
insert(void)
{
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[20];
char job[20];
EXEC SQL END DECLARE SECTION;
char slID[8];
printf("\nplease input the employee number\n");
gets(slID);
id=atoi(slID);
printf("\nplease input the employee name\n");
gets(name);
printf("\nplease input the employee job\n");
gets(job);
printf("\nthe name is %s, the job is %s",name, job);
EXEC SQL INSERT INTO EMP(emp_id,emp_name,emp_job)
VALUES(:id,:name,:job);
printf("\nemployee %d inserted\n",id);
EXEC SQL COMMIT;
return;
}
void
Print(char *s)
{
printf("%s\n",s);
}
void
selection(void)
{
char slID[8];
int ilID;
printf("\nplease input the employee number\n");
gets(slID);
ilID=atoi(slID);
printf("ID=%d\n",ilID);
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[20];
char job[20];
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT emp_id,emp_name,emp_job
INTO :id,name,job
FROM EMP
WHERE emp_id=:ilID;
printf("\n+++the empid = %d\n",id);
printf("\n+++the empname = %s\n",name);
printf("\n+++the empjob = %s\n",job);
}
int
oracle_connect(void)
{
strcpy(usr.arr, "ceda");
usr.len=(unsigned short)strlen((char *)usr.arr);
strcpy(pass.arr, "ceda");
pass.len=(unsigned short)strlen((char *)pass.arr);
//strcpy(server.arr, "satsvm");
//server.len=(unsigned short)strlen((char *)server);
EXEC SQL CONNECT :usr IDENTIFIED BY :pass;
EXEC ORACLE OPTION (ORACA=YES);
oraca.oradbgf=1;
oraca.oracchf=1;
oraca.orastxtf=3;
printf("%d\n",sqlca.sqlcode);
if(sqlca.sqlcode == 0)
{
Print("db connection is ok");
}
else
{
Print("db connection is error");
}
return 0;
}
static int oracle_disconnect(void)
{
EXEC SQL commit work release;
Print("db is disconnected");
}
int
main()
{
char clchoice[5];
//char choice;
oracle_connect();
while(1)
{
printf("\n************");
printf("\n* 1.Select *");
printf("\n* 2.Update *");
printf("\n* 3.Insert *");
printf("\n* 4.Delect *");
printf("\n* 5.Exit *");
printf("\n************");
printf("\nEnter choice: \n");
gets(clchoice);
//choice = getchar();
//switch(choice)
switch(clchoice[0])
{
case '1':
selection();
break;
case '2':
update();
break;
case '3':
insert();
break;
case '4':
delete();
break;
case '5':
Print("exit");
break;
default:
printf("\n\ninvalid choice\n");
break;
}
//if(choice == '5')
if(clchoice[0] == '5')
{
break;
}
}
oracle_disconnect();
return 0;
}
2012年2月1日星期三
<转载>inet_pton和inet_ntop函数
Linux下这2个IP地址转换函数,可以在将IP地址在“点分十进制”和“整数”之间转换
而且,inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6。算是比较新的函数了。
inet_pton函数原型如下[将“点分十进制” -> “整数”]
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
这个函数转换字符串到网络地址,第一个参数af是地址族,转换后存在dst中
inet_pton 是inet_addr的扩展,支持的多地址族有下列:
af = AF_INET
src为指向字符型的地址,即ASCII的地址的首地址(ddd.ddd.ddd.ddd格式的),函数将该地址
转换为in_addr的结构体,并复制在*dst中
af =AF_INET6
src为指向IPV6的地址,,函数将该地址
转换为in6_addr的结构体,并复制在*dst中
如果函数出错将返回一个负值,并将errno设置为EAFNOSUPPORT,如果参数af指定的地址族和src格式不对,函数将返回0。
inet_ntop函数原型如下[将“点分十进制” -> “整数”]
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
这个函数转换网络二进制结构到ASCII类型的地址,参数的作用和上面相同,只是多了一个参数socklen_t cnt,他是所指向缓存区dst的大小,避免溢出,如果缓存区太小无法存储地址的值,则返回一个空指针,并将errno置为ENOSPC
下面是例程
char IPdotdec[20]; //存放点分十进制IP地址
struct in_addr s; // IPv4地址结构体
int main (void)
{
// 输入IP地址
printf("Please input IP address: ");
scanf("%s", &IPdotdec);
// 转换
inet_pton(AF_INET, IPdotdec, (void *)&s);
printf("inet_pton: 0x%x\n", s.s_addr); // 注意得到的字节序
// 反转换
inet_ntop(AF_INET, (void *)&s, IPdotdec, 16);
printf("inet_ntop: %s\n", IPdotdec);
}
订阅:
博文 (Atom)