Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-12-2006, 08:34 PM   #1 (permalink)
clavezza
Registered User
 
Join Date: Apr 2006
Posts: 1
clavezza is on a distinguished road
DevC++ Help

Hello Group,

I am looking for some help with DevC++ compiler. Version 4.9.9.2. In my database Management II Course, we are supposed to compile a sample ODBC Application written in C Code. When I open the compiler, I cut and pasted the code into the work space because the code is rather long. I've read in other C forum that the error messages I am getting is due to a linkage problem with SQL libraries. Can someone explain the proper steps to fixing this problem of my. I have been trying to figure this out for the past 5 days and could not seem to find a good manuel or tutorial about SQL Libraries and how to link them. Here is the error messages I am getting:

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
gcc.exe main.o -o "Sample.c" -L"C:/Dev-Cpp/lib"
main.o(.text+0x72):main.c: undefined reference to `SQLGetDiagField@28'
main.o(.text+0xe2):main.c: undefined reference to `SQLGetDiagRec@32'
main.o(.text+0x1c0):main.c: undefined reference to `SQLAllocHandle@12'
main.o(.text+0x1f8):main.c: undefined reference to `SQLSetEnvAttr@16'
main.o(.text+0x227):main.c: undefined reference to `SQLAllocHandle@12'
main.o(.text+0x26b):main.c: undefined reference to `SQLConnect@28'
main.o(.text+0x2a5):main.c: undefined reference to `SQLAllocHandle@12'
main.o(.text+0x323):main.c: undefined reference to `SQLPrepare@12'
main.o(.text+0x3bd):main.c: undefined reference to `SQLBindParameter@40'
main.o(.text+0x457):main.c: undefined reference to `SQLBindParameter@40'
main.o(.text+0x4f1):main.c: undefined reference to `SQLBindParameter@40'
main.o(.text+0x58b):main.c: undefined reference to `SQLBindParameter@40'
main.o(.text+0x61d):main.c: undefined reference to `SQLExecute@4'
main.o(.text+0x6b9):main.c: undefined reference to `SQLPrepare@12'
main.o(.text+0x753):main.c: undefined reference to `SQLBindParameter@40'
main.o(.text+0x7b9):main.c: undefined reference to `SQLExecute@4'
main.o(.text+0x80c):main.c: undefined reference to `SQLBindCol@24'
main.o(.text+0x844):main.c: undefined reference to `SQLBindCol@24'
main.o(.text+0x859):main.c: undefined reference to `SQLFetch@4'
main.o(.text+0x8db):main.c: undefined reference to `SQLEndTran@12'
main.o(.text+0x8fb):main.c: undefined reference to `SQLFreeHandle@8'
main.o(.text+0x90f):main.c: undefined reference to `SQLDisconnect@4'
main.o(.text+0x92f):main.c: undefined reference to `SQLFreeHandle@8'
main.o(.text+0x94b):main.c: undefined reference to `SQLFreeHandle@8'
collect2: ld returned 1 exit status
make.exe: *** [Sample.c] Error 1
Execution terminated


Here is the C Code that I am currently working on:
Code:
include <stdio.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

void print_error( SQLSMALLINT htype, /* A handle type identifier */
SQLHANDLE hndl, /* A handle */
SQLRETURN frc, /* Return code to be included with error msg */
int line, /* Used for output message, indcate where */
char * file /* the error was reported from */
) {

SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1] ;
SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1] ;
SQLINTEGER sqlcode ;
SQLSMALLINT length, i ;
SQLINTEGER NumRecords;

printf( ">--- ERROR -- RC = %d Reported from %s, line %d ------------\n",
frc,
file,
line
) ;
SQLGetDiagField(htype, hndl, 0,SQL_DIAG_NUMBER, &NumRecords, SQL_IS_INTEGER,NULL);
printf("Total Number of diagnostic records: %d\n",NumRecords); 
i = 1 ;
while ( SQLGetDiagRec( htype,
hndl,
i,
sqlstate,
&sqlcode,
buffer,
SQL_MAX_MESSAGE_LENGTH + 1,
&length
) == SQL_SUCCESS ) {
printf( " SQLSTATE: %s\n", sqlstate ) ;
printf( "Native Error Code: %ld\n", sqlcode ) ;
printf( "%s \n", buffer ) ;
i++ ;
}

printf( ">--------------------------------------------------\n" ) ;
}

int main()
{
// Declare The Local Memory Variables
SQLHANDLE EnvHandle = 0;
SQLHANDLE ConHandle = 0;
SQLHANDLE StmtHandle = 0;
SQLRETURN RetCode = SQL_SUCCESS;
SQLCHAR SQLStmt[255];
SQLCHAR JobType[10];
SQLCHAR EmpNo[10];
SQLCHAR LastName[25];
SQLCHAR FirstName[15];
int EmpId=1002;

/**
* INITIALIZATION 
**/
// Allocate An Environment Handle
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,&EnvHandle);

// Set The ODBC Application Version To 3.x
printf("Setting the ODBC version... \n");

if (EnvHandle != 0)
SQLSetEnvAttr(EnvHandle, SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3, 
SQL_IS_UINTEGER);

// Allocate A Connection Handle
printf("Creating Connection handle... \n");

if (EnvHandle != 0)
SQLAllocHandle(SQL_HANDLE_DBC, EnvHandle,&ConHandle);

// Connect To The Appropriate Data Source

if (ConHandle != 0)
{
RetCode = SQLConnect(ConHandle, (SQLCHAR *) "SAMPLE",SQL_NTS,
(SQLCHAR *) "app",SQL_NTS, 
(SQLCHAR *) "app", SQL_NTS);
printf("Got the connection... \n");
}

/*
* TRANSACTION PROCESSING 
**/

// Allocate An SQL Statement Handle
if (ConHandle != 0 && RetCode == SQL_SUCCESS)
SQLAllocHandle(SQL_HANDLE_STMT, ConHandle,&StmtHandle);
else{
printf("Error getting connection:\n");
print_error((SQLSMALLINT)SQL_HANDLE_DBC,ConHandle, 
RetCode,__LINE__,__FILE__ );
return(0);
}
// Define A SELECT SQL Statement That Uses A Parameter


strcpy((char *) SQLStmt, "INSERT INTO EMPLOYEE VALUES (?,?,?,?, \

CURRENT DATE)"); 
RetCode = SQLPrepare(StmtHandle, SQLStmt, SQL_NTS);
if(RetCode!=SQL_SUCCESS)
{
printf("Error preparing the insert statement:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}
RetCode = SQLBindParameter(StmtHandle, 1,SQL_PARAM_INPUT, SQL_C_LONG, 
SQL_INTEGER,sizeof(EmpId), 
0, &EmpId ,sizeof(EmpId), NULL);
if(RetCode!=SQL_SUCCESS)
{
printf("Error binding the first param in insert:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}
RetCode = SQLBindParameter(StmtHandle, 2,SQL_PARAM_INPUT, SQL_C_CHAR, 
SQL_CHAR,sizeof(FirstName), 
0, FirstName,sizeof(FirstName), NULL);
if(RetCode!=SQL_SUCCESS)
{
printf("Error binding the second param in insert:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}

RetCode = SQLBindParameter(StmtHandle, 3,SQL_PARAM_INPUT, SQL_C_CHAR, 
SQL_CHAR,sizeof(LastName), 
0, LastName,sizeof(LastName), NULL);
if(RetCode!=SQL_SUCCESS)
{
printf("Error binding the third param in insert:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}

RetCode = SQLBindParameter(StmtHandle, 4,SQL_PARAM_INPUT, SQL_C_CHAR, 
SQL_CHAR,sizeof(JobType), 
0, JobType,sizeof(JobType), NULL);
if(RetCode!=SQL_SUCCESS)
{
printf("Error binding the fourth param in insert:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}
strcpy((char *) FirstName, "Robert");
strcpy((char *) LastName, "Evans");
strcpy((char *) JobType, "ENGINEER");


RetCode = SQLExecute(StmtHandle);
if (RetCode == SQL_SUCCESS)
{
printf("Successfully executed the insert statement...\n");
}
else
{
printf("Error executing insert statement:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}

// A SELECT Statement

strcpy((char *) SQLStmt, "SELECT EMPNO, LASTNAME FROM ");
strcat((char *) SQLStmt, "EMPLOYEE WHERE JOBTYPE = ?");

// Prepare The SQL Statement

RetCode = SQLPrepare(StmtHandle, SQLStmt, SQL_NTS);
if(RetCode!=SQL_SUCCESS)
{
printf("Error preparing:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}

// Bind The Parameter Marker Used In The SQL Statement To
// An Application Variable

RetCode = SQLBindParameter(StmtHandle, 1,SQL_PARAM_INPUT, SQL_C_CHAR, 
SQL_CHAR,sizeof(JobType), 
0, JobType,sizeof(JobType), NULL);
if(RetCode!=SQL_SUCCESS)
{
printf("Error binding:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}
// Populate The "Bound" Application Variable

strcpy((char *) JobType, "ENGINEER");
// Execute The SQL Statement

RetCode = SQLExecute(StmtHandle);
// If The SQL Statement Executed Successfully, Retrieve
// The Results

if (RetCode == SQL_SUCCESS)
{
printf("Successfully executed the select statement...\n");

// Bind The Columns In The Result Data Set Returned
// To Application Variables

SQLBindCol(StmtHandle, 1, SQL_C_CHAR, (SQLPOINTER)EmpNo, 
sizeof(EmpNo), NULL);

SQLBindCol(StmtHandle, 2, SQL_C_CHAR, (SQLPOINTER)
LastName, sizeof(LastName), NULL);

// While There Are Records In The Result Data Set
// Produced, Retrieve And Display Them

while (RetCode != SQL_NO_DATA)
{
RetCode = SQLFetch(StmtHandle);
if (RetCode != SQL_NO_DATA)
printf("%-8s %s\n", EmpNo, LastName);
}
}else
{
printf("Error executing select statement:\n");
print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandl e,
RetCode,__LINE__,__FILE__ );
}
// Commit The Transaction
RetCode = SQLEndTran(SQL_HANDLE_DBC, ConHandle,SQL_COMMIT);

/**
* TERMINATION 
**/

// Free The SQL Statement Handle 
if (StmtHandle != 0)
SQLFreeHandle(SQL_HANDLE_STMT, StmtHandle);


// Terminate The Data Source Connection
if (ConHandle != 0)
RetCode = SQLDisconnect(ConHandle);

// Free The Connection Handle
if (ConHandle != 0)
SQLFreeHandle(SQL_HANDLE_DBC, ConHandle);

// Free The Environment Handle
if (EnvHandle != 0)
SQLFreeHandle(SQL_HANDLE_ENV, EnvHandle);

// Return Control To The OS
return(0);
}
Thanks Group for all of your help in this matter

Chris Lavezza
clavezza@hotmail.com

Last edited by Valmont; 04-13-2006 at 06:56 AM.
clavezza is offline   Reply With Quote
Old 04-13-2006, 06:58 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Two things:
1)
Code:
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
I think it should be:
Code:
#include "sql.h"
#include "sqlext.h"
#include "sqltypes.h"
Check where your headerfiles are.

Secondly:
include <stdio.h>
should be
#include <stdio.h>
__________________
Valmont is offline   Reply With Quote
Old 04-13-2006, 11:19 AM   #3 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
The problem isn't in your source code, it's in your compilation settings.

You need to tell the compiler what additional libraries to link with the program you're making. There may be a project/compiler settings tab in DevC++ to do this, or otherwise you may have to look at the makefile if you're using gcc.

I'm not familiar enough with DevC++ to offer more help, but hopefully this can at least point you in the right direction.
kyoryu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:53 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting