Connecting Delphi on Linux to SQL Server
RAD Studio enables you build a Delphi application for both Windows and Linux platforms. You can, for example, build an ODBC application that uses a Microsoft ODBC driver on Windows and an Easysoft ODBC driver on Linux. In the following tutorial, which describes how to create a console application for Linux that retrieves SQL Server data, the components are:
Windows Machine --------------- RAD Studio Linux Machine ------------- Platform Assistant Server Delphi Application unixODBC Driver Manager SQL Server ODBC driver Windows Machine --------------- SQL Server
program SQLServer; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.ODBC, FireDAC.Phys.ODBCDef, FireDAC.DApt, Data.DB, FireDAC.Comp.Client, FireDAC.ConsoleUI.Wait; var RHConnection: TFDConnection; RHQuery: TFDQuery; sValue: String; begin try RHConnection:=TFDConnection.Create(nil); RHConnection.Params.Add('DriverID=ODBC'); RHConnection.Params.Add('DataSource=SQLSERVER_SAMPLE'); RHConnection.Connected:=true; sValue := RHConnection.ExecSQLScalar('select ''SQL Server from Linux'' as test_col'); Writeln(sValue); ReadLn; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.
- Download the SQL Server ODBC driver for 64-bit Linux platforms.
- Install and license the SQL Server ODBC driver on the machine where the Platform Assistant (PA Server) is or will be installed.
For installation instructions, refer to the SQL Server ODBC driver documentation.
Note You need the unixODBC Driver Manager installed on your machine. The Easysoft distribution includes a version of the unixODBC Driver Manager that the Easysoft SQL Server ODBC driver has been tested with. The Easysoft driver setup program gives you the option to install unixODBC.
- Create an ODBC data source in
/etc/odbc.ini
that connects to the SQL Server database you want to access from Delphi. For example:[SQLSERVER_SAMPLE] Driver = Easysoft ODBC-SQL Server Server = my_machine\SQLEXPRESS User = my_domain\my_user Password = my_password # If the database you want to connect to is the default # for the SQL Server login, omit this attribute Database = Northwind
- Use isql to test the new data source. For example:
cd /usr/local/easysoft/unixODBC/bin ./isql.sh -v SQLSERVER_SAMPLE
At the prompt, enter
help
to display a list of tables. To exit, press Return in an empty prompt line.If you're unable to connect, refer to this article and the SQL Server ODBC driver knowledge base for assistance.
- If you have not already done so, install the PA Server on the machine where you have installed the SQL Server ODBC driver.
- Set the environment on this machine so that your Delphi program can load the SQL Server ODBC driver. For example:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/easysoft/sqlserver:/usr/local/easysoft/lib: /usr/local/easysoft/unixODBC/lib export $LD_LIBRARY_PATH
Start the PA Server. For example:
cd ~/PAServer-19.0 ./paserver
If you did not install the unixODBC Driver Manager that is included in the SQL Server ODBC driver distribution, omit
/usr/local/easysoft/unixODBC/lib
from the environment variable value. - In RAD Studio, create a new Delphi console application.
- Set the target platform for the application to be 64-bit Linux.
- Edit the profile properties for your target platform to specify the details for your PA Server.
- Insert the code shown at the start of this tutorial into the application.
- Run the application.