How do you access a database from a C++ or Windows API program? :-/ I would like to be able to get data from a database as strings or something. I want to use a database so that it is easily changed from MS Access or something similar. Thanks in advance.

Recommended Answers

All 8 Replies

The oldest and most common method is via ODBC. Its pretty complicated stuff. There are a few c++ classes, use google to find them too.

Another method is ADO, Microsoft has an ActiveX control for that.

MySQL is another simpler and free database server too. My advice is to browse your GUI library for built-in database functionality (I know Borland VCL has all sorts of nice GUI widgets for database programming, but it's a bit outdated (I haven't done database stuff for a long time), I'm sure MFC or Qt have plenty as well, this will be a big time-saver).

Yes, MFC has a database class, but its not really very good when it comes to complicated requirements. If all you need is a single table using simple selects and updates the MFC classes will work well.

Thanks everyone. Basically, I was thinking of writing a simple GUI to access a database. For example, I want to make a basic program which would give the user the prices of computer parts and what fits together etc. It's just fun to work on new things, at first I thought about text files etc, but I know how to do that already. I've heard of MFC, it's like an extension of the Windows API that simplifies coding, I think, but is there like a way with Windows API without MFC? Thanks.

I found some information at MSDN. Contains information on Database Functions (Windows). It says is for developers writing their own installers, so I'm not sure whether I can use these functions for normal database access. Anyone know how to use these functions?

You don't want to use those functions. google for "ODBC Tutorials" and you will learn how to read and update any SQL compliant database, such as MS Access, SQL Server, MySQL, Oracle, Sybase, etc. You will also need to learn SQL scripting language. google for SQL tutorials too.

If your database is "online," it is rather easy to access it with C++. To make things even simpler, you would probably want to use something like MySQL as your online database, but it's all a matter of taste really.

Now, onto the steps you'd take to access your online database.
First of all, you will have to have bought a domain that includes PHP/MYSQL support (assuming your database is a mySQL one, in this example). After you have bought a domain that supports both mysql and php (don't really need the php) accessing your (online) database, is a piece of cake.

You'd need to include <mysql.h>,<winsock.h>, and finally, libmysql.lib

Lastly, learn how to use SQL commands, in C++. Using SQL with C++ is almost as easy as using SQL with PHP.

Ex:
---
To connect to a MySQL database using C++, you'd have to use the mysql_real_connect() function. In PHP, the function to connect to a mySQL database, is mysql_connect(). (Just to display how similar PHP and C++ get when it comes to SQL commands).

Final Note:
-----------
If you would like to access an online database (particularly a mySQL one), you are going to have to first of all learn SQL (I suggest visiting w3schools.com for that)
After you have learned SQL, THEN you will be able to use C++ to implement SQL like commands to manipulate an SQL database.

*sigh* Looks like I'll have to learn ODBC. I don't really need the database online, but that was helpful information, as I might do that later. I just wanted to have a database for easy rearrangement of information for my program. Thanks everyone!

---------
I'll probably soon be starting a new thread asking for help on ODBC, seems kind of complicated. :confused:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.