知识屋:更实用的电脑技术知识网站
所在位置:首页 > 教育

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

发表时间:2022-03-25来源:网络

2019独角兽企业重金招聘Python工程师标准>>>

一、例子一(根据系统自带example1.cp改写)

teststu.cp

#include  /*建立通讯区域*/ 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 S CALL error_handler(); EXEC SQL WHENEVER S 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.s) { fprintf(stderr, "\n** ASE Error "); fprintf(stderr, "\n** %s", sqlca.sqlerrm.s); } 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

二、例子二:在esqlc中调用存储过程:

创建测试用存储过程test_proc.sql 。

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

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.cp

#define ERREXIT    -1 #define STDEXIT    0 #include  /* 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.s)     {         fprintf(stderr, "/n** ASE Error ");         fprintf(stderr, "/n** %s", sqlca.sqlerrm.s);     }     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 S CALL error_handler();     EXEC SQL WHENEVER S 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!


收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜