Home » Developer & Programmer » Precompilers, OCI & OCCI » Pro*C/C++: EXEC SQL CONNECT TO "database" problem (Windows XP)
Pro*C/C++: EXEC SQL CONNECT TO "database" problem [message #279057] Tue, 06 November 2007 14:48 Go to next message
boludes
Messages: 7
Registered: October 2007
Location: Argentina
Junior Member
Hello everybody, i need your help please, becouse, i have a problem with my Pro*C/C++, i don't know how to use the sintax
EXEC SQL CONNECT TO "database";

The questions are:

Where I should pu it?
How can i use, 'couse when i write EXEC SQL CONNECT TO "DB001";
the follow error display:

**
Syntax error at line 77, column 21, file D:\oracle\ora92\precomp\demo\proc\sample1\sample1c.pc:
Error at line 77, column 21 in file D:\oracle\ora92\precomp\demo\proc\sample1\sa
mple1c.pc

EXEC SQL CONNECT TO :db_name USER :scott USING :tiger
....................1
PCC-S-02201, Encountered the symbol "TO" when expecting one of the following:

:

Error at line 0, column 0 in file D:\oracle\ora92\precomp\demo\proc\sample1\samp
le1c.pc
PCC-F-02102, Fatal error while doing C preprocessing
**

Thank you so much!
Good Luck.

[RENAMED by LF: the original title was "Problem from Argentina"]

[Updated on: Thu, 08 November 2007 05:57] by Moderator

Report message to a moderator

Re: Problem from Argentina [message #279135 is a reply to message #279057] Wed, 07 November 2007 01:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Invalid syntax.

Pro*C/C++ Programmer's Guide
Chapter 3 Database Concepts
Section Connect to the Database

Regards
Michel
Re: Problem from Argentina [message #279309 is a reply to message #279057] Wed, 07 November 2007 13:53 Go to previous messageGo to next message
boludes
Messages: 7
Registered: October 2007
Location: Argentina
Junior Member
Thank you Michel Cadot, but i have a problem with this data, i can't understand that... can you or someone help me?
Thanks.
Regards!
Re: Problem from Argentina [message #279392 is a reply to message #279309] Thu, 08 November 2007 01:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
What don't you understand? The CONNECT statement syntax?

Regards
Michel
Re: Pro*C/C++: EXEC SQL CONNECT TO "database" problem [message #279478 is a reply to message #279057] Thu, 08 November 2007 12:43 Go to previous messageGo to next message
boludes
Messages: 7
Registered: October 2007
Location: Argentina
Junior Member
I don't understand the syntax CONNECT, how I make a connection to ONE ESPECIFIC DATABASE. My problem is, I have two (2) DATABASE created, now, I, with Pro*C/C++ precompiler, and I want to make a connection, for example, to DBTESTL (name of the DATABASE), and I have another, DATABASE (DB47 the other DATABASE) and in the two (2) DATABASE I have two tables, EMP and DEPT, (Is an example of Oracle sample1.pc) Now, When I compile the proyect, in Pro*C/C++ precompiler, and when I compile with VisualC++, the program take the DB47 but, I want to connect the .pc to DBTESTL (database 1), and I don't know how can I connect to DBTESTL. How can I make the connection? Which command I can use? How to use the commend.... Thank you. Smile
icon9.gif  Re: Pro*C/C++: EXEC SQL CONNECT TO "database" problem [message #279479 is a reply to message #279057] Thu, 08 November 2007 12:51 Go to previous messageGo to next message
boludes
Messages: 7
Registered: October 2007
Location: Argentina
Junior Member
This is the code, I make modification in he.


/*
* sample1.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 <stdio.h>
#include <string.h>


/* 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. */
/*
Define a host structure for the output values of a SELECT statement.
*/
struct {
VARCHAR emp_name[UNAME_LEN];
float salary;
float commission;
} emprec;
/*
Define an indicator struct to correspond to the host output struct. */
struct
{
short emp_name_ind;
short sal_ind;
short comm_ind;
} emprec_ind;

/* Input host variable. */
int emp_number;
int total_queried;
/* Include the SQL Communications Area.
You can use #include or EXEC SQL INCLUDE. */
#include <sqlca.h>

/* Declare error handling function. */
void sql_error();

main()
{
char temp_char[32];

/* Connect to ORACLE--
* Copy the username into the VARCHAR.
*/
strncpy((char *) username.arr, "SCOTT", UNAME_LEN);
/* Set the length component of the VARCHAR. */
username.len = strlen((char *) username.arr);
/* Copy the password. */
strncpy((char *) password.arr, "TIGER", PWD_LEN);
password.len = 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 DECLARE DBTESTL DATABASE;
This is correct?

EXEC SQL CONNECT :username IDENTIFIED BY :password
AT DBTESTL;This?

printf("\nConnected to ORACLE as user: %s\n", username.arr);
/* Loop, selecting individual employee's results */
total_queried = 0;
for (;;)
{
/* Break out of the inner loop when a
* 1403 ("No data found") condition occurs.
*/
EXEC SQL WHENEVER NOT FOUND DO break;
for (;;)
{
emp_number = 0;
printf("\nEnter employee number (0 to quit): ");
gets(temp_char);
emp_number = atoi(temp_char);
if (emp_number == 0)
break;
EXEC SQL SELECT ename, sal, NVL(comm, 0)
INTO :emprec INDICATOR :emprec_ind
FROM EMP
WHERE EMPNO = :emp_number;
/* Print data. */
printf("\n\nEmployee\tSalary\t\tCommission\n");
printf("--------\t------\t\t----------\n");
/* Null-terminate the output string data. */
emprec.emp_name.arr[emprec.emp_name.len] = '\0';
printf("%-8s\t%6.2f\t\t",
emprec.emp_name.arr, emprec.salary);
if (emprec_ind.comm_ind == -1)
printf("NULL\n");
else
printf("%6.2f\n", emprec.commission);

total_queried++;
} /* end inner for (;;) */
if (emp_number == 0) break;
printf("\nNot a valid employee number - try again.\n");
} /* end outer for(;;) */

printf("\n\nTotal rows returned was %d.\n", total_queried);
printf("\nG'day.\n\n\n");

/* Disconnect from ORACLE. */
EXEC SQL COMMIT WORK RELEASE;
exit(0);
}
void sql_error(msg)
char *msg;
{
char err_msg[128];
int 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(1);
}

Did you see any mistakes? Any modification to make?
Thank You Michel Cadot. Surprised
Re: Pro*C/C++: EXEC SQL CONNECT TO "database" problem [message #279567 is a reply to message #279479] Fri, 09 November 2007 01:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Did you see there is many ways to format you post?
Do we have to scan all your code to find CONNECT?
You have to help us help you.
Please read OraFAQ Forum Guide.

What happens now when you compile?
It seems correct but I'm not a compiler.

Regards
Michel

icon9.gif  Re: Pro*C/C++: EXEC SQL CONNECT TO "database" problem [message #279710 is a reply to message #279057] Fri, 09 November 2007 12:49 Go to previous messageGo to next message
boludes
Messages: 7
Registered: October 2007
Location: Argentina
Junior Member
...
char db_string[20] = "SCOTT/TIGER@DBTESTL";
...
EXEC SQL DECLARE DBTESTL DATABASE;

EXEC SQL CONNECT :username IDENTIFIED BY :password
AT DBTESTL USING :db_string;

Is that correct?

When I run the program, this problem apear.

How can I detect the problem in "SQLNET.ORA".

***********************************************
ORACLE error--

ORA-12154: TNS:could not resolve service name

Press any key to continue
***********************************************


When I write this code:
----------------------

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
EXEC SQL DECLARE DBTESTL DATABASE;

EXEC SQL CONNECT :username IDENTIFIED BY :password
AT DBTESTL;
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Apear:

Connected to ORACLE as user: SCOTT

Enter employee number (0 to quit):

When i write the employee numbre, for example 2367
apear:
__________________________________________________

Connected to ORACLE as user: SCOTT

Enter employee number (0 to quit): 2367

ORACLE error--

ORA-01012: not logged on
Press any key to continue
____________________________________________________
But, why tell me that it is connected, if not?

("Connected to ORACLE as user: SCOTT")

Thanks.
Re: Pro*C/C++: EXEC SQL CONNECT TO "database" problem [message #279782 is a reply to message #279710] Sat, 10 November 2007 03:57 Go to previous message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
It seems correct and the error comes from your client configuration:
ORA-12154: TNS:could not resolve the connect identifier specified
 *Cause:  A connection to a database or other service was requested using
 a connect identifier, and the connect identifier specified could not
 be resolved into a connect descriptor using one of the naming methods
 configured. For example, if the type of connect identifier used was a
 net service name then the net service name could not be found in a
 naming method repository, or the repository could not be
 located or reached.
 *Action:
   - If you are using local naming (TNSNAMES.ORA file):
      - Make sure that "TNSNAMES" is listed as one of the values of the
        NAMES.DIRECTORY_PATH parameter in the Oracle Net profile
        (SQLNET.ORA)
      - Verify that a TNSNAMES.ORA file exists and is in the proper
        directory and is accessible.
      - Check that the net service name used as the connect identifier
        exists in the TNSNAMES.ORA file.
      - Make sure there are no syntax errors anywhere in the TNSNAMES.ORA
        file.  Look for unmatched parentheses or stray characters. Errors
        in a TNSNAMES.ORA file may make it unusable.
   - If you are using directory naming:
      - Verify that "LDAP" is listed as one of the values of the
        NAMES.DIRETORY_PATH parameter in the Oracle Net profile
        (SQLNET.ORA).
      - Verify that the LDAP directory server is up and that it is
        accessible.
      - Verify that the net service name or database name used as the
        connect identifier is configured in the directory.
      - Verify that the default context being used is correct by
        specifying a fully qualified net service name or a full LDAP DN
        as the connect identifier
   - If you are using easy connect naming:
      - Verify that "EZCONNECT" is listed as one of the values of the
        NAMES.DIRETORY_PATH parameter in the Oracle Net profile
        (SQLNET.ORA).
      - Make sure the host, port and service name specified
        are correct.
      - Try enclosing the connect identifier in quote marks.

   See the Oracle Net Services Administrators Guide or the Oracle
   operating system specific guide for more information on naming.

A wrong tnsnames.ora is the more likely.

Regards
Michel
Previous Topic: Connecting to Oracle using Pro*C/C++
Next Topic: Pro*C connection problem
Goto Forum:
  


Current Time: Thu Mar 28 09:12:18 CDT 2024