ITPub博客

首页 > Linux操作系统 > Linux操作系统 > linux下gcc编译C访问oracle

linux下gcc编译C访问oracle

原创 Linux操作系统 作者:gengzhizhen 时间:2009-09-02 10:04:07 0 删除 编辑
t1.h内容:
#ifndef __EXAMPLES_H
#define __EXAMPLES_H
#include "sqlora.h"

#undef __P
#if (defined(PROTOTYPES) || defined(__STDC__) || defined(__cplusplus) )
# define __P(protos) protos
#else
# define __P(protos) ()
#endif

/**
 * ex3.c
 */
int update_manager __P((sqlo_db_handle_t dbh));

/**
 * ex8.c
 */
int update_emp __P((sqlo_db_handle_t dbh, double factor, const char * job));

/**
 * ex9.c
 */
int call_plsql __P((sqlo_db_handle_t dbh));

#define error_exit(_dbh, _msg);
#endif



t1.c内容:
#include
#include
#include "t1.h"
int update_manager(sqlo_db_handle_t dbh)
{
  const char * argv[2];
  int stat;

  argv[0] = "0.5";
  argv[1] = "MANAGER";

  stat = sqlo_run(dbh, "UPDATE EMP SET SAL = SAL * :1 WHERE JOB = :2",
                 2, argv);
  if (0 > stat) {
    error_exit(dbh, "sqlo_run");
  }
  return stat;
}
/* $Id: ex3.c 221 2002-08-24 12:54:47Z kpoitschke $ */

int update_emp(sqlo_db_handle_t dbh, double factor, const char * job)
{
  int stat;
  char stmt[1024];

  sprintf(stmt, "UPDATE EMP SET SAL = SAL * %f WHERE JOB = '%s'",
          factor, job);

  if ( 0 > (stat = sqlo_exec(dbh, stmt)))
    error_exit(dbh, "sqlo_run");

  return stat;                  /* number of processed rows */
}

int call_plsql(sqlo_db_handle_t dbh)
{
  sqlo_stmt_handle_t sth = SQLO_STH_INIT;

  /* the stored procecdure call */
  char * stmt =
    "BEGIN\n"
    "    delt;\n"
    "END;\n";
  if ( 0 <= (sth = sqlo_prepare(dbh, stmt))) {
      /* execute the call */
      if (SQLO_SUCCESS != sqlo_execute(sth, 1))
        error_exit(dbh, "sqlo_execute");


    if (SQLO_SUCCESS != sqlo_close(sth))
      error_exit(dbh, "sqlo_execute");

  } else {
    error_exit(dbh, "sqlo_prepare");
  }
  return 1;
}

test.c内容:
#include
#include
#include "sqlora.h"
#include "t1.h"
static CONST char * _defuser="scott/tiger@mydata";

int main(int argc,char **argv)
{
sqlo_db_handle_t dbh;
CONST char * cstr = _defuser;
char server_version[100];
/*int stat;*/
if (SQLO_SUCCESS!= sqlo_init(SQLO_OFF,1,100))
{
printf("Failed to init libsqlora8\n");
return EXIT_FAILURE;
}
if (SQLO_SUCCESS != sqlo_connect(&dbh,cstr))
{
printf("Cannot login with %s \n",cstr);
return EXIT_FAILURE;
}
if (SQLO_SUCCESS != sqlo_server_version(dbh,server_version,sizeof(server_version)))
{
printf("Failed to get the server version: %s\n", sqlo_geterror(dbh));
return EXIT_FAILURE;
}
printf("Connected to:\n%s\n\n", server_version);
[oracle@oracle9idemo examples]$ cat test.c
#include
#include
#include "sqlora.h"
#include "t1.h"
static CONST char * _defuser="scott/tiger@mydata";

int main(int argc,char **argv)
{
sqlo_db_handle_t dbh;
CONST char * cstr = _defuser;
char server_version[100];
/*int stat;*/
if (SQLO_SUCCESS!= sqlo_init(SQLO_OFF,1,100))
{
printf("Failed to init libsqlora8\n");
return EXIT_FAILURE;
}
if (SQLO_SUCCESS != sqlo_connect(&dbh,cstr))
{
printf("Cannot login with %s \n",cstr);
return EXIT_FAILURE;
}
if (SQLO_SUCCESS != sqlo_server_version(dbh,server_version,sizeof(server_version)))
{
printf("Failed to get the server version: %s\n", sqlo_geterror(dbh));
return EXIT_FAILURE;
}
printf("Connected to:\n%s\n\n", server_version);
/* ex3.c */
/*stat = update_manager(dbh);*/
/*printf("Reduced the salary of %d managers\n", stat);*/
/* ex8.c */
/*  stat = update_emp(dbh, 2.0, "MANAGER");*/
/*  printf("Doubled the salary of %d managers.\n", stat);*/
/*  sqlo_commit(dbh);*/

/* logout */
call_plsql(dbh);
  sqlo_finish(dbh);

  return EXIT_SUCCESS;
}


对上面的文件编译:
[oracle@oracle9idemo examples]$ gcc -Wall -c t1.c
[oracle@oracle9idemo examples]$ gcc -Wall -c test.c
[oracle@oracle9idemo examples]$ gcc -lsqlora8 test.o t1.o -o test
[oracle@oracle9idemo examples]$ ./test

OK了

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15142212/viewspace-613769/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-02-09

  • 博文量
    119
  • 访问量
    203627