#include <stdio.h>
#include <string.h>
EXEC SQL INCLUDE sqLCa.H;
#define SQLCODE sqlca.sqlcode
EXEC SQL BEGIN DECLARE SECTION;
typedef struct
{
int id;
char name[120+1];
char email[300+1];
int age;
}TBL_TT;
TBL_TT tbltt;
EXEC SQL END DECLARE SECTION;
int ConnectDatabase()
{
EXEC SQL BEGIN DECLARE SECTION;
char userid[20];
char passwd[20];
char id[60];
EXEC SQL END DECLARE SECTION;
strcpy( userid,"love2t");
strcpy( passwd,"love2t");
memset( id , 0, sizeof(id ));
SQLCODE = 0 ;
sprintf( id, "%s/%s@oraclelinux", userid, passwd);
/*EXEC SQL CONNECT :userid IDENTIFIED BY :passwd;*/
EXEC SQL CONNECT :id ;
return( SQLCODE ) ;
}
int main()
{
ConnectDatabase();
if(SQLCODE)
{
printf("连接失败\n");
}else
{
printf("连接成功\n");
}
EXEC SQL BEGIN DECLARE SECTION;
char fld_name[120];
char fld_email[300];
int fld_age;
char sql_str[500];
EXEC SQL END DECLARE SECTION;
/*查询表*/
memset(sql_str,0,sizeof(sql_str));
strcpy(sql_str,"select name,email,age from tt");
EXEC SQL PREPARE qry_tmp FROM :sql_str;
//EXEC SQL DECLARE cur_name CURSOR FOR qry_tmp;
EXEC SQL DECLARE cur_name CURSOR FOR
select * from tt;
EXEC SQL OPEN cur_name;
#define SQLNOTFOUND 1403
while(1)
{
EXEC SQL FETCH cur_name into :fld_name,:fld_email,:fld_age ;
printf("sqlcode: %d\t",SQLCODE);
if(SQLCODE == SQLNOTFOUND)
break;
Trim(fld_name);
Trim(fld_email);
printf("%s\t%s\t%d\n",fld_name,fld_email,fld_age);
}
EXEC SQL CLOSE cur_name;
/* 插入表*/
tbltt.id=5;
strcpy(tbltt.name,"markeloff");
strcpy(tbltt.email,"markeloff@126.com");
tbltt.age=17;
EXEC SQL insert into TT(id,name,email,age) values(:tbltt);
printf("sqlcode: %d\n",SQLCODE);
EXEC SQL commit work;
/*更新表*/
EXEC SQL UPDATE TT set name='starix' where name='markeloff';
EXEC SQL commit work;
EXEC SQL insert into tt(id,name,email,age) values(6,'dd','dd',666);
EXEC SQL commit work;
EXEC SQL delete from tt where name='dd';
EXEC SQL commit work;
return 0;
}
Pro*C的一些基本操作
最新推荐文章于 2022-04-01 15:07:47 发布