-
December 16th, 2005, 06:38 PM
#1
Accessing data from a database
Hi,
I'm trying to grab data from specific cells in a database and put them into a variable within an application. I'm creating an application that allows a user to input an id have the name associated with it returned to them, so basic database functions really. Except I'm not sure how to go about it.
I can pass SQL statements to the database, so basically I can alter the database with delete and insert commands, but I'm not sure what's happening when I pass a command that returns a value ...how can I access that value and assign it to a variable?
I've tried SQLBindCol() and SQLGetData(), but admittedly I don't have a full understanding how these work and I'm just getting a bunch of numbers, not the values expected.
Regarding the code below, that's about where I'm at now. Thing is, the SQL statement is specific and should only return a single value, so I'm not sure how that works in conjunction with SQLGetData, which, since you have to specify a column number I assume it must expect a row(s) of data. Or am I off base here?
Any pointers appreciated.
Code:
UCHAR firstName[128];
SDWORD cbFirstName;
string statement = "SELECT first_name FROM directory WHERE id = '123'";
retcode = SQLAllocStmt (hDBC, &hStmt);
retcode = SQLPrepare(hStmt,(SQLCHAR*)statement.c_str(),statement.size());
retcode = SQLExecute (hStmt);
retcode = SQLFetch (hStmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
SQLGetData(hStmt, 1, SQL_C_CHAR,firstName, 128, &cbFirstName);
retcode = SQLFetch (hStmt);
cout<<"firstName: "<<firstName<<endl<<"cbFirstName: "<<cbFirstName<<endl;
}
-
December 17th, 2005, 04:30 AM
#2
Re: Accessing data from a database
It takes seconds for rating…that actually compensates the minutes taken for giving answers
The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
Regards, Be generous->Rate people
Jayender!!
-
December 17th, 2005, 12:04 PM
#3
Re: Accessing data from a database
Thanks for the input. But how might I go about this using ODBC SQL calls? Without using other class libraries?
I think I've got it working, kinda. Instead of calling a specific field, like in the code above, and instead call the row, I can then use SQLBindCol() to select the name field using the column number:
Code:
string function = "SELECT * FROM directory WHERE id = 123";
[...]
SQLBindCol (hStmt, 2, SQL_C_CHAR, firstName, sizeof(firstName), &cbFirstName);
I wouldn't have thought that was the most efficient way of doing things, but that that's the best I've got to go on at the moment.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|