c++ builder console-mode example -- where can I find one?

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
PhilCo
Posts: 4
Joined: Mon 18 Jun 2012 19:34

c++ builder console-mode example -- where can I find one?

Post by PhilCo » Wed 17 Apr 2013 14:35

I'd like to use UniDAC in several console-mode programs I'm building. The help file states that this facility is included in UniDAC, by design, but I've yet to find a working example of how to set up such a Project, in C++.

That is, an example that does not result in fatal linker errors or run-time errors. I have made plenty of those myself! Obviously, I'm missing something in the source-code steps, or in how the Project is created, the linker settings, the run-time component settings, or some other Project settings. There are so many potential places to look for undocumented obstacles, it really is not funny.

The oldest compiler I might have to use for this is C++ Builder 6. The oldest operating system it might have to run on is Windows 2000 Professional. The program will have to connect to Oracle and SQLite, at a minimum. Ability to use CodeGuard would be a real plus.

I've spotted a Delphi console-mode example in this forum, but it does not appear complete, nor do I see any clear way to translate it to C++.

Does anyone know where I can find such an example?

-PhilCo

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: c++ builder console-mode example -- where can I find one?

Post by AlexP » Thu 18 Apr 2013 14:10

Hello,

Here is a sample of a console application for С++ Builder XE2 to work with SQLite, the code will be the same for other DBs, except of specifying the provider and involving appropriate modules

Code: Select all

#include <vcl.h>
#include <windows.h>

#pragma hdrstop
#pragma argsused

#include <tchar.h>
#include <stdio.h>
#include "DBAccess.hpp"
#include "MemDS.hpp"
#pragma link "liteprovider160.lib"
#pragma link "SQLiteUniProvider"
#include "Uni.hpp"
#include "UniProvider.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
	TUniConnection *UniConnection = new TUniConnection(NULL);
	try
	{
		UniConnection->ProviderName = "SQLite";
		UniConnection->Database = ":memory:";
		UniConnection->Connect();
		UniConnection->ExecSQL("CREATE TABLE T_TEST(F_ID INTEGER)");
		UniConnection->ExecSQL("INSERT INTO T_TEST VALUES(1),(2),(3),(4),(5)");

		TUniQuery  *UniQuery = new TUniQuery(NULL);
		try
		{
			UniQuery->Connection = UniConnection;
			UniQuery->SQL->Text = "SELECT * FROM T_TEST";
			UniQuery->Open();
			while (!UniQuery->Eof)
			{
				ShowMessage(UniQuery->FieldByName("F_ID")->AsString);
				UniQuery->Next();
			}
			UniQuery->Close();
			UniConnection->Close();
		}
		__finally
		{
			UniQuery->Free();
		}
	}
	__finally
	{
		UniConnection->Free();
	}
	return 0;
}

PhilCo
Posts: 4
Joined: Mon 18 Jun 2012 19:34

Re: c++ builder console-mode example -- where can I find one?

Post by PhilCo » Thu 18 Apr 2013 19:57

I appreciate your efforts. Thank you.

Using default project settings, and making only the absolute minimal changes needed to make the code compile and link with BCB6 and UniDAC 4.3.8, the program crashes on the Connect() call, with an access violation.

Project [exe name] faulted with message: 'access violation at 0x77fac528: write of address 0x00030ffc'. Process Stopped. Use Step or Run to continue.

All my prior attempts to Connect(), from a console-mode program, have ended with access violations.

I've saved screen shots of all of the Project settings, PATH, computer settings. The (slightly modified) source is ready to send.

For diagnostic purposes, what would you like me to do next?

-PhilCo

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: c++ builder console-mode example -- where can I find one?

Post by AlexP » Fri 19 Apr 2013 11:54

Hello,

We have tested our UniDAC 4.6.12 on С++ Builder 6 Build 10.166 with SQLite3.dll 3.7.16.2 - and haven't detected AV when connecting to the DB. Please try to reproduce the error on the latest UniDAC version and SQLite3.dll

jft
Posts: 7
Joined: Wed 08 Nov 2006 03:22

Re: c++ builder console-mode example -- where can I find one?

Post by jft » Thu 20 Mar 2014 08:00

For those looking for a Delphi equivalent of the above example code to use Unidac in a Delphi console application or a Delphi DLL, here you are (you'll need sqlite3.dll in the path):

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,DBAccess,MemDS,Uni,UniProvider,SQLiteUniProvider,Dialogs;

var
  UniConnection : TUniConnection;
  UniQuery      : TUniQuery;

begin
  UniConnection := TUniConnection.Create(nil);
  try
    try
      UniConnection.ProviderName := 'SQLite';
      UniConnection.Database     := ':memory:';
      UniConnection.Connect;
      UniConnection.ExecSQL('CREATE TABLE T_TEST(F_ID INTEGER)',[]);
      UniConnection.ExecSQL('INSERT INTO T_TEST VALUES(1),(2),(3),(4),(5)',[]);

      UniQuery  := TUniQuery.Create(nil);
        try
          UniQuery.Connection := UniConnection;
          UniQuery.SQL.Text := 'SELECT * FROM T_TEST';
          UniQuery.Open;
          while not UniQuery.Eof do begin
            ShowMessage(UniQuery.FieldByName('F_ID').AsString);
            UniQuery.Next;
          end;
          UniQuery.Close;
          UniConnection.Close;
        finally
           UniQuery.Free;
        end;
     finally
       UniConnection.Free;
     end;
  except
    on E: Exception do
      ShowMessage(E.ClassName+': '+E.Message);
  end;
end.
Thanks Alex,
Cheers
John Townsend

Post Reply