Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieving string from database via odbc?

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
hi all,

i'm new to odbc programming in c++. i've put together a simple program to learn on and it's failing in two places. first, a string value is coming back as jibberish. also, rowcount returns a negative value. what am i doing wrong here? connecting to a sql server database.

Code:
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <sql.h>             
#include <sqlext.h>          
#include <sqltypes.h>        
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	RETCODE retcode;
	SQLHENV hENVIR;
	SQLHDBC hCONNECT;
	SQLHSTMT hSTMT;
	SQLCHAR szServer[] = "sql76";
	SQLCHAR szUser[] = "user";
	SQLCHAR szPassword[] = "password";
	SQLCHAR szSQL[256];

	//CORE INITIALIZATION
	SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hENVIR);
	SQLSetEnvAttr(hENVIR, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 
	SQLAllocHandle(SQL_HANDLE_DBC, hENVIR, &hCONNECT);
	retcode = SQLConnect(hCONNECT, szServer, SQL_NTS, szUser, SQL_NTS, szPassword, SQL_NTS);
	SQLAllocHandle(SQL_HANDLE_STMT, hCONNECT, &hSTMT );

	//DATA COMMUNICATION
	SQLINTEGER sqPTID;
	SQLCHAR sqPATFNAME[256];
	SQLINTEGER signal;                   

	SQLBindCol(hSTMT, 1, SQL_C_SLONG, &sqPTID, sizeof(sqPTID), &signal);
	SQLBindCol(hSTMT, 2, SQL_C_CHAR, &sqPATFNAME, SQL_NTS, &signal);

	long state;
	strcpy((char*)szSQL,"SELECT PTID, PAT_FIRST_NAME FROM mrpa99");
	state = SQLExecDirect(hSTMT,szSQL, SQL_NTS);
	state = SQLFetch(hSTMT);
	state = SQLFetch(hSTMT);

//sqPATFNAME printing as a series of ¦¦¦¦¦¦¦ characters
	cout << sqPTID << ": " << sqPATFNAME << endl;

	SQLINTEGER rowcount;
	SQLRowCount(hSTMT, &rowcount);

//rowcount returning -1
	cout << rowcount << endl;

	SQLCloseCursor(hSTMT);
	SQLEndTran (SQL_HANDLE_ENV, hENVIR, SQL_ROLLBACK);

	//CORE CLEANUP
	SQLFreeHandle(SQL_HANDLE_STMT, hSTMT); 
	SQLDisconnect(hCONNECT);
	SQLFreeHandle(SQL_HANDLE_DBC, hCONNECT); 
	SQLFreeHandle(SQL_HANDLE_ENV, hENVIR); 

	return 0;
}
 
well, i found the problem w/ the string, though i don't understand why the existing code was failing.

i changed

Code:
SQLBindCol(hSTMT, 2, SQL_C_CHAR, &sqPATFNAME, SQL_NTS, &signal);

to

Code:
SQLBindCol(hSTMT, 2, SQL_C_CHAR, &sqPATFNAME, sizeof(sqPATFNAME), &signal);
 
actually, it looks like it should be

Code:
SQLBindCol(hSTMT, 2, SQL_C_CHAR, sqPATFNAME, sizeof(sqPATFNAME), &signal);
 
Hey, I am also a beginning ODBC programmer in C/C++, and am about to throw myself off a liff through frustration.

I am VSC++ and have all the neccesary header and lib files that Microsoft say I need for my applications.

However I am getting unresolved external symbol errors. The linker cannot resolve the SQL functions.

These are the errors;

/****************************************************8888
serial.obj : error LNK2019: unresolved external symbol _SQLDisconnect@4 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLFreeHandle@8 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLFetch@4 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLBindParameter@40 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLPrepare@12 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLConnect@28 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLSetEnvAttr@16 referenced in function _main
serial.obj : error LNK2019: unresolved external symbol _SQLAllocHandle@12 referenced in function _main
/************************************************

The source code is compiling so it is not a header file problem. The problem lies in the linking of the lib and dll file. I have the appropriate lib files in a directory that I have told VSC++ to look in for lib files.

Can anyone help, its driving me insane. Especially as it is not a code problem but a linker error.

It seems to me that at link stage the linker does not know what lib file it should be linking to. Is there a way of specifying this i.e forcing the compiler to use that lib file in the link process. There has to be another way besides specifying the path in the lib directory search list.

Please help
 
You will need to link against odbc32.lib and/or odbccp32.lib.

#include< ... >
#include< ... >

...
/* you'll need */
#pragma comment(lib, "odbc32.lib")
/* and / or*/
#pragma comment(lib, "odbccp32.lib")

or add the lib to the project options tab in linker settings
 
Because you said “ I am new …using C/C++)” , PERMIT ME me to suggest you to start learning the .NET and C#. First of all, .NET will be the future and 2nd, C# is wholly new language designed explicitly for .NET with a syntax derived from C++.
The examples of code you post here will take only few lines where there are no pointers, no DLL, lib hell, no ODBC but some anothers clean and clear objects.
Jump there because this is the time.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top