Sign in Join
Wiki copytemporary

Firebird embedded: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# What is Firebird?
22
33Firebird embedded is a special version of the [[Firebird]] database server. It lets you run your programs without a dedicated database server. Instead, you add a library (DLL/.so/.dylib) to your application) and let your application access your Firebird .fdb database file using that library.
44
55This is similar to the way programs like Microsoft Access and LibreOffice Base can work.
66
77## Advantages
88
99- runs on many architectures (Linux, macOS, Windows, FreeBSD, Solaris)
1010- no separate server setup necessary, reducing complexity
1111- you can easily switch from embedded to full client/server - without changing a line of code (except your connection string). This is probably a nice advantage compared to sqlite.
1212
1313## Disadvantages
1414
1515- only single user access
1616- need to distribute some libraries along with your program when distributing it (unless you can statically link them)
1717
1818# Installation
1919
2020On Windows, Firebird embedded and regular Firebird client libraries are different files. Firebird embedded can always serve as a regular client, making it a logical choice for inclusion in your installation.
2121
2222## Windows
2323
2424Download and unzip the Firebird embedded kit. Make sure the .dll and .manifest files are either:
2525
2626- in your project directory and executable output directory (where the .exe is generated) or
2727- in a directory in your PATH (not your system directory) (useful if you don't want to keep copying dlls)
2828
2929Note that bitness must match: you must use the 64 bit embedded version if you compile your program for 64 bit, and 32 if you're writing 32 bit programs. Also make sure the the .dlls and .manifest files are placed in your project output (executable) directory, and distribute them with your applications, along with the license files.
3030
3131As an indication, the minimum necessary files for Firebird 2.5 are:
3232
3333- fbembed.dll
3434- firebird.msg
3535- ib_util.dll
3636- icudt30.dll
3737- icuin30.dll
3838- icuuc30.dll
3939- IDPLicense.txt
4040- IPLicense.txt
4141- Microsoft.VC80.CRT.manifest
4242- msvcp80.dll
4343- msvcr80.dll
4444
4545## Linux
4646
4747For Debian, probably similar for Ubuntu:
4848
4949Get library files as well as the symlink: e.g. /usr/lib/x86_64-linux-gnu/libfbembed.so linking to /user/lib/x86_64-linux-gnu/libfbembed.so.bla.x.y
5050
5151``` bash
5252aptitude install libfbembed2.5 firebird-dev
5353```
5454
5555For at least Firebird 2.5+: Do not specify a username or password as that will force a lookup to security.fdb which may fail if system-wide security.fdb cannot be accessed. Do specify the role RDB\$ADMIN on Firebird 2.5+ as that should give you full control over the database file.
5656
5757Put these files in your project directory (to do: test if this is all needed):
5858
59-`firebird/libfbembed.so (or perhaps libfbembed.so.2.5)`\
60-`firebird.conf`\
61-`firebird/security.fdb #perhaps not necessary`\
62-`firebird/intl/fbintl`\
63-`firebird/udf/fbudf.so`
59+```text
60+firebird/libfbembed.so (or perhaps libfbembed.so.2.5)
61+firebird.conf
62+firebird/security.fdb #perhaps not necessary
63+firebird/intl/fbintl
64+firebird/udf/fbudf.so
65+```
6466
6567For Firebird 3 you will need:
6668
67-`firebird/libfbclient.so`\
68-`plugins/libEngine12.so`
69+```text
70+firebird/libfbclient.so
71+plugins/libEngine12.so
72+```
6973
7074You need to change plugins directory variable. If you cannot do that you need libEngine into its original directory.
7175
7276Had set this in firebird.conf (to do: check if this is all needed)
7377
7478``` ini
7579RootDirectory=./firebird #replace with your directory
7680DatabaseAccess = Full #probably useful
7781ExternalFileAccess = Full #probably useful
7882UdfAccess = Full #probably useful
7983#disable
8084#TempDirectories = /tmp #=> didn't fix /tmp/firebird lock issues
8185#TempDirectories = ~/tmp #=> didn't fix /tmp/firebird lock issues
8286```
8387
84882 environment variables need to be set before the application is started. Supposing the application is installed in /home/pascaldev/embed:
8589
8690``` bash
8791# tell dynamic loader where to find embedded lib:
8892LD_LIBRARY_PATH=/home/pascaldev/embed/firebird
8993# tell server where to find files (messages, config etc)
9094FIREBIRD=. #or use absolute path: /home/pascaldev/embed
9195```
9296
9397If you hit errors like
9498
95-`EIBDatabaseError/ : DoInternalConnect :`\
96-` -Can't access lock files' directory /tmp/firebird`
99+```text
100+EIBDatabaseError/ : DoInternalConnect :
101+ -Can't access lock files' directory /tmp/firebird
102+```
97103
98104you probably have stale lock files lying around that are owned by another user Deleting the /tmp/firebird directory works, but the problem is that it will be recreated under your own local permissions
99105
100106See [How to setup Firebird embedded on Linux or Mac?](http://www.firebirdfaq.org/faq51/) for hints on working with embedded Firebird on Linux or Apple macOS. Somebody used Firebird embedded with FPC: [Earlier version of Firebird embedded on Linux](http://www.mwasoftware.co.uk/index.php?option=com_content&task=view&id=105)
101107
102108## macOS
103109
104110- todo: write this, perhaps adapt [Earlier version of Firebird embedded on Linux](http://www.mwasoftware.co.uk/index.php?option=com_content&task=view&id=105) and especially [1](http://paulbeachsblog.blogspot.mx/2012/09/firebird-embedded-on-macosx.html)
105111
106112## FreeBSD
107113
108114- to do: write this, perhaps adapt [Earlier version of Firebird embedded on Linux](http://www.mwasoftware.co.uk/index.php?option=com_content&task=view&id=105)
109115
110116# Connecting to Firebird servers
111117
112118As mentioned, Firebird embedded can work like a regular Firebird client. Therefore, using the embedded Firebird library instead of the client/server Firebird library can help make your application more flexible.
113119
114120Note: there is a bug at least in Firebird embedded 2.5.3 together with FPC 2.6.4 that crashes when closing a connection to a remote server. This bug has been fixed in FPC trunk.
115121
116122# Use
117123
118124Once you have the proper Firebird embedded libraries in the right path (see above), you need to specify that you are connecting to an embedded instead of a client server environment. Leave the hostname property of the *IBConnection* object empty. The rest of the properties work like in the client/server version, e.g. the DatabaseName is the full path to your .fdb file - or just the database name if it is in the same directory as fbembed.dll. Additionally, older versions of FPC (FPC 2.6.2+ certainly don't need this) may require that you set the *UseEmbeddedFirebird* property to true. The property is not present in the SQLDB units, but you need to add *ibase60dyn* to your uses clause.
119125
120126## Creating databases
121127
122128### Using Firebird ISQL
123129
124130You can create databases by using the Firebird isql application (e.g. as provided in the regular Firebird (client) package). On Windows, this executable should be in the same directory as your fbembed.dll. Note: isql expects the fbclient library, not fbembed; you can copy and rename fbembed, e.g. into fbclient.dll on Windows, to fix this. Run **isql**, then do:
125131
126132``` sql
127133create database 'employee.fdb' user 'SYSDBA' password 'masterkey' page_size 16384 default character set UTF8;
128134exit;
129135```
130136
131137Dropping/deleting a database would basically mean deleting the file, but you can also run: **isql**
132138
133139``` sql
134140connect 'employee.fdb' user 'sysdba' password 'masterkey';
135141drop database;
136142commit;
137143exit;
138144```
139145
140146### Programmatically creating databases
141147
142148You can also create a database from within your program. The program in the Creating the test database section below shows how you can detect missing databases and create them on the fly. A similar procedure could be called in Lazarus applications when initializing your form (FormShow event).
143149
144150### Creating objects like tables, views, triggers...
145151
146152Creating the objects - as most things - is the same in Firebird and Firebird embedded. Please see [[Firebird|Firebird#Creating_objects_programmatically]]
147153
148154# Running FPC database test suite
149155
150156It can be useful to run the FPC database test framework (in packages\fcl-db\tests) when you are hunting bugs, trying to find out how something works, or improving the database code.
151157
152158## Telling the framework which database to use
153159
154160You will need to edit the db test framework .ini file to tell the test executable that it needs to use your embedded database. See [[Databases|Databases#Running_FPC_database_tests]] for instructions on how to do this.
155161
156162## Creating the test database
157163
158164If you have set up the framework to use Firebird, you will need to prepare the environment for testing.
159165
160166Copy over the Firebird embedded libraries and other files for your architecture into the test directory.
161167
162168Now create the test database - manually or programmatically
163169
164170### Manually creating a database
165171
166172**isql**
167173
168174``` sql
169175create database 'test.fdb' user 'SYSDBA' password 'masterkey' page_size 16384 default character set UTF8;
170176exit;
171177```
172178
173179### Programmatically creating a database
174180
175181You can programmatically create a database. The example below creates a local, embedded database, but it can be easily changed to a remote database (adjust hostname and if necessary path):
176182
177183``` pascal
178184program firebirdembeddedtest;
179185{$mode objfpc}{$H+}
180186
181187uses
182188 {$IFDEF UNIX}{$IFDEF UseCThreads}
183189 cthreads,
184190 {$ENDIF}{$ENDIF}
185191 Classes, SysUtils,
186192 sqldb, IBConnection {for Firebird};
187193
188194const
189195 // Name for the database. Be sure to specify the same name in your database.ini
190196 DatabaseFile = 'test.fdb'; //Sensible name for a test database, right?
191197var
192198 Fire: TIBConnection;
193199
194200begin
195201 //Connection to Firebird database
196202 // The next line is needed for quite old FPC versions
197203 // Newer versions will first look for fbembed.dll in the application directory automatically
198204 //UseEmbeddedFirebird:=true; // Using embedded (and fbembed.dll) or regular client/server (fbclient.dll), requires ibase60dyn in uses
199205 Fire:=TIBConnection.Create(nil);
200206 try
201207 Fire.HostName := ''; //must be empty for embedded Firebird; must be filled for client/server Firebird
202208 Fire.DatabaseName := DatabaseFile; //(path and) filename
203209 // Username and password do not matter for authentication, but you do get authorizations in the database
204210 // based on the name (and optionally role) you give.
205211 Fire.Username := 'SYSDBA';
206212 Fire.Password := 'masterkey'; //default password for SYSDBA
207213 Fire.Charset := 'UTF8'; //Send and receive string data in UTF8 encoding
208214 Fire.Dialect := 3; //Nobody uses 1 or 2 anymore.
209215 Fire.Params.Add('PAGE_SIZE=16384'); //I like a large page size (used when creating a database). Useful for larger indexes=>larger possible column sizes
210216
211217 // Find out if there is a database in the application directory.
212218 // If not, create it. Note: this may fail if you don't have enough permissions.
213219
214220 // If you use client/server, you obviously don't need this part of the code.
215221 if (FileExists(DatabaseFile)=false) then
216222 begin
217223 writeln('File '+DatabaseFile+' does not exist.');
218224 writeln('Creating a Firebird embedded database...');
219225 // Create the database as it doesn't exist
220226 try
221227 Fire.CreateDB; //Create the database file.
222228 except
223229 on E: Exception do
224230 begin
225231 writeln('ERROR creating database. Probably problems loading embedded library:');
226232 writeln('- not all files present');
227233 writeln('- wrong architecture (e.g. 32 bit instead of 64 bit)');
228234 writeln('Exception message:');
229235 writeln(E.ClassName+'/'+E.Message);
230236 end;
231237 end;
232238 Fire.Close;
233239 end;
234240 finally
235241 Fire.Free;
236242 end;
237243end.
238244```
239245
240246## Running the test suite
241247
242248Finally compile and run the *dbtestframework.pas* database test program. It should generate status output on the console and XML output in files with test results that can be analysed and sent to developers.
243249
244250# Windows x64 and FPC pre-March 2013
245251
246252> **Warning:** In versions of FPC below FPC 2.7.1 r23738 (March 2013), FPC x64 on Windows does not work well with the Firebird embedded dll - or other DLLs. The section below contains a workaround if you have an old FPC version
247253
248254On versions of FPC below 2.7.1 r23738 you can get errors/exceptions from the IBConnection object even if you write correct code (see e.g. [Create database bug report](http://bugs.freepascal.org/view.php?id=21581).
249255
250256Workarounds:
251257
2522581. move to a development snapshot/FPC version after FPC 2.7.1 r23738; see [[http://wiki.lazarus.freepascal.org/User Changes 3.0|http://wiki.lazarus.freepascal.org/User_Changes_3.0#Exception_handling_has_been_changed_to_be_ABI-conformant]]
2532592. if on FPC 2.7.1 lower than r23738, compile FPC with -dTEST_WIN64_SEH in order to use the new Structured Exception Handling (SEH) code on Win64.
2542603. use a 32 bit FPC and Lazarus on Windows 64
255261
256262# External references
257263
258264- [How to setup Firebird embedded on Linux or Mac?](http://www.firebirdfaq.org/faq51/)
259265- [Earlier version of Firebird embedded on Linux](http://www.mwasoftware.co.uk/index.php?option=com_content&task=view&id=105)
260266
261267---
262268*From the Free Pascal wiki, page "Firebird embedded", as it stood on 2020-02-16 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history