Site hosted by Angelfire.com: Build your free website today!

..


We need to download 2 things.
The right dll and a SQLite3 database reader to check are work.
SQLite3 DLLs
Under Precompiled Binaries for Windows sect either 32 bit or 64 bit.
Put it in the program directory with the executable.
DB Browser for SQLite make it easier to debug SQLite programs.
DB Browser for SQLite

Are first SQLite3 program is going to use Lazarus (Free Pascal) to create a local database file.

Will let start by opening Lazarus, creating a form and call it MyFirstSQLite3App.

Object Inspector > Form1 > Caption = MyFirstSQLite3App
Then File > Save AS > (Create a directory SQLite3_Create) and File name: = SQLite3_Create.pas .
Press F9 to run program.

As you know. If you drop a component on a form from toolbar.
The memory for that component is automatically allocated and at close of program deallocated.
But if you create your own. You need to allocate and deallocate memory.
Will will create are own objects (Class with memory).

To do this we need to add sqldb, SQLite3Conn, and sqlite3ds to uses line.

New we will create 3 variables under TForm1 = class(TForm).
DataSet: TSQLite3DataSet; Connect: TSQLite3Connection; and Trans: TSQLTransaction;

Above var line add const and DBFile = 'muliTabe.db';

New drop 2 buttons from Standard Tab in the toolbar.
Button1 > Caption = Create and Button2 > Caption = Exit.
New in the Object Inspector > Form1 > Tab Events > OnClick ... to right.
In the procedure TForm1.Button2Click(Sender: TObject); under begin add Form1.Close;

Save you work. File > Save.

Press F12, if needed, to display the form. And double click on the Create button.
procedure TForm1.Button1Click(Sender: TObject); under begin add.

This program create a database file. If the file already exist. There will be an error.
So if the file exist delete it.

This code create 2 objects. Connect and Trans.
Fill in 2 properties.
Connect.Transaction and Connect.DatabaseName from above.
Then it start the Transaction.

There is 3 things you need to know about SQL functions and Lazarus.
One:
SQL is a C liberty and C uses double quits for string and Pascal uses single quits.

Two:
How to access these C functions from Lazarus.
Will there a Free Pascal (Lazarus is a Free Pascal IDE) function for that.
ExecuteDirect() part of the TSQLite3Connection class.
So Connect.ExecuteDirect(' '); will work.

SQL Books and Lazarus.
SQL come with a command-line database compiler and many of the references use it's function.
So if you have a reference book. It probable uses these C functions.
For example: On page 164 of "Sams Teach Yourself SQL in 10 Minutes" (Lots of luck on that) it says.

Using the Pascal function you would writing like this.

In Pascal each string start and end with a ' (Single Quotation).
The + is to tell the compiler to add the next line as one line. So.
The compiler see the function as.

Pascal Connect.ExecuteDirect(' ');
C part is CREATE TABLE "Products"(prod_id CHAR(10) NOT NULL, vend_id CHAR(10) NOT NULL);

New we have created a Table (Products) and 2 columns prod_id, and vend_id.
But there no Data. Add Data.

New we add 2 value to the 2 columns. 1122 and 2211.
This is a way to use SQL books with Lazarus.


We create a table "customer" with 5 columns.
Roll INTEGER PRIMARY KEY,
Roll is the column name.
It is a INTEGER (a whole number),
PRIMARY KEY (uniquely identifies each record, roll).
You do not add this. It is automatically added.
Date_added DATE DEFAULT CURRENT_DATE
automatically add the date.
first_name Char(128) NOT NULL
Column's name first_name.
Can be upto 127 charters with a string terminator.
string terminator is automatically added.
NOT NULL Must have a value.

We need some Data in the rolls.

Note the space after ' and before VALUE. It is needed.
INSERT INTO "Table name" ( the 3 columns names).
' space VALUE( the 3 value that correspond to the columns).

Add indexes are used to speed up the time needed to search a Table.


CREATE UNIQUE INDEX "CRoll" ON "customer"("Roll");
We are CREATE a UNIQUE INDEX call CRoll. (no duplicate values allowed).
CREATE INDEX "CSSN" ON "customer"("SSN");
CREATE INDEX (not a UNIQUE one) called CSSN. Using Table customer and column SSN.
The Index name is what we use to search the table.


Trans.Commit;
Copy the data into the database file.
Trans.Free; and Connect.Free;
Free the heap memory used by the objects Trans and Connect.

The whole thing will look like this.