|
-
January 16th, 2011, 04:10 PM
#1
C++ and MySQL connection, just a few questions
Hey all,
First time here and I'm in the beginning of learning C++ on my own.
I know some basics and allthough PHP is not exactly C++ it has some similarities and I believe
that I do have some help with over 10 years of PHP development in my back..
Anyhow, in PHP there is a command for receiving mysql field names in an array, named;
"mysql_fetch_assoc" which can be found either on this link;
http://dev.mysql.com/doc/refman/5.1/...ql-fetch-assoc
or on the following;
http://se.php.net/mysql_fetch_assoc
This makes it so you can easily use $result['memberId'] to extract the information from the memberId field
from the row you're looking at.
What I understand, both from not seeing that available in the C version of the MySQL API and
from some previous programming in C, this isn't "possible" because there are only numeric index arrays in C..
Is this correct? (not completely sure)
Anyway, is there a way to solve it or am I forced to use "result[0]" to get the fieldvalue
from the row I'm currently reading?
Also, I've briefly looked at MySQL++ and that is surely nothing for me as I intend to write my
own library, both for licensing issues and also to actually learn from the ground up.
I'm using Visual C++ on Windows7 64-bit, but I believe I'm running a 32-bit version of VC++ if
that makes any differences.. 
Hope to hear from you guys soon and thanks in advance for your help!
-
January 16th, 2011, 09:33 PM
#2
Re: C++ and MySQL connection, just a few questions
Wow that's amazing lol. I've used mysql many times (freelance web dev) and I never knew you could use c to access it. Anyways, from what I'm reading in the docs, there is no fetch_assoc because, like you said, c only has numeric indices.
But mysql_fetch_field seems like what you want as it returns the current field name in the mysql object. So you can still access your fields by name.
http://dev.mysql.com/doc/refman/5.0/...tch-field.html
-
January 18th, 2011, 03:48 PM
#3
Re: C++ and MySQL connection, just a few questions
Hey, thanks for your reply!
Nice that I managed to make someone else learn something, that doesn't happen too often 
Anyhow, yeah in PHP it's quite easy, which it seems not to be in C..
But by using mysql_fetch_field I only get the name of the fields and their relative IDs in the
order I SELECT them within the query.
My thought on a resolution would be to create two arrays.
One holding the result from mysql_fetch_field;
Code:
Array
(
[0] => mUniqueId
[1] => mId
[2] => mUsername
[3] => mPassword
[4] => mActive
[5] => mLastLogin
)
This array holds both the names of fields and their relative IDs.
Then I create another array that looks like this;
Code:
Array
(
[0] => 1
[1] => dbee8a4bd093d58d8616895454512c96
[2] => toffo
[3] => 775a4c51da90b7840227230c9fc9ad2f
[4] => 1
[5] => 1295382941
)
Then I could do an in_array search in the first array, probably with a new function as in_array
doesnt seem to exist in C++ either!?!?
Something like arrAssoc(arrayName, "mUsername")
That would then give a return value from array 2 that should be "toffo".
Would that be overkill and very unnecessary?
The result of doing it this way is, creating a function to do something that should already be there..?!
Also, doing two queries on the database, which double the amount of queries going to the database server..
In the end, would this be something You would use? (anyone reading the thread)
No matter that answer, how do you access your databases?
Do you code with result[2] everywhere you want to get the mUsername field used? Or do you define
variables to hold the value or.. How do You handle your database handling?
Let me know so I can "get over" the PHP way
-
January 19th, 2011, 08:58 AM
#4
Re: C++ and MySQL connection, just a few questions
In C++ you would store the data in a 'struct'
Code:
struct Record
(
int mUniqueId;
int mId
std::string mUsername;
std::string mPassword;
bool mActive;
int mLastLogin;
);
Record result;
... fill in the data from the request
std::string name = result.mUsername;
Check out the STL containers and algorithms.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
January 19th, 2011, 10:07 AM
#5
Re: C++ and MySQL connection, just a few questions
In C/C++ you would do
Code:
- connect to database
- build a select statement as a string e. g. "select from users where username = 'toffo'"
+ you normally would do normal string concatenation with operator +=
+ or using a stringstream as a helper like oss << "select ... '" << username << "'";
- create a buffer for the fetch statement
+ for single fetch a struct like the one John has introduced is fine beside that std::string
would be replaced by a char array[] that has maximum size as defined for the varchar2
column.
+ Also bool probably must be replaced by an integer.
- bind the columns to the buffer addresses
- execute the select statement
- do a fetch to read record by record until error and store the records read in a container
(if more than one)
- alternatively provide an array of your structure and do a multi-fetch
You will find functions connect, bind, execute, fetch (similar names) in the API used. Or you use ODBC API where you could have an arbitrary DBMS.
-
January 19th, 2011, 10:11 AM
#6
Re: C++ and MySQL connection, just a few questions
Note, there were also API's and class libraries which do it similar to PHP. Often those tools can generate C/C++ code directly from the table definition of the data source. Then you only would need to provide the select statement.
-
January 19th, 2011, 10:58 AM
#7
Re: C++ and MySQL connection, just a few questions
Create an array of structures, that's what I would do.
-
January 21st, 2011, 08:30 AM
#8
Re: C++ and MySQL connection, just a few questions
If you are going to use MySQL with C++ , you can start with higher level API ( as in mokia QT or wxWidgets ) both provide clean consistent interface for all major databases , don't waste your time doing things the hards way.
-
January 21st, 2011, 08:42 AM
#9
Re: C++ and MySQL connection, just a few questions
If you are going to use MySQL with C++ , you can start with higher level API ( as in mokia QT or wxWidgets ) both provide clean consistent interface for all major databases , don't waste your time doing things the hards way.
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
|