代码如下
#include <iostream>  
#include <sqlca.h>  
using namespace std;  
  
/* 声明SQL变量部分 */  
EXEC SQL BEGIN DECLARE SECTION;  
    char *uid = "scott/tiger@orcl";  
EXEC SQL END DECLARE SECTION;  
  
int main()  
{  
    EXEC SQL CONNECT :uid; /* 连接ORACLE 数据库 */  
    if(sqlca.sqlcode!=0)/* 连接成功 */  
        cout << sqlca.sqlerrm.sqlerrmc<<endl;  
    else  
        cout << "success!!!" <<endl;  
    return 0;  
}  但是一直提示 int EXEC:重定义,缺少;在标识符SQL前面等等错误,我不知道是咋回事

解决方案 »

  1.   

    先要注册一个sql error才行。
      // Register sql_error() as the error handler
      EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error:");
    void sql_error(char *msg)
    {
        EXEC SQL WHENEVER SQLERROR CONTINUE;
        cout << endl << msg << endl;
        cout << sqlca.sqlerrm.sqlerrmc << endl;
        EXEC SQL ROLLBACK RELEASE;
        exit(1);
    }然后再SQL Connect
      

  2.   

    给你一个完整的例子:/*  cppdemo1.pc
     *
     *  Prompts the user for an employee number, then queries the emp table for
     *  the employee's name, salary and commission.  Uses indicator variables (in
     *  an indicator struct) to determine if the commission is NULL.
     */#include <iostream.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>// Parse=partial by default when code=cpp so, preprocessor directives are
    // recognized and parsed fully.
    #define     UNAME_LEN      20
    #define     PWD_LEN        40// Declare section is required when CODE=CPP and/or PARSE={PARTIAL|NONE}
    EXEC SQL BEGIN DECLARE SECTION;
      VARCHAR username[UNAME_LEN];  // VARCHAR is an ORACLE supplied struct
      varchar password[PWD_LEN];    // varchar can be in lower case also  // Define a host structure for the output values of a SELECT statement
      struct empdat {
          VARCHAR   emp_name[UNAME_LEN];
          float     salary;
          float     commission;
      } emprec;  // Define an indicator struct to correspond to the host output struct
      struct empind {
          short     emp_name_ind;
          short     sal_ind;
          short     comm_ind;
      } emprec_ind;  // Input host variables
      int   emp_number;
      int   total_queried;
    EXEC SQL END DECLARE SECTION;// Define a C++ class object to match the desired struct from the above
    // declare section.
    class emp {
      char  ename[UNAME_LEN];
      float salary;
      float commission;
    public:
      // Define a constructor for this C++ object that takes ordinary C objects.
      emp(empdat&, empind&);
      friend ostream& operator<<(ostream&, emp&);
    };emp::emp(empdat& dat, empind& ind)
    {
      strncpy(ename, (char *)dat.emp_name.arr, dat.emp_name.len);
      ename[dat.emp_name.len] = '\0';
      this->salary = dat.salary;
      this->commission = (ind.comm_ind < 0) ? 0 : dat.commission;
    }ostream& operator<<(ostream& s, emp& e)
    {
      return s << e.ename << " earns " << e.salary << 
                  " plus " << e.commission << " commission." << endl << endl;
    }// Include the SQL Communications Area
    // You can use #include or EXEC SQL INCLUDE
    #include <sqlca.h>// Declare error handling function
    void sql_error(char *msg);void main()
    {
      char temp_char[32];  // Register sql_error() as the error handler
      EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error:");  // Connect to ORACLE.  Program will call sql_error() if an error occurs
      // when connecting to the default database.  Note the (char *) cast when
      // copying into the VARCHAR array buffer.
      username.len = (unsigned short)strlen(strcpy((char *)username.arr, "SCOTT"));
      password.len = (unsigned short)strlen(strcpy((char *)password.arr, "TIGER"));
      
      EXEC SQL CONNECT :username IDENTIFIED BY :password;  // Here again, note the (char *) cast when using VARCHARs
      cout << "\nConnected to ORACLE as user: "
           << (char *)username.arr << endl << endl;  // Loop, selecting individual employee's results
      total_queried = 0;
      while (1)
      {
          emp_number = 0;
          cout << "Enter employee number (0 to quit): " ;
          gets(temp_char);
          emp_number = atoi(temp_char);
          if (emp_number == 0)
            break;      // Branch to the notfound label when the 
          // 1403 ("No data found") condition occurs
          EXEC SQL WHENEVER NOT FOUND GOTO notfound;      EXEC SQL SELECT ename, sal, comm
              INTO :emprec INDICATOR :emprec_ind    // You can also use C++ style
              FROM EMP                              // comments in SQL statemtents.
              WHERE EMPNO = :emp_number;      {
            // Basic idea is to pass C objects to C++ constructors thus
            // creating equivalent C++ objects used in the usual C++ way
            emp e(emprec, emprec_ind);
            cout << e;
          }      total_queried++;
          continue;
    notfound:
          cout << "Not a valid employee number - try again." << endl << endl;
      } // end while(1)  cout << endl << "Total rows returned was " << total_queried << endl;
      cout << "Have a nice day!" << endl << endl;  // Disconnect from ORACLE
      EXEC SQL COMMIT WORK RELEASE;
      exit(0);
    }
    void sql_error(char *msg)
    {
        EXEC SQL WHENEVER SQLERROR CONTINUE;
        cout << endl << msg << endl;
        cout << sqlca.sqlerrm.sqlerrmc << endl;
        EXEC SQL ROLLBACK RELEASE;
        exit(1);
    }