diff --git a/Examples/Delphi/Delphi.dof b/Examples/Delphi/Delphi.dof new file mode 100644 index 0000000..1719ba2 --- /dev/null +++ b/Examples/Delphi/Delphi.dof @@ -0,0 +1,141 @@ +[FileVersion] +Version=7.0 +[Compiler] +A=8 +B=0 +C=1 +D=1 +E=0 +F=0 +G=1 +H=1 +I=1 +J=0 +K=0 +L=1 +M=0 +N=1 +O=1 +P=1 +Q=0 +R=0 +S=0 +T=0 +U=0 +V=1 +W=0 +X=1 +Y=1 +Z=1 +ShowHints=1 +ShowWarnings=1 +UnitAliases=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +NamespacePrefix= +SymbolDeprecated=1 +SymbolLibrary=1 +SymbolPlatform=1 +UnitLibrary=1 +UnitPlatform=1 +UnitDeprecated=1 +HResultCompat=1 +HidingMember=1 +HiddenVirtual=1 +Garbage=1 +BoundsError=1 +ZeroNilCompat=1 +StringConstTruncated=1 +ForLoopVarVarPar=1 +TypedConstVarPar=1 +AsgToTypedConst=1 +CaseLabelRange=1 +ForVariable=1 +ConstructingAbstract=1 +ComparisonFalse=1 +ComparisonTrue=1 +ComparingSignedUnsigned=1 +CombiningSignedUnsigned=1 +UnsupportedConstruct=1 +FileOpen=1 +FileOpenUnitSrc=1 +BadGlobalSymbol=1 +DuplicateConstructorDestructor=1 +InvalidDirective=1 +PackageNoLink=1 +PackageThreadVar=1 +ImplicitImport=1 +HPPEMITIgnored=1 +NoRetVal=1 +UseBeforeDef=1 +ForLoopVarUndef=1 +UnitNameMismatch=1 +NoCFGFileFound=1 +MessageDirective=1 +ImplicitVariants=1 +UnicodeToLocale=1 +LocaleToUnicode=1 +ImagebaseMultiple=1 +SuspiciousTypecast=1 +PrivatePropAccessor=1 +UnsafeType=0 +UnsafeCode=0 +UnsafeCast=0 +[Linker] +MapFile=0 +OutputObjs=0 +ConsoleApp=1 +DebugInfo=0 +RemoteSymbols=0 +MinStackSize=16384 +MaxStackSize=1048576 +ImageBase=4194304 +ExeDescription= +[Directories] +OutputDir=..\Bin\ +UnitOutputDir= +PackageDLLOutputDir= +PackageDCPOutputDir= +SearchPath=..\..\Source\ +Packages=vcl;rtl;vclx;VclSmp;vclshlctrls +Conditionals= +DebugSourceDirs= +UsePackages=0 +[Parameters] +RunParams= +HostApplication= +Launcher= +UseLauncher=0 +DebugCWD= +[Version Info] +IncludeVerInfo=0 +AutoIncBuild=0 +MajorVer=1 +MinorVer=0 +Release=0 +Build=0 +Debug=0 +PreRelease=0 +Special=0 +Private=0 +DLL=0 +Locale=1049 +CodePage=1251 +[Version Info Keys] +CompanyName= +FileDescription= +FileVersion=1.0.0.0 +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion=1.0.0.0 +Comments= +[HistoryLists\hlUnitAliases] +Count=1 +Item0=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +[HistoryLists\hlSearchPath] +Count=1 +Item0=..\..\Source\ +[HistoryLists\hlOutputDirectorry] +Count=1 +Item0=..\Bin\ diff --git a/Examples/Delphi/Delphi.dpr b/Examples/Delphi/Delphi.dpr new file mode 100644 index 0000000..90db53d --- /dev/null +++ b/Examples/Delphi/Delphi.dpr @@ -0,0 +1,13 @@ +program Delphi; + +uses + Forms, + Main in 'Main.pas' {Form_Main}; + +{$R *.res} + +begin + Application.Initialize; + Application.CreateForm(TForm_Main, Form_Main); + Application.Run; +end. diff --git a/Examples/Delphi/Main.dfm b/Examples/Delphi/Main.dfm new file mode 100644 index 0000000..d889d99 --- /dev/null +++ b/Examples/Delphi/Main.dfm @@ -0,0 +1,61 @@ +object Form_Main: TForm_Main + Left = 421 + Top = 198 + Width = 673 + Height = 434 + Caption = 'Delphi SQLite Example' + Color = clBtnFace + Font.Charset = DEFAULT_CHARSET + Font.Color = clWindowText + Font.Height = -11 + Font.Name = 'MS Sans Serif' + Font.Style = [] + OldCreateOrder = False + Position = poScreenCenter + PixelsPerInch = 96 + TextHeight = 13 + object Panel_Actions: TPanel + Left = 0 + Top = 0 + Width = 154 + Height = 396 + Align = alLeft + BevelOuter = bvNone + TabOrder = 0 + object Button_DB_Read: TButton + Left = 8 + Top = 48 + Width = 136 + Height = 33 + Caption = 'Read Database' + TabOrder = 1 + OnClick = Button_DB_ReadClick + end + object Button_DB_Create: TButton + Left = 8 + Top = 8 + Width = 136 + Height = 33 + Caption = 'Create Database' + TabOrder = 0 + OnClick = Button_DB_CreateClick + end + end + object Memo_Result: TMemo + Left = 154 + Top = 0 + Width = 503 + Height = 396 + Align = alClient + BorderStyle = bsNone + Color = 4207920 + Font.Charset = DEFAULT_CHARSET + Font.Color = 16773344 + Font.Height = -11 + Font.Name = 'MS Sans Serif' + Font.Style = [] + ParentFont = False + ScrollBars = ssVertical + TabOrder = 1 + end +end diff --git a/Examples/Delphi/Main.pas b/Examples/Delphi/Main.pas new file mode 100644 index 0000000..ee2c2d8 --- /dev/null +++ b/Examples/Delphi/Main.pas @@ -0,0 +1,217 @@ +unit Main; + +interface + +uses + Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, + Dialogs, StdCtrls, ExtCtrls; + +type + TForm_Main = class(TForm) + Panel_Actions: TPanel; + Memo_Result: TMemo; + Button_DB_Create: TButton; + Button_DB_Read: TButton; + procedure Button_DB_CreateClick(Sender: TObject); + procedure Button_DB_ReadClick(Sender: TObject); + end; + +var + Form_Main: TForm_Main; + +implementation + +uses + SQLite3, SQLite3Wrap; + +{$R *.dfm} + +procedure TForm_Main.Button_DB_CreateClick(Sender: TObject); +var + DB: TSQLite3Database; + Stmt: TSQLite3Statement; + IDs: array[1..6] of Integer; +begin + // Delete database if it already exists + DeleteFile('artists.db'); + + // Create datatbase and fill it with example data + DB := TSQLite3Database.Create; + try + DB.Open('artists.db'); + + // Create table "artists" + DB.Execute('CREATE TABLE artists (name TEXT, born REAL, died REAL)'); + + // Fill the table with artists + Stmt := DB.Prepare('INSERT INTO artists (name, born, died) VALUES (?, ?, ?)'); + try + Stmt.BindText (1, 'Leonardo da Vinci'); + Stmt.BindDouble(2, EncodeDate(1452, 4, 15)); + Stmt.BindDouble(3, EncodeDate(1519, 5, 2)); + + Stmt.StepAndReset; // StepAndReset executes a prepared statement + // and resets it so we can reuse it again + + IDs[1] := DB.LastInsertRowID; // Save newly added artist's ID to use it + // when filling "paintings" table below + + Stmt.BindText (1, 'Raphael'); + Stmt.BindDouble(2, EncodeDate(1483, 3, 28)); + Stmt.BindDouble(3, EncodeDate(1520, 4, 6)); + Stmt.StepAndReset; + IDs[2] := DB.LastInsertRowID; + + Stmt.BindText (1, 'Arkhip Kuindzhi'); + Stmt.BindDouble(2, EncodeDate(1842, 1, 27)); + Stmt.BindDouble(3, EncodeDate(1898, 7, 24)); + Stmt.StepAndReset; + IDs[3] := DB.LastInsertRowID; + + Stmt.BindText (1, 'Nicholas Roerich'); + Stmt.BindDouble(2, EncodeDate(1874, 10, 9)); + Stmt.BindDouble(3, EncodeDate(1947, 12, 13)); + Stmt.StepAndReset; + IDs[4] := DB.LastInsertRowID; + + Stmt.BindText (1, 'Ivan Aivazovsky'); + Stmt.BindDouble(2, EncodeDate(1817, 7, 29)); + Stmt.BindDouble(3, EncodeDate(1900, 5, 5)); + Stmt.StepAndReset; + IDs[5] := DB.LastInsertRowID; + + Stmt.BindText (1, 'Ivan Shishkin'); + Stmt.BindDouble(2, EncodeDate(1832, 1, 25)); + Stmt.BindDouble(3, EncodeDate(1898, 3, 20)); + Stmt.StepAndReset; + IDs[6] := DB.LastInsertRowID; + finally + Stmt.Free; + end; + + // Create table "paintings" + DB.Execute('CREATE TABLE paintings (title TEXT, year INTEGER, artist INTEGER)'); + + // Fill the table with paintings info + Stmt := DB.Prepare('INSERT INTO paintings (title, year, artist) VALUES (?, ?, ?)'); + try + // Leonardo da Vinci + Stmt.BindText(1, 'The Virgin and Child with St. Anne'); + Stmt.BindInt (2, 1508); + Stmt.BindInt (3, IDs[1]); + Stmt.StepAndReset; + + Stmt.BindText(1, 'Mona Lisa'); + Stmt.BindInt (2, 1519); + Stmt.BindInt (3, IDs[1]); + Stmt.StepAndReset; + + // Raphael + Stmt.BindText(1, 'Sistine Madonna'); + Stmt.BindInt (2, 1514); + Stmt.BindInt (3, IDs[2]); + Stmt.StepAndReset; + + Stmt.BindText(1, 'Transfiguration'); + Stmt.BindInt (2, 1520); + Stmt.BindInt (3, IDs[2]); + Stmt.StepAndReset; + + // Arkhip Kuindzhi + Stmt.BindText(1, 'After a rain'); + Stmt.BindInt (2, 1879); + Stmt.BindInt (3, IDs[3]); + Stmt.StepAndReset; + + Stmt.BindText(1, 'Elbrus'); + Stmt.BindInt (2, 1895); + Stmt.BindInt (3, IDs[3]); + Stmt.StepAndReset; + + // Nicholas Roerich + Stmt.BindText(1, 'To Kailas. Lahul'); + Stmt.BindInt (2, 1932); + Stmt.BindInt (3, IDs[4]); + Stmt.StepAndReset; + + Stmt.BindText(1, 'Krishna'); + Stmt.BindInt (2, 1929); + Stmt.BindInt (3, IDs[4]); + Stmt.StepAndReset; + + // Ivan Aivazovsky + Stmt.BindText(1, 'The Mary Caught in a Storm'); + Stmt.BindInt (2, 1892); + Stmt.BindInt (3, IDs[5]); + Stmt.StepAndReset; + + Stmt.BindText(1, 'Brig "Mercury" Attacked by Two Turkish Ships'); + Stmt.BindInt (2, 1892); + Stmt.BindInt (3, IDs[5]); + Stmt.StepAndReset; + + // Ivan Shishkin + Stmt.BindText(1, 'Morning in a Pine Forest'); + Stmt.BindInt (2, 1889); + Stmt.BindInt (3, IDs[6]); + Stmt.StepAndReset; + + Stmt.BindText(1, 'Wood Distances'); + Stmt.BindInt (2, 1884); + Stmt.BindInt (3, IDs[6]); + Stmt.StepAndReset; + finally + Stmt.Free; + end; + finally + DB.Free; + end; +end; + +procedure TForm_Main.Button_DB_ReadClick(Sender: TObject); +var + DB: TSQLite3Database; + Stmt_Artists, + Stmt_Paintings: TSQLite3Statement; +begin + if not FileExists('artists.db') then + begin + ShowMessage('The database does not exist. Please create one.'); + Exit; + end; + + DB := TSQLite3Database.Create; + try + DB.Open('artists.db'); + + // Show all artists and their paintings + Stmt_Artists := DB.Prepare('SELECT rowid, name, born, died FROM artists ORDER BY born'); + Stmt_Paintings := DB.Prepare('SELECT title, year FROM paintings WHERE artist = ? ORDER BY year'); + try + while Stmt_Artists.Step = SQLITE_ROW do + begin + Memo_Result.Lines.Add(Stmt_Artists.ColumnText(1)); + Memo_Result.Lines.Add(DateToStr(Stmt_Artists.ColumnDouble(2)) + ' - ' + DateToStr(Stmt_Artists.ColumnDouble(3))); + + Memo_Result.Lines.Add('paintings:'); + Stmt_Paintings.BindInt(1, Stmt_Artists.ColumnInt(0)); + while Stmt_Paintings.Step = SQLITE_ROW do + Memo_Result.Lines.Add(' ' + Stmt_Paintings.ColumnText(0) + ' (' + Stmt_Paintings.ColumnText(1) + ')'); + Stmt_Paintings.Reset; + + Memo_Result.Lines.Add(''); + end; + finally + Stmt_Paintings.Free; + Stmt_Artists.Free; + end; + finally + DB.Free; + end; + + // Add separator + Memo_Result.Lines.Add('------------------------------------------------'); + Memo_Result.Lines.Add(''); +end; + +end.