sybase嵌入式c/c++編程例子

1、例子一(根據系統自帶example1.cp改寫)sql

teststu.cpshell

#include <stdio.h>


/*創建通信區域*/
EXEC SQL INCLUDE SQLCA;

/* 
** These tokens must be declared in a declare section
** because they are used in declare sections below.
*/
EXEC SQL BEGIN DECLARE SECTION;
#define TYPESIZE 	13
#define	TIDSIZE 	6
EXEC SQL END DECLARE SECTION;

#define  EOLN	'\0'
/* 
** Forward declarations of the error and message handlers and
** other subroutines called from main().
*/
void	error_handler();
void	warning_handler();

int
main(int argc, char *argv[])
{
/*聲明宿主變量*/
	EXEC SQL BEGIN DECLARE SECTION;
	/* storage for login name and password. */
	char	username[30];
 	char 	sname[30];
	char	password[30];
	char 	server[30];
	EXEC SQL END DECLARE SECTION;
/*錯誤處理*/
	EXEC SQL WHENEVER SQLERROR CALL error_handler();
	EXEC SQL WHENEVER SQLWARNING CALL warning_handler();
	EXEC SQL WHENEVER NOT FOUND CONTINUE;
	
/*鏈接到 SQL SERVER 服務器*/

	strcpy(username, "mymotif");
	strcpy(password, "wxwpxh");
	strcpy(server, "MYMOTIFVOSTRO145480");

	EXEC SQL CONNECT :username IDENTIFIED BY :password using :server;
	
/*執行查詢,並顯示結果*/
	EXEC SQL USE testdb;
	EXEC SQL DECLARE c1 CURSOR FOR  
            SELECT SNAME FROM STUDENT;
 	EXEC SQL OPEN c1;  
 	printf("name in table student\n");
   	do {
      		EXEC SQL FETCH c1 INTO :sname;  
      		if (sqlca.sqlcode != 0) break;
     		printf( "student name = %s\n", sname );
   	} while ( 1 );
 	EXEC SQL CLOSE c1;

	return(0);
}

/*錯誤處理程序*/
/*
** void error_handler()
** 
**	Displays error codes and numbers from the SQLCA and exits with
**	an ERREXIT status. 
*/
void 
error_handler(void)
{
	fprintf(stderr, "\n** SQLCODE=(%ld)", sqlca.sqlcode);

	if (sqlca.sqlerrm.sqlerrml)
	{
		fprintf(stderr, "\n** ASE Error ");
		fprintf(stderr, "\n** %s", sqlca.sqlerrm.sqlerrmc);
	}

	fprintf(stderr, "\n\n");

	exit(-1);
}

/*
** void warning_handler()
** 
**	Displays warning messages.
*/
void 
warning_handler(void)
{

	if (sqlca.sqlwarn[1] == 'W')
	{
		fprintf(stderr, 
			"\n** Data truncated.\n");
	}

	if (sqlca.sqlwarn[3] == 'W')
	{
		fprintf(stderr, 
			"\n** Insufficient host variables to store results.\n");
	}	
	return;
}

編譯:數據庫

$cpre64 -m teststu.cp
$ cc -m64  -g -DSYB_LP64 -I. -I/opt/sybase/OCS-16_0/include  teststu.c /opt/sybase/OCS-16_0/include/sybesql.c -L/opt/sybase/OCS-16_0/lib  -lsybct64 -lsybtcl64 -lsybcs64 -lsybcomn64 -lsybintl64 -lsybunic64  -rdynamic -ldl -lnsl -lm   -o teststu

執行:服務器

$ ./teststu 
name in table student
student name = 馬志元                    
student name = 馬元                       
student name = 王海濱                    
student name = 金力標                    
student name = 馬小樂                    
student name = 馬娟

數據庫testdb及表STUDENT的建立見博文測試

http://my.oschina.net/u/2245781/blog/620710 spa

2、例子二:在esqlc中調用存儲過程:.net

建立測試用存儲過程test_proc.sql 。code

$ isql -SMYMOTIFVOSTRO145480 -Umymotif -Pwxwpxh -i test_proc.sqlserver

use testdb
go

if exists ( select 1 from sysobjects where name = 'test_proc' )
drop proc test_proc
go

if exists ( select 1 from sysobjects where name = 't123')
drop table t123
go

create table t123(id int primary key, col2 varchar(32) not null)
insert into t123 values(1, 'iihero')
insert into t123 values(2, 'Sybase')
insert into t123 values(3, 'ASE')
go

create proc test_proc (@id_min int, @num_t123 int output) with recompile
as
select @num_t123 = count( a.id ) from t123 a where a.id > = @id_min
go 

setuser
go

declare @num_t123 int
exec test_proc 1, @num_t123 output

select @num_t123
go

代碼test.cpblog

#define ERREXIT    -1
#define STDEXIT    0

#include <stdio.h>

/* Declare the SQLCA. */
EXEC SQL INCLUDE SQLCA;

EXEC SQL BEGIN DECLARE SECTION;
#define TYPESIZE     13
#define    TIDSIZE     6
EXEC SQL END DECLARE SECTION;

#define  EOLN    '/0'

void error_handler(void)
{
    fprintf(stderr, "/n** SQLCODE=(%ld)", sqlca.sqlcode);

    if (sqlca.sqlerrm.sqlerrml)
    {
        fprintf(stderr, "/n** ASE Error ");
        fprintf(stderr, "/n** %s", sqlca.sqlerrm.sqlerrmc);
    }

    fprintf(stderr, "/n/n");

    exit(ERREXIT);
}

void warning_handler(void)
{

    if (sqlca.sqlwarn[1] == 'W')
    {
        fprintf(stderr, 
            "/n** Data truncated./n");
    }

    if (sqlca.sqlwarn[3] == 'W')
    {
        fprintf(stderr, 
            "/n** Insufficient host variables to store results./n");
    }    
    return;
}

int main()
{
    EXEC SQL BEGIN DECLARE SECTION;
    /* storage for login name and password. */
    char    username[30];
    char    password[30];
    char    server[30];
    EXEC SQL END DECLARE SECTION;
    
    EXEC SQL BEGIN DECLARE SECTION;
    CS_INT id_min;
    CS_INT num_t123;
    CS_SMALLINT retcode;
    EXEC SQL END DECLARE SECTION;    
    
    EXEC SQL WHENEVER SQLERROR CALL error_handler();
    EXEC SQL WHENEVER SQLWARNING CALL warning_handler();
    EXEC SQL WHENEVER NOT FOUND CONTINUE;    
    
    strcpy(username, "mymotif");
    strcpy(password, "wxwpxh");
    strcpy(server, "MYMOTIFVOSTRO145480");
    
    EXEC SQL CONNECT :username IDENTIFIED BY :password using :server;
    EXEC SQL USE testdb;
    
    printf("Begin test!\n");
    
    id_min = 0;
    exec sql exec :retcode = test_proc :id_min, :num_t123 output;
    
    printf("num_t123=%d\n", num_t123);
    

    
    printf("End test!\n");
    
    return STDEXIT;
}

運行:

$ ./test 
Begin test!
num_t123=3
End test!
相關文章
相關標籤/搜索