Using dbExpress Framework on Windows and Mac with C++Builder

Let's share the knowledge with your friends

During the following weeks I’m writing a series of C++ examples to demonstrate the use of VCL, FireMonkey, RTL, dbExpress, and others stuff. Each example will have a major focus, but you will be able to learn other stuffs as well. If there is something specific on C++ you would like to see, please let me know and I will do my best to bring that in one of my following posts.

In this first post I demonstrate how to use dbExpress Framework to run SQL’s against your database. Since I want to run the application on Windows and Mac, the console application will target FireMonkey.

The Console Application Wizard for C++ has been updated in C++Builder XE2, adding a Target Framework Option as you can see below.

The following code shows how to connect to an InterBase database, execute a SELECT SQL against the COUNTRY table, and display the results, of course you can use dbExpress Framework to run queries against any database supported by dbExpress, just remember that some dbExpress drivers are not available on Mac in XE2, for example SQL Server and ODBC drivers.

If you are C++ developer you will understand pretty quickly the following code, which has some comments to help you to understand the code.

#include <fmx.h>

#pragma hdrstop
#pragma argsused

#include <tchar.h>
#include <stdio.h>
#include <System.SysUtils.hpp>
#include <Data.DBXDynalink.hpp>
#include <Data.DBXCommon.hpp>
#include <Data.DBXInterbase.hpp>
#include <memory>

int _tmain(int argc, _TCHAR* argv[]) {

	// Get database connection instance
	std::auto_ptr<TDBXConnection>conn
		(TDBXConnectionFactory::GetConnectionFactory()->GetConnection
		("EMPLOYEE", "sysdba", "masterkey"));

	if (conn.get() != NULL) {

		printf("================= Connection Properties ============\n");

		AnsiString s = conn->ConnectionProperties->Properties->Text + "\n";
		printf(s.c_str());

		// create command and transaction objects to execute the query
		std::auto_ptr<TDBXCommand>command(conn->CreateCommand());

                // initiate a transaction
		TDBXTransaction *transaction =
			conn->BeginTransaction(TDBXIsolations::ReadCommitted);

		command->Text = "SELECT * FROM Country";
		command->Prepare();

		// execute the query and get the cursor (DBXReader)
		std::auto_ptr<TDBXReader>reader(command->ExecuteQuery());

		// print number of columns and each record
		printf(AnsiString("Number of Columns:" + IntToStr(reader->ColumnCount) +
			"\n").c_str());

		// display the list of records
		while (reader->Next()) {
			printf((reader->Value[reader->GetOrdinal("COUNTRY")]->GetAnsiString
				() + "\n").c_str());
		}

		printf("====================================================\n");

		conn->CommitFreeAndNil(transaction);
	}

	Sleep(5000);
	return 0;
}

Before you run this code, make sure you have the dbExpress alias EMPLOYEE setup to connect into the EMPLOYEE.GDB database and the InterBase client installed, if everything is ok just compile and run the program on Windows and Mac, the following image shows this application running on Mac.

There are few things you need to be aware when deploying and running this kind of application on Mac; on my following post I will explain everything about that.

For now we’ve learned how to use DBX Framework and make the application to compile and run on Windows and Mac.

You can download the source code from here or just update your local RAD Studio XE2 samples folder from our RAD Studio SVN repository.


Let's share the knowledge with your friends
2 replies
  1. Alan Taylor
    Alan Taylor says:

    Hi,

    Thanks for some C++ Builder recognition. I would like to see how you would pool DBExpress SQL Server connections in a DataSnap Server Module, including thread management. If you want, I can send you my Pooling object for your professional comment. You could polish it up and make it available to all.

    Al.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.