Sign in Join
Wiki copytemporary

SQLdb Tutorial0: what changed

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

OldNewLine
11## Introduction
22
33This article will help you set up your database environment with sample tables and data to use in SQLdb Tutorial 1, 2 and 3.
44
55Although this page is quite long, you will only go through a few of the sections as this article contains instructions for various database systems.
66
77If you want to get up and running quickly, I'd suggest installing the Firebird server and example database as described below.
88
99## Requirements
1010
1111This tutorial is written for use with recent Lazarus versions (Laz 1.0); it should also work on older versions Lazarus 0.9.30.
1212
1313Furthermore you need an SQL/relational database, such as [Firebird](http://sourceforge.net/project/showfiles.php?group_id=9028) (if possible version 2.0 or newer). It's easiest if you use standard settings (e.g user name SYSDBA and password masterkey), and that you have the employee sample database installed.
1414
1515You can use another database (e.g. Microsoft SQL Server, MySQL, PostgreSQL, Oracle, SQLite, Sybase ASE or another database using ODBC). Please make sure you have the required database client libraries installed (see the various wiki articles on databases) Please see below (section **No Firebird or employee.fdb installed?**) for a way to set up your tables
1616
1717## Firebird installation
1818
1919In case you haven't yet installed Firebird or the sample database that is used with the tutorials, please find some instructions below.
2020
2121### Install Firebird client/server
2222
2323If you haven't installed anything, you can download and run the installer from www.firebirdsql.org and install the server (e.g. "32-bit Classic, Superclassic & Superserver").
2424
2525### Firebird database libraries on Windows
2626
2727After installation, on Windows you will need to have the Firebird client DLLs present:
2828
2929- they could be in your system directory (available for all programs)
3030- they can also be in your Lazarus directory (for design time support in the IDE) and in the output directory where the executable is (to run the compiled program).
3131
3232If you haven't installed the Firebird server, an easy way to get the client DLLs is: download Firebird Embedded 2.5 from [1](http://www.firebirdsql.org/en/firebird-2-5/) Extract these files to your application directory:
3333
3434``` bash
3535fbclient.dll #only if using the server
3636#or
3737fbembed.dll #only if using embedded
3838firebird.msg
3939ib_util.dll
4040icudt30.dll
4141icuin30.dll
4242icuuc30.dll
4343Microsoft.VC80.CRT.manifest
4444msvcp80.dll
4545msvcr80.dll
4646```
4747
4848Rename fbembed.dll to fbclient.dll (the name for a regular, client-server Firebird client - this helps use on older Lazarus/FPC versions). The embedded Firebird DLL can also act as a regular Firebird client.
4949
5050Make sure the employee.fdb database is in your project directory. You can copy it from the examples/empbuild/ directory of the firebird 2.5 server.
5151
5252Finally, compile your project (even if it empty) once to create the output directory, and copy the dlls, as well as the employee.fdb database, into that directory.
5353
5454### Firebird database libraries on other systems
5555
5656On Linux/OSX, you will also need the Firebird client shared libraries. On Linux you can use your distribution's method of getting programs to get the Firebird client packages, e.g. on Debian:
5757
5858``` bash
5959aptitude install libfbclient2 firebird-dev #we need the dev version because FPC 2.6 and lower will look for libfbclient.so
6060```
6161
6262## No Firebird or employee.fdb installed?
6363
6464If you don't have the employee sample database installed or are using a different database, here is a minimal version of the table we'll be using (note: directions for some specific databases can be found below).
6565
6666### Automatic creation
6767
6868The easiest way of setting this up is to create a new empty database on your system, then connect to it with the TSQLScriptSample sample/utility program. This program is included with current Lazarus development releases in the `./examples/database/tsqlscript` directory, but can also be downloaded from
6969
7070
7171<http://svn.freepascal.org/svn/lazarus/trunk/examples/database/tsqlscript/>
7272
7373Compile and run the program, then connect to the database:
7474
75-<figure>
76-<img src="tsqlscriptsample_connect.png" title="tsqlscriptsample_connect.png" />
77-<figcaption>tsqlscriptsample_connect.png</figcaption>
78-</figure>
75+![](/wiki-images/6/6e/Tsqlscriptsample_connect.png)
7976
8077then click the button *Copy table creation script*, and *Run script*:
8178
82-<figure>
83-<img src="tsqlscriptsample_runfirstscript.png" title="tsqlscriptsample_runfirstscript.png" />
84-<figcaption>tsqlscriptsample_runfirstscript.png</figcaption>
85-</figure>
79+![](/wiki-images/a/ab/Tsqlscriptsample_runfirstscript.png)
8680
8781The program should indicate success. Now do the same for *Copy sample data script*, and *Run script*
8882
8983In case of problems, you can try the manual steps below.
9084
9185### Manual creation
9286
9387We will create CUSTOMER and EMPLOYEE tables that will be used in a later tutorial.
9488
9589Run these SQL commands in your database editor/tool:
9690
9791``` sql
9892CREATE TABLE CUSTOMER
9993(
10094 CUST_NO INTEGER NOT NULL,
10195 CUSTOMER VARCHAR(25) NOT NULL,
10296 CITY VARCHAR(25),
10397 COUNTRY VARCHAR(15),
10498 CONSTRAINT CT_CUSTOMER_PK PRIMARY KEY (CUST_NO)
10599);
106100```
107101
108102``` sql
109103CREATE TABLE EMPLOYEE
110104(
111105 EMP_NO INTEGER NOT NULL,
112106 FIRST_NAME VARCHAR(15) NOT NULL,
113107 LAST_NAME VARCHAR(20) NOT NULL,
114108 PHONE_EXT VARCHAR(4),
115109 JOB_CODE VARCHAR(5) NOT NULL,
116110 JOB_GRADE INTEGER NOT NULL,
117111 JOB_COUNTRY VARCHAR(15) NOT NULL,
118112 SALARY NUMERIC(10,2) NOT NULL,
119113 CONSTRAINT CT_EMPLOYEE_PK PRIMARY KEY (EMP_NO)
120114);
121115```
122116
123117Some data so you can at least show something.... first some clients:
124118
125119``` sql
126120INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (1, 'Michael Design', 'San Diego', 'USA');
127121INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (2, 'VC Technologies', 'Dallas', 'USA');
128122INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (3, 'Klämpfl, Van Canneyt', 'Boston', 'USA');
129123INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (4, 'Felipe Bank', 'Manchester', 'England');
130124INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (5, 'Joost Systems, LTD.', 'Central Hong Kong', 'Hong Kong');
131125INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (6, 'Van der Voort Int.', 'Ottawa', 'Canada');
132126INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (7, 'Mrs. Mauvais', 'Pebble Beach', 'USA');
133127INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (8, 'Asinine Vacation Rentals', 'Lihue', 'USA');
134128INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (9, 'Fax', 'Turtle Island', 'Fiji');
135129INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (10, 'FPC Corporation', 'Tokyo', 'Japan');
136130INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (11, 'Dynamic Intelligence Corp', 'Zurich', 'Switzerland');
137131INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (12, '3D-Pad Corp.', 'Paris', 'France');
138132INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (13, 'Swen Export, Ltd.', 'Milan', 'Italy');
139133INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (14, 'Graeme Consulting', 'Brussels', 'Belgium');
140134INSERT INTO CUSTOMER (CUST_NO, CUSTOMER, CITY, COUNTRY) VALUES (15, 'Klenin Inc.', 'Den Haag', 'Netherlands');
141135```
142136
143137Then some employees:
144138
145139``` sql
146140INSERT INTO EMPLOYEE (emp_no, first_name, last_name, phone_ext, job_code, job_grade,
147141 job_country, salary)
148142 VALUES (1,'William','Shatner','1702','CEO',1,'USA',48000);
149143INSERT INTO EMPLOYEE (emp_no, first_name, last_name, phone_ext, job_code, job_grade,
150144 job_country, salary)
151145 VALUES (2,'Ivan','Rzeszow','9802','Eng',2,'Russia',38000);
152146INSERT INTO EMPLOYEE (emp_no, first_name, last_name, phone_ext, job_code, job_grade,
153147 job_country, salary)
154148 VALUES (3,'Erin','Powell','1703','Admin',2,'USA',45368);
155149```
156150
157151Please create the database, table and insert the data in your database environment.
158152
159153### SQLite
160154
161155If you are using SQLite, you can create the database mentioned above in your project directory by running the sqlite executable:
162156
163157``` bash
164158sqlite employee.sqlite
165159```
166160
167161Now copy and paste the above CREATE TABLE and INSERT statements. To test if the right data is present, enter this query:
168162
169163``` SQL
170164select * from customer;
171165```
172166
173167End your session with
174168
175169``` SQL
176170.quit
177171```
178172
179173A file called employee.sqlite should now be created in your project directory.
180174
181175Make sure the required sqlite dll/so is installed - e.g. on Windows, sqlite3.dll should be present in
182176
183177- your Lazarus + project output directory or
184178- your system directory
185179
186180Also, if you have a 32-bit sqlite dll, make sure you have a 32-bit lazarus.
187181
188182Compile your project (even if it is empty) once to create the output directory, and (on Windows) copy the dll, as well as the employee.sqlite database, into that directory.
189183
190184### PostgreSQL
191185
192186This section assumes you're using a Linux server and the shell; comparable steps can be done using Windows and GUI tools such as pgadmin.
193187
194188Log in to your server and switch to the postgres account:
195189
196190``` bash
197191su - postgres -c psql # immediately start up psql SQL interpreter
198192```
199193
200194Create a user for the database and the tables:
201195
202196``` SQL
203197CREATE USER employee WITH PASSWORD 'hellopassword'; -- of course, adjust password to taste
204198-- something like 'CREATE ROLE' should appear indicating success.
205199-- to later change the password you can use something like
206200-- alter user employee with password '<newpasswordhere>';
207201
208202-- We're going to let the password never expire; if you want more security, you can leave this step out:
209203ALTER USER employee VALID UNTIL 'infinity'; --password never expires
210204
211205-- Now we're tightening it up a bit again:
212206-- Don't allow user to create a database or create other users:
213207ALTER USER employee NOCREATEDB NOCREATEROLE; --restrict object creation
214208-- something like 'ALTER ROLE' should appear indicating success.
215209
216210-- Create our database:
217211CREATE DATABASE employee;
218212-- something like CREATE DATABASE should appear indicating success.
219213
220214-- Assign all privileges on database employee to user employee:
221215GRANT ALL PRIVILEGES ON DATABASE employee TO employee; -- allow user full permissions to database
222216-- something like GRANT should appear indicating success.
223217
224218-- We create the table using a serial datatype - aka autonumber/autoincrement:
225219CREATE TABLE customer
226220(
227221 cust_no serial NOT NULL,
228222 customer character varying(25) NOT NULL,
229223 city character varying(25),
230224 country character varying(15),
231225 CONSTRAINT integ_60 PRIMARY KEY (cust_no )
232226);
233227
234228-- Then create the employee table:
235229CREATE TABLE EMPLOYEE
236230(
237231 EMP_NO SERIAL NOT NULL,
238232 FIRST_NAME VARCHAR(15) NOT NULL,
239233 LAST_NAME VARCHAR(20) NOT NULL,
240234 PHONE_EXT VARCHAR(4),
241235 JOB_CODE VARCHAR(5) NOT NULL,
242236 JOB_GRADE INTEGER NOT NULL,
243237 JOB_COUNTRY VARCHAR(15) NOT NULL,
244238 SALARY NUMERIC(10,2) NOT NULL,
245239 CONSTRAINT CT_EMPLOYEE_PK PRIMARY KEY (EMP_NO)
246240);
247241
248242-- Now copy and paste the INSERT statements from the section above to insert the data.
249243
250244-- To test if the right data is present, enter this query:
251245SELECT * FROM customer;
252246-- You should see some customer data.
253247
254248-- Exit out of psql:
255249\q
256250```
257251
258252Now you should be on a shell logged in as the postgres user.
259253
260254If your server is on another machine than your development machine, make sure you allow network access to the database. See your postgresql documentation for details, but something like this should work:
261255
262256``` bash
263257# please adjust nano (e.g. use vim,emacs,joe...) and the postgres version number depending on situation
264258nano /etc/postgresql/8.4/main/pg_hba.conf
265259```
266260
267261Verify if there is a line like - NOTE: replace 192.168.0.1 with your own LAN ip address range
268262
269263\#allow access from local network using md5 hashed passwords: host all all 192.168.0.1/24 md5
270264
271265or more restrictive:
272266
273267\# only allow network access to the employee database by the employee user host employee employee 192.168.0.1/24 md5
274268
275269If there isn't such a line, add the line at the end, save and close your editor. See PostgreSQL documentation for more details.
276270
277271Reload PostgreSQL settings:
278272
279273``` bash
280274
281275psql
282276```
283277
284278then
285279
286280``` SQL
287281
288282SELECT pg_reload_conf(); --reload settings...
289283-- ...and exit back to shell:
290284\q
291285```
292286
293287Test logging in to PostgreSQL.
294288
295289Note: by default PostgreSQL tries an ident/unix domain socket login which doesn't allow passwords. So we specify a host to force TCP/IP login:
296290
297291``` bash
298292psql -h 127.0.0.1 -d employee -U employee -W #Log in via tcp/ip. Enter your db password.
299293```
300294
301295Make sure the required PostgreSQL dll/so and any required other libraries is installed - e.g. on Windows, they should be either:
302296
303297- in your Lazarus + project output directory or
304298- in your system directory
305299
306300Compile your project (even if it is empty) once to create the output directory, and (on Windows) copy the dlls into that directory.
307301
308302## Finish
309303
310304Now you've set up your database, you can continue to [[SQLdb Tutorial1|the first "real" tutorial]].
311305
312306## See also
313307
314308- [[SQLdb Tutorial1]]: First part of the DB tutorial series, showing how to set up a grid that shows database data
315309- [[SQLdb Tutorial2]]: Second part of the DB tutorial series, showing editing, inserting etc.
316310- [[SQLdb Tutorial3]]: Third part of the DB tutorial series, showing how to program for multiple databases and use a login form
317311- [[SQLdb Tutorial4]]: Fourth part of the DB tutorial series, showing how to use data modules
318312- [[Lazarus Database Overview]]: Information about the databases that Lazarus supports. Links to database-specific notes.
319313- [[SQLdb Package]]: information about the SQLdb package
320314- [[SQLdb Programming Reference]]: an overview of the interaction of the SQLdb database components
321315- [[SqlDBHowto]]: information about using the SQLdb package
322316- [[Working With TSQLQuery]]: information about TSQLQuery
323-
324-\
325317
326318---
327319*From the Free Pascal wiki, page "SQLdb Tutorial0", as it stood on 2019-08-29 after a change by Ottod, 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