Pro*C的一些基本操作

#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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值