- 论坛徽章:
- 0
|
如何在纯c中嵌入SQL查询
[quote]原帖由 "snowolf_37"]is #include<oci.h>; ?[/quote 发表:
NO
Give you a Eg.
/*
* filename:model.pc
* author:K.Ro
* inc:Hitachi,Tokyo
* create time:2005.3.24
* update time:
* desc:it is a model file for write pro*c programe for oracle+tuxedo
*/
#include <stdio.h>;
#include <string.h>;
#include <stdlib.h>;
#include <sqlda.h>;
#include <sqlcpr.h>;
#include <atmi.h>;
#include <fml.h>;
#include <userlog.h>;
/* set pro*c operation */
exec sql include sqlca;
exec sql include oraca;
exec oracle option (oraca=yes);
/* Define constants for VARCHAR lengths. */
#define UNAME_LEN 20
#define PWD_LEN 40
/* Declare variables. No declare section is
needed if MODE=ORACLE. */
VARCHAR username[UNAME_LEN]; /* VARCHAR is an Oracle-supplied struct */
varchar password[PWD_LEN]; /* varchar can be in lower case also. */
/* Declare error handling function. */
void sql_error(msg)
char *msg;
{
char err_msg[128];
size_t buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("\n%s\n", msg);
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s\n", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(EXIT_FAILURE);
}
void main()
{
/* Connect to ORACLE--
* Copy the username into the VARCHAR.
*/
strncpy((char *) username.arr, "sgac_dec", UNAME_LEN);
/* Set the length component of the VARCHAR. */
username.len =
(unsigned short) strlen((char *) username.arr);
/* Copy the password. */
strncpy((char *) password.arr, "dbwork", PWD_LEN);
password.len =
(unsigned short) strlen((char *) password.arr);
/* Register sql_error() as the error handler. */
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n" ;
/* Connect to ORACLE. Program will call sql_error()
* if an error occurs when connecting to the default database.
*/
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("\nConnected to ORACLE as user: %s\n", username.arr);
/* Disconnect from ORACLE. */
EXEC SQL ROLLBACK WORK RELEASE;
exit(EXIT_SUCCESS);
} |
|