Install and use a SQLite database lazarus / 16-Databases ---------------------------------------------------------------------- mmvisual · 2007-01-07 15:05 Hello, I'm a beginner with Lazarus. I want develope a new application for Linux and windows. I think Windows is the smaller problen, so I want build de application in Linux with Lazarus. I have install Lazarus, and I can make a new projekt with a button, compile and start. Ok it's a very simple test. Now I want use a SQLite database in my application. Here begin my problems: I need a step by step help how I can make a database connection. - Witch component can I use (from Lazarus-folder? / URL?) - How I include this units - How I include the file "sqlite3.so" - The resolution must be a functional program without any bugs - other requirements (rpm's from Linux system??) - I use Databases in Delphi and I know TDataSet/TDatasource/TDBxxx- components Thank you very much, Markus ---------------------------------------------------------------------- mmvisual · 2007-01-07 20:32 I have found it, I post the complete project ... i hope it is usefully for you. I use this system now: - Suse Linux 10.2 - FPC2.0.4 - Lazarus 0.9.20 - SQLite 3.3.9 - Include the Units "/usr/lib/lazarus/components/sqlite/"" - All is now the newest... File "project1.lpr" Code: [Select] program project1; {$IFDEF FPC} {$mode objfpc}{$H+} {$ENDIF} uses   {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,   {$ENDIF}{$ENDIF}   Interfaces, // this includes the LCL widgetset   Forms   { add your units here }, fmain; begin   Application.Initialize;   Application.CreateForm(TfrmMain, frmMain);   Application.Run; end. File "project1.lpi" Code: [Select]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           ---------------------------------------------------------------------- mmvisual · 2007-01-07 20:36 The next code... File "fmain.pas" Code: [Select] unit fmain; {$IFDEF FPC} {$mode objfpc}{$H+} {$ENDIF} interface uses   {$IFDEF WIN32}   Windows,   {$ELSE}   Libc,   {$ENDIF}   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,   Buttons, StdCtrls, ExtCtrls,   DBGrids, DB,   sqlite3ds; type   { TfrmMain }   TfrmMain = class(TForm)     btnExecSQL: TButton;     btnShowTable: TButton;     dsTest: TDatasource;     grdTest: TDBGrid;     lstOld: TListBox;     edSQL: TMemo;     Panel1: TPanel;     Splitter1: TSplitter;     procedure btnExecSQLClick(Sender: TObject);     Procedure FormCreate(Sender: TObject);     Procedure FormDestroy(Sender: TObject);     Procedure btnShowTableClick(Sender: TObject);     Procedure lstOldDblClick(Sender: TObject);   private     { private declarations }     qryTest: TSqlite3Dataset;   public     { public declarations }     Procedure qryTestAfterOpen(DataSet: TDataSet);   end; var   frmMain: TfrmMain; implementation { TfrmMain } procedure TfrmMain.btnExecSQLClick(Sender: TObject); Var   i: Integer;   bExec: Boolean;   sSQL: String; begin   sSQL := StringReplace(edSQL.Text, #10, ' ', [rfReplaceAll]);   sSQL := StringReplace(sSQL, #13, ' ', [rfReplaceAll]);   sSQL := StringReplace(sSQL, '', ' ', [rfReplaceAll]);   For i := 0 To lstOld.Items.Count - 1 Do   Begin // Delete all old same query's from list     If SameText(lstOld.Items.Strings[i], sSQL) Then     Begin       lstOld.Items.Delete(i);       Break;     End;   End;   bExec := Not(Pos('SELECT', UpperCase(sSQL)) = 1); // Query for Execution?   If bExec Then     lstOld.Items.Insert(0, 'Exec: ' + sSQL) // Log Query   Else lstOld.Items.Insert(0, sSQL);   qryTest.Close;   If bExec Then     qryTest.ExecSQL(sSQL) // Exec Query   Else Begin     qryTest.SQL := sSQL; // Open SELECT Query     qryTest.Open;   End; end; Procedure TfrmMain.FormCreate(Sender: TObject); Begin   qryTest := TSqlite3Dataset.Create(Nil);   dsTest.DataSet := qryTest;   qryTest.FileName := 'Test.db';   qryTest.AfterOpen := @qryTestAfterOpen;   btnShowTableClick(Nil); end; Procedure TfrmMain.FormDestroy(Sender: TObject); Begin   dsTest.DataSet := Nil;   qryTest.Free; end; Procedure TfrmMain.btnShowTableClick(Sender: TObject); Begin   // Text for query all existing table names in database   edSQL.Text := 'SELECT tbl_name FROM (SELECT * FROM sqlite_master UNION ' +                 'ALL SELECT * FROM sqlite_temp_master) WHERE type!=''meta''' +                 'ORDER BY type DESC, name';   If Sender <> Nil Then      btnExecSQLClick(Nil); end; Procedure TfrmMain.lstOldDblClick(Sender: TObject); Begin   If lstOld.ItemIndex < 0 Then Exit;   If lstOld.Items.Count <= 0 Then Exit;   edSQL.Text := lstOld.Items[lstOld.ItemIndex];   If Pos('Exec: ', edSQL.Text) = 1 Then      edSQL.Text := Copy(edSQL.Text, 7, Length(edSQL.Text) - 7); end; Procedure TfrmMain.qryTestAfterOpen(DataSet: TDataSet); Var i: Integer; Begin   For i := 0 To (DataSet.FieldCount - 1) Do   Begin // Set Displaywidth for String fields     If DataSet.Fields[i].DataType = ftString Then     Begin       // This is now a bug from FPC:       //DataSet.Fields[i].DisplayWidth := 15’;     End;   End; End; initialization   {$I fmain.lrs} end. ---------------------------------------------------------------------- mmvisual · 2007-01-07 20:38 The next code... File "fmain.lfm" Code: [Select] object frmMain: TfrmMain   Left = 242   Height = 381   Top = 137   Width = 524   HorzScrollBar.Page = 523   VertScrollBar.Page = 380   ActiveControl = btnExecSQL   Caption = 'frmMain'   OnCreate = FormCreate   OnDestroy = FormDestroy   object Panel1: TPanel     Height = 96     Width = 524     Align = alTop     BevelOuter = bvNone     TabOrder = 0     object btnExecSQL: TButton       Left = 446       Height = 25       Top = 22       Width = 75       Anchors = [akTop, akRight]       BorderSpacing.InnerBorder = 4       Caption = 'ExecSQL'       OnClick = btnExecSQLClick       TabOrder = 0     end     object btnShowTable: TButton       Height = 17       Width = 112       BorderSpacing.InnerBorder = 4       Caption = 'ShowTable'       OnClick = btnShowTableClick       TabOrder = 1     end     object edSQL: TMemo       Left = 1       Height = 64       Top = 25       Width = 440       Anchors = [akTop, akLeft, akRight]       Lines.Strings = (         ''       )       ScrollBars = ssAutoBoth       TabOrder = 2     end   end   object Splitter1: TSplitter     Cursor = crVSplit     Height = 6     Top = 152     Width = 524     Align = alTop     Color = clBlack     ParentColor = False     ResizeAnchor = akTop   end   object grdTest: TDBGrid     Height = 223     Top = 158     Width = 524     Align = alClient     DataSource = dsTest     FixedColor = clBtnFace     Options = [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColumnMove, dgColLines, dgRowLines, dgTabs, dgAlwaysShowSelection, dgConfirmDelete, dgCancelOnExit]     OptionsExtra = [dgeAutoColumns, dgeCheckboxColumn]     ParentColor = False     TabOrder = 1     TabStop = True   end   object lstOld: TListBox     Height = 56     Top = 96     Width = 524     Align = alTop     OnDblClick = lstOldDblClick     TabOrder = 2     TopIndex = -1   end   object dsTest: TDatasource     left = 137     top = 67   end end ---------------------------------------------------------------------- mmvisual · 2007-01-07 20:39 The last file... File "fmain.lrs" Code: [Select] LazarusResources.Add('TfrmMain','FORMDATA',[   'TPF0'#8'TfrmMain'#7'frmMain'#4'Left'#3#242#0#6'Height'#3'}'#1#3'Top '#3#137#0   +#5'Width'#3#12#2#18'HorzScrollBar.Page'#3#11#2#18'VertScrollBar.Pag e'#3'|'#1   +#13'ActiveControl'#7#10'btnExecSQL'#7'Caption'#6#7'frmMain'#8'OnCre ate'#7#10   +'FormCreate'#9'OnDestroy'#7#11'FormDestroy'#0#6'TPanel'#6'Panel1'#6 'Height'#2   +'`'#5'Width'#3#12#2#5'Align'#7#5'alTop'#10'BevelOuter'#7#6'bvNone'# 8'TabOrde'   +'r'#2#0#0#7'TButton'#10'btnExecSQL'#4'Left'#3#190#1#6'Height'#2#25# 3'Top'#2   +#22#5'Width'#2'K'#7'Anchors'#11#5'akTop'#7'akRight'#0#25'BorderSpac ing.Inner'   +'Border'#2#4#7'Caption'#6#7'ExecSQL'#7'OnClick'#7#15'btnExecSQLClic k'#8'TabO'   +'rder'#2#0#0#0#7'TButton'#12'btnShowTable'#6'Height'#2#17#5'Width'# 2'p'#25'B'   +'orderSpacing.InnerBorder'#2#4#7'Caption'#6#9'ShowTable'#7'OnClick' #7#17'btn'   +'ShowTableClick'#8'TabOrder'#2#1#0#0#5'TMemo'#5'edSQL'#4'Left'#2#1# 6'Height'   +#2'@'#3'Top'#2#25#5'Width'#3#184#1#7'Anchors'#11#5'akTop'#6'akLeft' #7'akRigh'   +'t'#0#13'Lines.Strings'#1#6#0#0#10'ScrollBars'#7#10'ssAutoBoth'#8'T abOrder'#2   +#2#0#0#0#9'TSplitter'#9'Splitter1'#6'Cursor'#7#8'crVSplit'#6'Height '#2#6#3'T'   +'op'#3#152#0#5'Width'#3#12#2#5'Align'#7#5'alTop'#5'Color'#7#7'clBla ck'#11'Pa'   +'rentColor'#8#12'ResizeAnchor'#7#5'akTop'#0#0#7'TDBGrid'#7'grdTest' #6'Height'   +#3#223#0#3'Top'#3#158#0#5'Width'#3#12#2#5'Align'#7#8'alClient'#10'D ataSource'   +#7#6'dsTest'#10'FixedColor'#7#9'clBtnFace'#7'Options'#11#9'dgEditin g'#8'dgTi'   +'tles'#11'dgIndicator'#14'dgColumnResize'#12'dgColumnMove'#10'dgCol Lines'#10   +'dgRowLines'#6'dgTabs'#21'dgAlwaysShowSelection'#15'dgConfirmDelete '#14'dgCa'   +'ncelOnExit'#0#12'OptionsExtra'#11#14'dgeAutoColumns'#17'dgeCheckbo xColumn'#0   +#11'ParentColor'#8#8'TabOrder'#2#1#7'TabStop'#9#0#0#8'TListBox'#6'l stOld'#6   +'Height'#2'8'#3'Top'#2'`'#5'Width'#3#12#2#5'Align'#7#5'alTop'#10'On DblClick'   +#7#14'lstOldDblClick'#8'TabOrder'#2#2#8'TopIndex'#2#255#0#0#11'TDat asource'#6   +'dsTest'#4'left'#3#137#0#3'top'#2'C'#0#0#0 ]); ---------------------------------------------------------------------- mmvisual · 2007-01-07 20:52 The description: Copy the database library: On next, copy the library "sqlite3.so" into the same folder from code and rename it into "sqlite3". Compile and start the application with Lazarus. The function: - Memo-Text with you can type your SQL command - Button to Execute the SQL - List with the last commands - TDBGrid with show the result - Button with SQL Code for looking witch tabels exists in the database - Button ShowTable type a SQL string into the memo who you can show all tabels in database Start the Application: - Type in the memo:   CREATE TABEL testtbl (ID INTEGER, txt VARCHAR(100)) - Press the button >> it create a automaticly a new database file "Test.db" and a table "testbl" - Type in memo:   SELECT * FROM testtbl - Press the button >> See a empty tabel in the Grid - Type in memo:   INSERT INTO testbl (ID, text) VALUES(3, 'gugus') - Press the button - double click in the list entry "SELECT * FROM testtbl" - Press the button >> See a empty tabel in the Grid with one entry Known bugs: - You cannot compile the line "DataSet.Fields[ i ].DisplayWidth := 15;" but the code is right. - The Code "For i := 0 To lstOld.Items.Count - 1 Do" want seek and delete double entry from the list, but it do not work always good. - Why this bugs are inside, I do not know... Please test this code and write a feedback. Have a lot of fun, Markus.