├── Examples ├── Bin │ └── sqlite3.dll ├── Delphi │ ├── Delphi.dof │ ├── Delphi.dpr │ ├── Main.dfm │ └── Main.pas └── Lazarus │ ├── lazarus.ico │ ├── lazarus.lpi │ ├── lazarus.lpr │ ├── main.lfm │ └── main.pas ├── LICENSE.md ├── README.md └── Source ├── SQLite3.pas ├── SQLite3Utils.pas └── SQLite3Wrap.pas /Examples/Bin/sqlite3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plashenkov/SQLite3-Delphi-FPC/4b942a245a962cec3bd3fc7d8912d01d3170ccf4/Examples/Bin/sqlite3.dll -------------------------------------------------------------------------------- /Examples/Delphi/Delphi.dof: -------------------------------------------------------------------------------- 1 | [FileVersion] 2 | Version=7.0 3 | [Compiler] 4 | A=8 5 | B=0 6 | C=1 7 | D=1 8 | E=0 9 | F=0 10 | G=1 11 | H=1 12 | I=1 13 | J=0 14 | K=0 15 | L=1 16 | M=0 17 | N=1 18 | O=1 19 | P=1 20 | Q=0 21 | R=0 22 | S=0 23 | T=0 24 | U=0 25 | V=1 26 | W=0 27 | X=1 28 | Y=1 29 | Z=1 30 | ShowHints=1 31 | ShowWarnings=1 32 | UnitAliases=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; 33 | NamespacePrefix= 34 | SymbolDeprecated=1 35 | SymbolLibrary=1 36 | SymbolPlatform=1 37 | UnitLibrary=1 38 | UnitPlatform=1 39 | UnitDeprecated=1 40 | HResultCompat=1 41 | HidingMember=1 42 | HiddenVirtual=1 43 | Garbage=1 44 | BoundsError=1 45 | ZeroNilCompat=1 46 | StringConstTruncated=1 47 | ForLoopVarVarPar=1 48 | TypedConstVarPar=1 49 | AsgToTypedConst=1 50 | CaseLabelRange=1 51 | ForVariable=1 52 | ConstructingAbstract=1 53 | ComparisonFalse=1 54 | ComparisonTrue=1 55 | ComparingSignedUnsigned=1 56 | CombiningSignedUnsigned=1 57 | UnsupportedConstruct=1 58 | FileOpen=1 59 | FileOpenUnitSrc=1 60 | BadGlobalSymbol=1 61 | DuplicateConstructorDestructor=1 62 | InvalidDirective=1 63 | PackageNoLink=1 64 | PackageThreadVar=1 65 | ImplicitImport=1 66 | HPPEMITIgnored=1 67 | NoRetVal=1 68 | UseBeforeDef=1 69 | ForLoopVarUndef=1 70 | UnitNameMismatch=1 71 | NoCFGFileFound=1 72 | MessageDirective=1 73 | ImplicitVariants=1 74 | UnicodeToLocale=1 75 | LocaleToUnicode=1 76 | ImagebaseMultiple=1 77 | SuspiciousTypecast=1 78 | PrivatePropAccessor=1 79 | UnsafeType=0 80 | UnsafeCode=0 81 | UnsafeCast=0 82 | [Linker] 83 | MapFile=0 84 | OutputObjs=0 85 | ConsoleApp=1 86 | DebugInfo=0 87 | RemoteSymbols=0 88 | MinStackSize=16384 89 | MaxStackSize=1048576 90 | ImageBase=4194304 91 | ExeDescription= 92 | [Directories] 93 | OutputDir=..\Bin\ 94 | UnitOutputDir= 95 | PackageDLLOutputDir= 96 | PackageDCPOutputDir= 97 | SearchPath=..\..\Source\ 98 | Packages=vcl;rtl;vclx;VclSmp;vclshlctrls 99 | Conditionals= 100 | DebugSourceDirs= 101 | UsePackages=0 102 | [Parameters] 103 | RunParams= 104 | HostApplication= 105 | Launcher= 106 | UseLauncher=0 107 | DebugCWD= 108 | [Version Info] 109 | IncludeVerInfo=0 110 | AutoIncBuild=0 111 | MajorVer=1 112 | MinorVer=0 113 | Release=0 114 | Build=0 115 | Debug=0 116 | PreRelease=0 117 | Special=0 118 | Private=0 119 | DLL=0 120 | Locale=1049 121 | CodePage=1251 122 | [Version Info Keys] 123 | CompanyName= 124 | FileDescription= 125 | FileVersion=1.0.0.0 126 | InternalName= 127 | LegalCopyright= 128 | LegalTrademarks= 129 | OriginalFilename= 130 | ProductName= 131 | ProductVersion=1.0.0.0 132 | Comments= 133 | [HistoryLists\hlUnitAliases] 134 | Count=1 135 | Item0=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; 136 | [HistoryLists\hlSearchPath] 137 | Count=1 138 | Item0=..\..\Source\ 139 | [HistoryLists\hlOutputDirectorry] 140 | Count=1 141 | Item0=..\Bin\ 142 | -------------------------------------------------------------------------------- /Examples/Delphi/Delphi.dpr: -------------------------------------------------------------------------------- 1 | program Delphi; 2 | 3 | uses 4 | Forms, 5 | Main in 'Main.pas' {Form_Main}; 6 | 7 | {$R *.res} 8 | 9 | begin 10 | Application.Initialize; 11 | Application.CreateForm(TForm_Main, Form_Main); 12 | Application.Run; 13 | end. 14 | -------------------------------------------------------------------------------- /Examples/Delphi/Main.dfm: -------------------------------------------------------------------------------- 1 | object Form_Main: TForm_Main 2 | Left = 421 3 | Top = 198 4 | Width = 673 5 | Height = 434 6 | Caption = 'Delphi SQLite Example' 7 | Color = clBtnFace 8 | Font.Charset = DEFAULT_CHARSET 9 | Font.Color = clWindowText 10 | Font.Height = -11 11 | Font.Name = 'MS Sans Serif' 12 | Font.Style = [] 13 | OldCreateOrder = False 14 | Position = poScreenCenter 15 | PixelsPerInch = 96 16 | TextHeight = 13 17 | object Panel_Actions: TPanel 18 | Left = 0 19 | Top = 0 20 | Width = 154 21 | Height = 396 22 | Align = alLeft 23 | BevelOuter = bvNone 24 | TabOrder = 0 25 | object Button_DB_Read: TButton 26 | Left = 8 27 | Top = 48 28 | Width = 136 29 | Height = 33 30 | Caption = 'Read Database' 31 | TabOrder = 1 32 | OnClick = Button_DB_ReadClick 33 | end 34 | object Button_DB_Create: TButton 35 | Left = 8 36 | Top = 8 37 | Width = 136 38 | Height = 33 39 | Caption = 'Create Database' 40 | TabOrder = 0 41 | OnClick = Button_DB_CreateClick 42 | end 43 | end 44 | object Memo_Result: TMemo 45 | Left = 154 46 | Top = 0 47 | Width = 503 48 | Height = 396 49 | Align = alClient 50 | BorderStyle = bsNone 51 | Color = 4207920 52 | Font.Charset = DEFAULT_CHARSET 53 | Font.Color = 16773344 54 | Font.Height = -11 55 | Font.Name = 'MS Sans Serif' 56 | Font.Style = [] 57 | ParentFont = False 58 | ScrollBars = ssVertical 59 | TabOrder = 1 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /Examples/Delphi/Main.pas: -------------------------------------------------------------------------------- 1 | unit Main; 2 | 3 | interface 4 | 5 | uses 6 | Windows, SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; 7 | 8 | type 9 | TForm_Main = class(TForm) 10 | Panel_Actions: TPanel; 11 | Memo_Result: TMemo; 12 | Button_DB_Create: TButton; 13 | Button_DB_Read: TButton; 14 | procedure Button_DB_CreateClick(Sender: TObject); 15 | procedure Button_DB_ReadClick(Sender: TObject); 16 | end; 17 | 18 | var 19 | Form_Main: TForm_Main; 20 | 21 | implementation 22 | 23 | uses 24 | SQLite3, SQLite3Wrap; 25 | 26 | {$R *.dfm} 27 | 28 | procedure TForm_Main.Button_DB_CreateClick(Sender: TObject); 29 | var 30 | DB: TSQLite3Database; 31 | Stmt: TSQLite3Statement; 32 | IDs: array[1..6] of Integer; 33 | begin 34 | // Delete database if it already exists 35 | DeleteFile('artists.db'); 36 | 37 | // Create database and fill it with example data 38 | Screen.Cursor := crHourGlass; 39 | DB := TSQLite3Database.Create; 40 | try 41 | DB.Open('artists.db'); 42 | 43 | // Create table "artists" 44 | DB.Execute('CREATE TABLE artists (name TEXT, born REAL, died REAL)'); 45 | 46 | // Fill the table with artists 47 | Stmt := DB.Prepare('INSERT INTO artists (name, born, died) VALUES (?, ?, ?)'); 48 | try 49 | Stmt.BindText (1, 'Leonardo da Vinci'); 50 | Stmt.BindDouble(2, EncodeDate(1452, 4, 15)); 51 | Stmt.BindDouble(3, EncodeDate(1519, 5, 2)); 52 | 53 | Stmt.StepAndReset; // StepAndReset executes a prepared statement 54 | // and resets it so we can reuse it again 55 | 56 | IDs[1] := DB.LastInsertRowID; // Save newly added artist's ID to use it 57 | // when filling "paintings" table below 58 | 59 | Stmt.BindText (1, 'Raphael'); 60 | Stmt.BindDouble(2, EncodeDate(1483, 3, 28)); 61 | Stmt.BindDouble(3, EncodeDate(1520, 4, 6)); 62 | Stmt.StepAndReset; 63 | IDs[2] := DB.LastInsertRowID; 64 | 65 | Stmt.BindText (1, 'Arkhip Kuindzhi'); 66 | Stmt.BindDouble(2, EncodeDate(1842, 1, 27)); 67 | Stmt.BindDouble(3, EncodeDate(1898, 7, 24)); 68 | Stmt.StepAndReset; 69 | IDs[3] := DB.LastInsertRowID; 70 | 71 | Stmt.BindText (1, 'Nicholas Roerich'); 72 | Stmt.BindDouble(2, EncodeDate(1874, 10, 9)); 73 | Stmt.BindDouble(3, EncodeDate(1947, 12, 13)); 74 | Stmt.StepAndReset; 75 | IDs[4] := DB.LastInsertRowID; 76 | 77 | Stmt.BindText (1, 'Ivan Aivazovsky'); 78 | Stmt.BindDouble(2, EncodeDate(1817, 7, 29)); 79 | Stmt.BindDouble(3, EncodeDate(1900, 5, 5)); 80 | Stmt.StepAndReset; 81 | IDs[5] := DB.LastInsertRowID; 82 | 83 | Stmt.BindText (1, 'Ivan Shishkin'); 84 | Stmt.BindDouble(2, EncodeDate(1832, 1, 25)); 85 | Stmt.BindDouble(3, EncodeDate(1898, 3, 20)); 86 | Stmt.StepAndReset; 87 | IDs[6] := DB.LastInsertRowID; 88 | finally 89 | Stmt.Free; 90 | end; 91 | 92 | // Create table "paintings" 93 | DB.Execute('CREATE TABLE paintings (title TEXT, year INTEGER, artist INTEGER)'); 94 | 95 | // Fill the table with paintings info 96 | Stmt := DB.Prepare('INSERT INTO paintings (title, year, artist) VALUES (?, ?, ?)'); 97 | try 98 | // Leonardo da Vinci 99 | Stmt.BindText(1, 'The Virgin and Child with St. Anne'); 100 | Stmt.BindInt (2, 1508); 101 | Stmt.BindInt (3, IDs[1]); 102 | Stmt.StepAndReset; 103 | 104 | Stmt.BindText(1, 'Mona Lisa'); 105 | Stmt.BindInt (2, 1519); 106 | Stmt.BindInt (3, IDs[1]); 107 | Stmt.StepAndReset; 108 | 109 | // Raphael 110 | Stmt.BindText(1, 'Sistine Madonna'); 111 | Stmt.BindInt (2, 1514); 112 | Stmt.BindInt (3, IDs[2]); 113 | Stmt.StepAndReset; 114 | 115 | Stmt.BindText(1, 'Transfiguration'); 116 | Stmt.BindInt (2, 1520); 117 | Stmt.BindInt (3, IDs[2]); 118 | Stmt.StepAndReset; 119 | 120 | // Arkhip Kuindzhi 121 | Stmt.BindText(1, 'After a rain'); 122 | Stmt.BindInt (2, 1879); 123 | Stmt.BindInt (3, IDs[3]); 124 | Stmt.StepAndReset; 125 | 126 | Stmt.BindText(1, 'Elbrus'); 127 | Stmt.BindInt (2, 1895); 128 | Stmt.BindInt (3, IDs[3]); 129 | Stmt.StepAndReset; 130 | 131 | // Nicholas Roerich 132 | Stmt.BindText(1, 'To Kailas. Lahul'); 133 | Stmt.BindInt (2, 1932); 134 | Stmt.BindInt (3, IDs[4]); 135 | Stmt.StepAndReset; 136 | 137 | Stmt.BindText(1, 'Krishna'); 138 | Stmt.BindInt (2, 1929); 139 | Stmt.BindInt (3, IDs[4]); 140 | Stmt.StepAndReset; 141 | 142 | // Ivan Aivazovsky 143 | Stmt.BindText(1, 'The Mary Caught in a Storm'); 144 | Stmt.BindInt (2, 1892); 145 | Stmt.BindInt (3, IDs[5]); 146 | Stmt.StepAndReset; 147 | 148 | Stmt.BindText(1, 'Brig "Mercury" Attacked by Two Turkish Ships'); 149 | Stmt.BindInt (2, 1892); 150 | Stmt.BindInt (3, IDs[5]); 151 | Stmt.StepAndReset; 152 | 153 | // Ivan Shishkin 154 | Stmt.BindText(1, 'Morning in a Pine Forest'); 155 | Stmt.BindInt (2, 1889); 156 | Stmt.BindInt (3, IDs[6]); 157 | Stmt.StepAndReset; 158 | 159 | Stmt.BindText(1, 'Wood Distances'); 160 | Stmt.BindInt (2, 1884); 161 | Stmt.BindInt (3, IDs[6]); 162 | Stmt.StepAndReset; 163 | finally 164 | Stmt.Free; 165 | end; 166 | 167 | ShowMessage('Database created.'); 168 | finally 169 | DB.Free; 170 | Screen.Cursor := crDefault; 171 | end; 172 | end; 173 | 174 | procedure TForm_Main.Button_DB_ReadClick(Sender: TObject); 175 | var 176 | DB: TSQLite3Database; 177 | Stmt_Artists, 178 | Stmt_Paintings: TSQLite3Statement; 179 | begin 180 | if not FileExists('artists.db') then 181 | begin 182 | ShowMessage('The database does not exist. Please create one.'); 183 | Exit; 184 | end; 185 | 186 | DB := TSQLite3Database.Create; 187 | try 188 | DB.Open('artists.db'); 189 | 190 | // Show all artists and their paintings 191 | Stmt_Artists := DB.Prepare('SELECT rowid, name, born, died FROM artists ORDER BY born'); 192 | Stmt_Paintings := DB.Prepare('SELECT title, year FROM paintings WHERE artist = ? ORDER BY year'); 193 | try 194 | while Stmt_Artists.Step = SQLITE_ROW do 195 | begin 196 | Memo_Result.Lines.Add(Stmt_Artists.ColumnText(1)); 197 | Memo_Result.Lines.Add(DateToStr(Stmt_Artists.ColumnDouble(2)) + ' - ' + DateToStr(Stmt_Artists.ColumnDouble(3))); 198 | 199 | Memo_Result.Lines.Add('paintings:'); 200 | Stmt_Paintings.BindInt(1, Stmt_Artists.ColumnInt(0)); 201 | while Stmt_Paintings.Step = SQLITE_ROW do 202 | Memo_Result.Lines.Add(' ' + Stmt_Paintings.ColumnText(0) + ' (' + Stmt_Paintings.ColumnText(1) + ')'); 203 | Stmt_Paintings.Reset; 204 | 205 | Memo_Result.Lines.Add(''); 206 | end; 207 | finally 208 | Stmt_Paintings.Free; 209 | Stmt_Artists.Free; 210 | end; 211 | finally 212 | DB.Free; 213 | end; 214 | 215 | // Add separator 216 | Memo_Result.Lines.Add('------------------------------------------------'); 217 | Memo_Result.Lines.Add(''); 218 | end; 219 | 220 | end. 221 | -------------------------------------------------------------------------------- /Examples/Lazarus/lazarus.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plashenkov/SQLite3-Delphi-FPC/4b942a245a962cec3bd3fc7d8912d01d3170ccf4/Examples/Lazarus/lazarus.ico -------------------------------------------------------------------------------- /Examples/Lazarus/lazarus.lpi: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | <ResourceType Value="res"/> 11 | <UseXPManifest Value="True"/> 12 | <Icon Value="0"/> 13 | </General> 14 | <i18n> 15 | <EnableI18N LFM="False"/> 16 | </i18n> 17 | <VersionInfo> 18 | <StringTable ProductVersion=""/> 19 | </VersionInfo> 20 | <BuildModes Count="1"> 21 | <Item1 Name="Default" Default="True"/> 22 | </BuildModes> 23 | <PublishOptions> 24 | <Version Value="2"/> 25 | <IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/> 26 | <ExcludeFileFilter Value="*.(bak|ppu|o|so);*~;backup"/> 27 | </PublishOptions> 28 | <RunParams> 29 | <local> 30 | <FormatVersion Value="1"/> 31 | </local> 32 | </RunParams> 33 | <RequiredPackages Count="1"> 34 | <Item1> 35 | <PackageName Value="LCL"/> 36 | </Item1> 37 | </RequiredPackages> 38 | <Units Count="2"> 39 | <Unit0> 40 | <Filename Value="lazarus.lpr"/> 41 | <IsPartOfProject Value="True"/> 42 | <UnitName Value="lazarus"/> 43 | </Unit0> 44 | <Unit1> 45 | <Filename Value="main.pas"/> 46 | <IsPartOfProject Value="True"/> 47 | <ComponentName Value="Form_Main"/> 48 | <HasResources Value="True"/> 49 | <ResourceBaseClass Value="Form"/> 50 | <UnitName Value="Main"/> 51 | </Unit1> 52 | </Units> 53 | </ProjectOptions> 54 | <CompilerOptions> 55 | <Version Value="11"/> 56 | <PathDelim Value="\"/> 57 | <Target> 58 | <Filename Value="../Bin/lazarus"/> 59 | </Target> 60 | <SearchPaths> 61 | <IncludeFiles Value="$(ProjOutDir)"/> 62 | <OtherUnitFiles Value="../../Source"/> 63 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 64 | </SearchPaths> 65 | <Linking> 66 | <Debugging> 67 | <GenerateDebugInfo Value="False"/> 68 | </Debugging> 69 | <Options> 70 | <Win32> 71 | <GraphicApplication Value="True"/> 72 | </Win32> 73 | </Options> 74 | </Linking> 75 | <Other> 76 | <CompilerMessages> 77 | <MsgFileName Value=""/> 78 | </CompilerMessages> 79 | <CompilerPath Value="$(CompPath)"/> 80 | </Other> 81 | </CompilerOptions> 82 | <Debugging> 83 | <Exceptions Count="3"> 84 | <Item1> 85 | <Name Value="EAbort"/> 86 | </Item1> 87 | <Item2> 88 | <Name Value="ECodetoolError"/> 89 | </Item2> 90 | <Item3> 91 | <Name Value="EFOpenError"/> 92 | </Item3> 93 | </Exceptions> 94 | </Debugging> 95 | </CONFIG> 96 | -------------------------------------------------------------------------------- /Examples/Lazarus/lazarus.lpr: -------------------------------------------------------------------------------- 1 | program Lazarus; 2 | 3 | {$mode objfpc}{$H+} 4 | 5 | uses 6 | {$IFDEF UNIX}{$IFDEF UseCThreads} 7 | cthreads, 8 | {$ENDIF}{$ENDIF} 9 | Interfaces, // this includes the LCL widgetset 10 | Forms, Main 11 | { you can add units after this }; 12 | 13 | {$R *.res} 14 | 15 | begin 16 | RequireDerivedFormResource := True; 17 | Application.Initialize; 18 | Application.CreateForm(TForm_Main, Form_Main); 19 | Application.Run; 20 | end. 21 | 22 | -------------------------------------------------------------------------------- /Examples/Lazarus/main.lfm: -------------------------------------------------------------------------------- 1 | object Form_Main: TForm_Main 2 | Left = 394 3 | Height = 396 4 | Top = 139 5 | Width = 657 6 | Caption = 'Lazarus SQLite Example' 7 | ClientHeight = 396 8 | ClientWidth = 657 9 | Position = poScreenCenter 10 | LCLVersion = '1.0.12.0' 11 | object Memo_Result: TMemo 12 | Left = 154 13 | Height = 396 14 | Top = 0 15 | Width = 503 16 | Align = alClient 17 | BorderStyle = bsNone 18 | Color = 4207920 19 | Font.Color = 16773344 20 | ParentFont = False 21 | ScrollBars = ssVertical 22 | TabOrder = 0 23 | end 24 | object Panel_Actions: TPanel 25 | Left = 0 26 | Height = 396 27 | Top = 0 28 | Width = 154 29 | Align = alLeft 30 | BevelOuter = bvNone 31 | ClientHeight = 396 32 | ClientWidth = 154 33 | TabOrder = 1 34 | object Button_DB_Read: TButton 35 | Left = 8 36 | Height = 33 37 | Top = 48 38 | Width = 136 39 | Caption = 'Read Database' 40 | OnClick = Button_DB_ReadClick 41 | TabOrder = 0 42 | end 43 | object Button_DB_Create: TButton 44 | Left = 8 45 | Height = 33 46 | Top = 8 47 | Width = 136 48 | Caption = 'Create Database' 49 | OnClick = Button_DB_CreateClick 50 | TabOrder = 1 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /Examples/Lazarus/main.pas: -------------------------------------------------------------------------------- 1 | unit Main; 2 | 3 | {$MODE DELPHI} 4 | 5 | interface 6 | 7 | uses 8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 | ExtCtrls; 10 | 11 | type 12 | 13 | { TForm_Main } 14 | 15 | TForm_Main = class(TForm) 16 | Panel_Actions: TPanel; 17 | Button_DB_Read: TButton; 18 | Button_DB_Create: TButton; 19 | Memo_Result: TMemo; 20 | procedure Button_DB_CreateClick(Sender: TObject); 21 | procedure Button_DB_ReadClick(Sender: TObject); 22 | end; 23 | 24 | var 25 | Form_Main: TForm_Main; 26 | 27 | implementation 28 | 29 | uses 30 | SQLite3, SQLite3Wrap; 31 | 32 | {$R *.lfm} 33 | 34 | { TForm_Main } 35 | 36 | procedure TForm_Main.Button_DB_CreateClick(Sender: TObject); 37 | var 38 | DB: TSQLite3Database; 39 | Stmt: TSQLite3Statement; 40 | IDs: array[1..6] of Integer; 41 | begin 42 | // Delete database if it already exists 43 | DeleteFile('artists.db'); 44 | 45 | // Create database and fill it with example data 46 | Screen.Cursor := crHourGlass; 47 | DB := TSQLite3Database.Create; 48 | try 49 | DB.Open('artists.db'); 50 | 51 | // Create table "artists" 52 | DB.Execute('CREATE TABLE artists (name TEXT, born REAL, died REAL)'); 53 | 54 | // Fill the table with artists 55 | Stmt := DB.Prepare('INSERT INTO artists (name, born, died) VALUES (?, ?, ?)'); 56 | try 57 | Stmt.BindText (1, 'Leonardo da Vinci'); 58 | Stmt.BindDouble(2, EncodeDate(1452, 4, 15)); 59 | Stmt.BindDouble(3, EncodeDate(1519, 5, 2)); 60 | 61 | Stmt.StepAndReset; // StepAndReset executes a prepared statement 62 | // and resets it so we can reuse it again 63 | 64 | IDs[1] := DB.LastInsertRowID; // Save newly added artist's ID to use it 65 | // when filling "paintings" table below 66 | 67 | Stmt.BindText (1, 'Raphael'); 68 | Stmt.BindDouble(2, EncodeDate(1483, 3, 28)); 69 | Stmt.BindDouble(3, EncodeDate(1520, 4, 6)); 70 | Stmt.StepAndReset; 71 | IDs[2] := DB.LastInsertRowID; 72 | 73 | Stmt.BindText (1, 'Arkhip Kuindzhi'); 74 | Stmt.BindDouble(2, EncodeDate(1842, 1, 27)); 75 | Stmt.BindDouble(3, EncodeDate(1898, 7, 24)); 76 | Stmt.StepAndReset; 77 | IDs[3] := DB.LastInsertRowID; 78 | 79 | Stmt.BindText (1, 'Nicholas Roerich'); 80 | Stmt.BindDouble(2, EncodeDate(1874, 10, 9)); 81 | Stmt.BindDouble(3, EncodeDate(1947, 12, 13)); 82 | Stmt.StepAndReset; 83 | IDs[4] := DB.LastInsertRowID; 84 | 85 | Stmt.BindText (1, 'Ivan Aivazovsky'); 86 | Stmt.BindDouble(2, EncodeDate(1817, 7, 29)); 87 | Stmt.BindDouble(3, EncodeDate(1900, 5, 5)); 88 | Stmt.StepAndReset; 89 | IDs[5] := DB.LastInsertRowID; 90 | 91 | Stmt.BindText (1, 'Ivan Shishkin'); 92 | Stmt.BindDouble(2, EncodeDate(1832, 1, 25)); 93 | Stmt.BindDouble(3, EncodeDate(1898, 3, 20)); 94 | Stmt.StepAndReset; 95 | IDs[6] := DB.LastInsertRowID; 96 | finally 97 | Stmt.Free; 98 | end; 99 | 100 | // Create table "paintings" 101 | DB.Execute('CREATE TABLE paintings (title TEXT, year INTEGER, artist INTEGER)'); 102 | 103 | // Fill the table with paintings info 104 | Stmt := DB.Prepare('INSERT INTO paintings (title, year, artist) VALUES (?, ?, ?)'); 105 | try 106 | // Leonardo da Vinci 107 | Stmt.BindText(1, 'The Virgin and Child with St. Anne'); 108 | Stmt.BindInt (2, 1508); 109 | Stmt.BindInt (3, IDs[1]); 110 | Stmt.StepAndReset; 111 | 112 | Stmt.BindText(1, 'Mona Lisa'); 113 | Stmt.BindInt (2, 1519); 114 | Stmt.BindInt (3, IDs[1]); 115 | Stmt.StepAndReset; 116 | 117 | // Raphael 118 | Stmt.BindText(1, 'Sistine Madonna'); 119 | Stmt.BindInt (2, 1514); 120 | Stmt.BindInt (3, IDs[2]); 121 | Stmt.StepAndReset; 122 | 123 | Stmt.BindText(1, 'Transfiguration'); 124 | Stmt.BindInt (2, 1520); 125 | Stmt.BindInt (3, IDs[2]); 126 | Stmt.StepAndReset; 127 | 128 | // Arkhip Kuindzhi 129 | Stmt.BindText(1, 'After a rain'); 130 | Stmt.BindInt (2, 1879); 131 | Stmt.BindInt (3, IDs[3]); 132 | Stmt.StepAndReset; 133 | 134 | Stmt.BindText(1, 'Elbrus'); 135 | Stmt.BindInt (2, 1895); 136 | Stmt.BindInt (3, IDs[3]); 137 | Stmt.StepAndReset; 138 | 139 | // Nicholas Roerich 140 | Stmt.BindText(1, 'To Kailas. Lahul'); 141 | Stmt.BindInt (2, 1932); 142 | Stmt.BindInt (3, IDs[4]); 143 | Stmt.StepAndReset; 144 | 145 | Stmt.BindText(1, 'Krishna'); 146 | Stmt.BindInt (2, 1929); 147 | Stmt.BindInt (3, IDs[4]); 148 | Stmt.StepAndReset; 149 | 150 | // Ivan Aivazovsky 151 | Stmt.BindText(1, 'The Mary Caught in a Storm'); 152 | Stmt.BindInt (2, 1892); 153 | Stmt.BindInt (3, IDs[5]); 154 | Stmt.StepAndReset; 155 | 156 | Stmt.BindText(1, 'Brig "Mercury" Attacked by Two Turkish Ships'); 157 | Stmt.BindInt (2, 1892); 158 | Stmt.BindInt (3, IDs[5]); 159 | Stmt.StepAndReset; 160 | 161 | // Ivan Shishkin 162 | Stmt.BindText(1, 'Morning in a Pine Forest'); 163 | Stmt.BindInt (2, 1889); 164 | Stmt.BindInt (3, IDs[6]); 165 | Stmt.StepAndReset; 166 | 167 | Stmt.BindText(1, 'Wood Distances'); 168 | Stmt.BindInt (2, 1884); 169 | Stmt.BindInt (3, IDs[6]); 170 | Stmt.StepAndReset; 171 | finally 172 | Stmt.Free; 173 | end; 174 | 175 | ShowMessage('Database created.'); 176 | finally 177 | DB.Free; 178 | Screen.Cursor := crDefault; 179 | end; 180 | end; 181 | 182 | procedure TForm_Main.Button_DB_ReadClick(Sender: TObject); 183 | var 184 | DB: TSQLite3Database; 185 | Stmt_Artists, 186 | Stmt_Paintings: TSQLite3Statement; 187 | begin 188 | if not FileExists('artists.db') then 189 | begin 190 | ShowMessage('The database does not exist. Please create one.'); 191 | Exit; 192 | end; 193 | 194 | DB := TSQLite3Database.Create; 195 | try 196 | DB.Open('artists.db'); 197 | 198 | // Show all artists and their paintings 199 | Stmt_Artists := DB.Prepare('SELECT rowid, name, born, died FROM artists ORDER BY born'); 200 | Stmt_Paintings := DB.Prepare('SELECT title, year FROM paintings WHERE artist = ? ORDER BY year'); 201 | try 202 | while Stmt_Artists.Step = SQLITE_ROW do 203 | begin 204 | Memo_Result.Lines.Add(Stmt_Artists.ColumnText(1)); 205 | Memo_Result.Lines.Add(DateToStr(Stmt_Artists.ColumnDouble(2)) + ' - ' + DateToStr(Stmt_Artists.ColumnDouble(3))); 206 | 207 | Memo_Result.Lines.Add('paintings:'); 208 | Stmt_Paintings.BindInt(1, Stmt_Artists.ColumnInt(0)); 209 | while Stmt_Paintings.Step = SQLITE_ROW do 210 | Memo_Result.Lines.Add(' ' + Stmt_Paintings.ColumnText(0) + ' (' + Stmt_Paintings.ColumnText(1) + ')'); 211 | Stmt_Paintings.Reset; 212 | 213 | Memo_Result.Lines.Add(''); 214 | end; 215 | finally 216 | Stmt_Paintings.Free; 217 | Stmt_Artists.Free; 218 | end; 219 | finally 220 | DB.Free; 221 | end; 222 | 223 | // Add separator 224 | Memo_Result.Lines.Add('------------------------------------------------'); 225 | Memo_Result.Lines.Add(''); 226 | end; 227 | 228 | end. 229 | 230 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2013 Yuri Plashenkov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQLite for Delphi and FreePascal/Lazarus 2 | 3 | SQLite is a software library that implements a self-contained, serverless, zero-configuration, 4 | transactional SQL database engine. The source code for SQLite is in the public domain and is thus 5 | free for use for any purpose, commercial or private. SQLite is the most widely deployed SQL database 6 | engine in the world. 7 | 8 | This package contains complete SQLite3 API translation for Delphi and FreePascal/Lazarus, as well as 9 | a simple Unicode-enabled object wrapper to simplify the use of this database engine. 10 | 11 | ## Compatibility 12 | 13 | Compatible with Delphi/C++Builder 6–XE5 and FreePascal/Lazarus. 14 | 15 | ## Installation and usage notes 16 | 17 | Unzip the package somewhere on your hard drive. Now you need to add the full path of the Source 18 | folder to your IDE options. 19 | 20 | #### For Delphi 6–7 or C++Builder 6 21 | 22 | Select *Tools > Environment Options* from the menu, then go to the *Library* tab and add the full 23 | path of this package's Source directory to the *Library Path*. 24 | 25 | #### For Delphi 2005 and up, or C++Builder 2006 and up 26 | 27 | Select *Tools > Options* from the menu, then select *Environment Options > Delphi Options > 28 | Library - Win32*, and add the full path of this package's Source directory to the *Library Path*. 29 | 30 | #### For Lazarus 31 | 32 | Select *Environment > Options* from the menu, then select *CodeTools*, and add the full path of this 33 | package's Source directory to the *Search Path* ("Additional source search path for all projects"). 34 | 35 | When using this translation in your projects, add the SQLite3 unit to the uses clause of your source 36 | file if you want to use SQLite3 API, and the SQLite3Wrap unit if you want to use the accompanying 37 | wrapper. 38 | 39 | Do not forget to distribute the shared library (sqlite3.dll for Windows, sqlite3.so for Linux) with 40 | your program. 41 | 42 | ## Credits 43 | 44 | - [Yuri Plashenkov](https://github.com/plashenkov), developer 45 | - [Marek Mauder](https://github.com/galfar), contributor 46 | - [Alexander Kotliarskyi](https://github.com/frantic), contributor 47 | 48 | ## License 49 | 50 | This package is licensed under the [MIT license](LICENSE.md). 51 | -------------------------------------------------------------------------------- /Source/SQLite3.pas: -------------------------------------------------------------------------------- 1 | {* 2 | * SQLite for Delphi and FreePascal/Lazarus 3 | * 4 | * This unit contains complete SQLite3 API translation. 5 | * Version of SQLite: 3.6.22 6 | * 7 | * Copyright (c) 2013 Yuri Plashenkov 8 | * 9 | * MIT License 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy 12 | * of this software and associated documentation files (the "Software"), to deal 13 | * in the Software without restriction, including without limitation the rights 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | * copies of the Software, and to permit persons to whom the Software is 16 | * furnished to do so, subject to the following conditions: 17 | 18 | * The above copyright notice and this permission notice shall be included in all 19 | * copies or substantial portions of the Software. 20 | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 | * SOFTWARE. 28 | *} 29 | 30 | unit SQLite3; 31 | 32 | {$IFDEF FPC} 33 | {$MODE DELPHI} 34 | {$ENDIF} 35 | 36 | { $DEFINE SQLITE_DEPRECATED} // Enable deprecated functions 37 | { $DEFINE SQLITE_EXPERIMENTAL} // Enable experimental functions 38 | 39 | {$DEFINE SQLITE_ENABLE_COLUMN_METADATA} // Enable functions to work with 40 | // column metadata: 41 | // table name, DB name, etc. 42 | 43 | {$DEFINE SQLITE_ENABLE_UNLOCK_NOTIFY} // Enable sqlite3_unlock_notify() 44 | // function to receive DB unlock 45 | // notification 46 | 47 | { $DEFINE SQLITE_DEBUG} // Enable sqlite3_mutex_held() and 48 | // sqlite3_mutex_notheld() functions 49 | 50 | interface 51 | 52 | type 53 | PPAnsiCharArray = ^TPAnsiCharArray; 54 | TPAnsiCharArray = array[0..MaxInt div SizeOf(PAnsiChar) - 1] of PAnsiChar; 55 | 56 | const 57 | {$IFDEF MSWINDOWS} 58 | sqlite3_lib = 'sqlite3.dll'; 59 | {$ENDIF} 60 | {$IFDEF UNIX} 61 | sqlite3_lib = 'sqlite3.so'; 62 | {$ENDIF} 63 | {$IFDEF DARWIN} 64 | sqlite3_lib = 'libsqlite3.dylib'; 65 | {$ENDIF} 66 | 67 | //var sqlite3_version: PAnsiChar; 68 | function sqlite3_libversion: PAnsiChar; cdecl; external sqlite3_lib; 69 | function sqlite3_sourceid: PAnsiChar; cdecl; external sqlite3_lib; 70 | function sqlite3_libversion_number: Integer; cdecl; external sqlite3_lib; 71 | 72 | function sqlite3_threadsafe: Integer; cdecl; external sqlite3_lib; 73 | 74 | type 75 | PSQLite3 = type Pointer; 76 | 77 | function sqlite3_close(db: PSQLite3): Integer; cdecl; external sqlite3_lib; 78 | 79 | type 80 | TSQLite3Callback = function(pArg: Pointer; nCol: Integer; argv: PPAnsiCharArray; colv: PPAnsiCharArray): Integer; cdecl; 81 | 82 | function sqlite3_exec(db: PSQLite3; const sql: PAnsiChar; callback: TSQLite3Callback; pArg: Pointer; errmsg: PPAnsiChar): Integer; cdecl; external sqlite3_lib; 83 | 84 | const 85 | SQLITE_OK = 0; 86 | SQLITE_ERROR = 1; 87 | SQLITE_INTERNAL = 2; 88 | SQLITE_PERM = 3; 89 | SQLITE_ABORT = 4; 90 | SQLITE_BUSY = 5; 91 | SQLITE_LOCKED = 6; 92 | SQLITE_NOMEM = 7; 93 | SQLITE_READONLY = 8; 94 | SQLITE_INTERRUPT = 9; 95 | SQLITE_IOERR = 10; 96 | SQLITE_CORRUPT = 11; 97 | SQLITE_NOTFOUND = 12; 98 | SQLITE_FULL = 13; 99 | SQLITE_CANTOPEN = 14; 100 | SQLITE_PROTOCOL = 15; 101 | SQLITE_EMPTY = 16; 102 | SQLITE_SCHEMA = 17; 103 | SQLITE_TOOBIG = 18; 104 | SQLITE_CONSTRAINT = 19; 105 | SQLITE_MISMATCH = 20; 106 | SQLITE_MISUSE = 21; 107 | SQLITE_NOLFS = 22; 108 | SQLITE_AUTH = 23; 109 | SQLITE_FORMAT = 24; 110 | SQLITE_RANGE = 25; 111 | SQLITE_NOTADB = 26; 112 | SQLITE_ROW = 100; 113 | SQLITE_DONE = 101; 114 | 115 | const 116 | SQLITE_IOERR_READ = SQLITE_IOERR or (1 shl 8); 117 | SQLITE_IOERR_SHORT_READ = SQLITE_IOERR or (2 shl 8); 118 | SQLITE_IOERR_WRITE = SQLITE_IOERR or (3 shl 8); 119 | SQLITE_IOERR_FSYNC = SQLITE_IOERR or (4 shl 8); 120 | SQLITE_IOERR_DIR_FSYNC = SQLITE_IOERR or (5 shl 8); 121 | SQLITE_IOERR_TRUNCATE = SQLITE_IOERR or (6 shl 8); 122 | SQLITE_IOERR_FSTAT = SQLITE_IOERR or (7 shl 8); 123 | SQLITE_IOERR_UNLOCK = SQLITE_IOERR or (8 shl 8); 124 | SQLITE_IOERR_RDLOCK = SQLITE_IOERR or (9 shl 8); 125 | SQLITE_IOERR_DELETE = SQLITE_IOERR or (10 shl 8); 126 | SQLITE_IOERR_BLOCKED = SQLITE_IOERR or (11 shl 8); 127 | SQLITE_IOERR_NOMEM = SQLITE_IOERR or (12 shl 8); 128 | SQLITE_IOERR_ACCESS = SQLITE_IOERR or (13 shl 8); 129 | SQLITE_IOERR_CHECKRESERVEDLOCK = SQLITE_IOERR or (14 shl 8); 130 | SQLITE_IOERR_LOCK = SQLITE_IOERR or (15 shl 8); 131 | SQLITE_IOERR_CLOSE = SQLITE_IOERR or (16 shl 8); 132 | SQLITE_IOERR_DIR_CLOSE = SQLITE_IOERR or (17 shl 8); 133 | SQLITE_LOCKED_SHAREDCACHE = SQLITE_LOCKED or (1 shl 8); 134 | 135 | const 136 | SQLITE_OPEN_READONLY = $00000001; 137 | SQLITE_OPEN_READWRITE = $00000002; 138 | SQLITE_OPEN_CREATE = $00000004; 139 | SQLITE_OPEN_DELETEONCLOSE = $00000008; 140 | SQLITE_OPEN_EXCLUSIVE = $00000010; 141 | SQLITE_OPEN_MAIN_DB = $00000100; 142 | SQLITE_OPEN_TEMP_DB = $00000200; 143 | SQLITE_OPEN_TRANSIENT_DB = $00000400; 144 | SQLITE_OPEN_MAIN_JOURNAL = $00000800; 145 | SQLITE_OPEN_TEMP_JOURNAL = $00001000; 146 | SQLITE_OPEN_SUBJOURNAL = $00002000; 147 | SQLITE_OPEN_MASTER_JOURNAL = $00004000; 148 | SQLITE_OPEN_NOMUTEX = $00008000; 149 | SQLITE_OPEN_FULLMUTEX = $00010000; 150 | SQLITE_OPEN_SHAREDCACHE = $00020000; 151 | SQLITE_OPEN_PRIVATECACHE = $00040000; 152 | 153 | const 154 | SQLITE_IOCAP_ATOMIC = $00000001; 155 | SQLITE_IOCAP_ATOMIC512 = $00000002; 156 | SQLITE_IOCAP_ATOMIC1K = $00000004; 157 | SQLITE_IOCAP_ATOMIC2K = $00000008; 158 | SQLITE_IOCAP_ATOMIC4K = $00000010; 159 | SQLITE_IOCAP_ATOMIC8K = $00000020; 160 | SQLITE_IOCAP_ATOMIC16K = $00000040; 161 | SQLITE_IOCAP_ATOMIC32K = $00000080; 162 | SQLITE_IOCAP_ATOMIC64K = $00000100; 163 | SQLITE_IOCAP_SAFE_APPEND = $00000200; 164 | SQLITE_IOCAP_SEQUENTIAL = $00000400; 165 | 166 | const 167 | SQLITE_LOCK_NONE = 0; 168 | SQLITE_LOCK_SHARED = 1; 169 | SQLITE_LOCK_RESERVED = 2; 170 | SQLITE_LOCK_PENDING = 3; 171 | SQLITE_LOCK_EXCLUSIVE = 4; 172 | 173 | const 174 | SQLITE_SYNC_NORMAL = $00002; 175 | SQLITE_SYNC_FULL = $00003; 176 | SQLITE_SYNC_DATAONLY = $00010; 177 | 178 | type 179 | PSQLite3File = ^TSQLite3File; 180 | PSQLite3IOMethods = ^TSQLite3IOMethods; 181 | 182 | sqlite3_file = record 183 | pMethods: PSQLite3IOMethods; 184 | end; 185 | TSQLite3File = sqlite3_file; 186 | 187 | sqlite3_io_methods = record 188 | iVersion: Integer; 189 | xClose: function(id: PSQLite3File): Integer; cdecl; 190 | xRead: function(id: PSQLite3File; pBuf: Pointer; iAmt: Integer; iOfst: Int64): Integer; cdecl; 191 | xWrite: function(id: PSQLite3File; const pBuf: Pointer; iAmt: Integer; iOfst: Int64): Integer; cdecl; 192 | xTruncate: function(id: PSQLite3File; size: Int64): Integer; cdecl; 193 | xSync: function(id: PSQLite3File; flags: Integer): Integer; cdecl; 194 | xFileSize: function(id: PSQLite3File; var pSize: Int64): Integer; cdecl; 195 | xLock: function(id: PSQLite3File; locktype: Integer): Integer; cdecl; 196 | xUnlock: function(id: PSQLite3File; locktype: Integer): Integer; cdecl; 197 | xCheckReservedLock: function(f: PSQLite3File; var pResOut: Integer): Integer; cdecl; 198 | xFileControl: function(id: PSQLite3File; op: Integer; pArg: Pointer): Integer; cdecl; 199 | xSectorSize: function(id: PSQLite3File): Integer; cdecl; 200 | xDeviceCharacteristics: function(id: PSQLite3File): Integer; cdecl; 201 | end; 202 | TSQLite3IOMethods = sqlite3_io_methods; 203 | 204 | const 205 | SQLITE_FCNTL_LOCKSTATE = 1; 206 | SQLITE_GET_LOCKPROXYFILE = 2; 207 | SQLITE_SET_LOCKPROXYFILE = 3; 208 | SQLITE_LAST_ERRNO = 4; 209 | 210 | type 211 | PSQLite3Mutex = type Pointer; 212 | 213 | type 214 | PSQLite3VFS = ^TSQLite3VFS; 215 | sqlite3_vfs = record 216 | iVersion: Integer; 217 | szOsFile: Integer; 218 | mxPathname: Integer; 219 | pNext: PSQLite3VFS; 220 | zName: PAnsiChar; 221 | pAppData: Pointer; 222 | xOpen: function(pVfs: PSQLite3VFS; const zName: PAnsiChar; id: PSQLite3File; flags: Integer; pOutFlags: PInteger): Integer; cdecl; 223 | xDelete: function(pVfs: PSQLite3VFS; const zName: PAnsiChar; syncDir: Integer): Integer; cdecl; 224 | xAccess: function(pVfs: PSQLite3VFS; const zName: PAnsiChar; flags: Integer; var pResOut: Integer): Integer; cdecl; 225 | xFullPathname: function(pVfs: PSQLite3VFS; const zName: PAnsiChar; nOut: Integer; zOut: PAnsiChar): Integer; cdecl; 226 | xDlOpen: function(pVfs: PSQLite3VFS; const zFilename: PAnsiChar): Pointer; cdecl; 227 | xDlError: procedure(pVfs: PSQLite3VFS; nByte: Integer; zErrMsg: PAnsiChar); cdecl; 228 | xDlSym: function(pVfs: PSQLite3VFS; pHandle: Pointer; const zSymbol: PAnsiChar): Pointer; cdecl; 229 | xDlClose: procedure(pVfs: PSQLite3VFS; pHandle: Pointer); cdecl; 230 | xRandomness: function(pVfs: PSQLite3VFS; nByte: Integer; zOut: PAnsiChar): Integer; cdecl; 231 | xSleep: function(pVfs: PSQLite3VFS; microseconds: Integer): Integer; cdecl; 232 | xCurrentTime: function(pVfs: PSQLite3VFS; var prNow: Double): Integer; cdecl; 233 | xGetLastError: function(pVfs: PSQLite3VFS; nBuf: Integer; zBuf: PAnsiChar): Integer; cdecl; 234 | end; 235 | TSQLite3VFS = sqlite3_vfs; 236 | 237 | const 238 | SQLITE_ACCESS_EXISTS = 0; 239 | SQLITE_ACCESS_READWRITE = 1; 240 | SQLITE_ACCESS_READ = 2; 241 | 242 | function sqlite3_initialize: Integer; cdecl; external sqlite3_lib; 243 | function sqlite3_shutdown: Integer; cdecl; external sqlite3_lib; 244 | function sqlite3_os_init: Integer; cdecl; external sqlite3_lib; 245 | function sqlite3_os_end: Integer; cdecl; external sqlite3_lib; 246 | 247 | {$IFDEF SQLITE_EXPERIMENTAL} 248 | function sqlite3_config(op: Integer{; ...}): Integer; cdecl; external sqlite3_lib; 249 | 250 | function sqlite3_db_config(db: PSQLite3; op: Integer{; ...}): Integer; cdecl; external sqlite3_lib; 251 | 252 | type 253 | sqlite3_mem_methods = record 254 | xMalloc: function(nByte: Integer): Pointer; cdecl; 255 | xFree: procedure(pPrior: Pointer); cdecl; 256 | xRealloc: function(pPrior: Pointer; nByte: Integer): Pointer; cdecl; 257 | xSize: function(pPrior: Pointer): Integer; cdecl; 258 | xRoundup: function(n: Integer): Integer; cdecl; 259 | xInit: function(NotUsed: Pointer): Integer; cdecl; 260 | xShutdown: procedure(NotUsed: Pointer); cdecl; 261 | pAppData: Pointer; 262 | end; 263 | TSQLite3MemMethods = sqlite3_mem_methods; 264 | 265 | const 266 | SQLITE_CONFIG_SINGLETHREAD = 1; 267 | SQLITE_CONFIG_MULTITHREAD = 2; 268 | SQLITE_CONFIG_SERIALIZED = 3; 269 | SQLITE_CONFIG_MALLOC = 4; 270 | SQLITE_CONFIG_GETMALLOC = 5; 271 | SQLITE_CONFIG_SCRATCH = 6; 272 | SQLITE_CONFIG_PAGECACHE = 7; 273 | SQLITE_CONFIG_HEAP = 8; 274 | SQLITE_CONFIG_MEMSTATUS = 9; 275 | SQLITE_CONFIG_MUTEX = 10; 276 | SQLITE_CONFIG_GETMUTEX = 11; 277 | //SQLITE_CONFIG_CHUNKALLOC = 12; 278 | SQLITE_CONFIG_LOOKASIDE = 13; 279 | SQLITE_CONFIG_PCACHE = 14; 280 | SQLITE_CONFIG_GETPCACHE = 15; 281 | 282 | const 283 | SQLITE_DBCONFIG_LOOKASIDE = 1001; 284 | {$ENDIF} 285 | 286 | function sqlite3_extended_result_codes(db: PSQLite3; onoff: Integer): Integer; cdecl; external sqlite3_lib; 287 | 288 | function sqlite3_last_insert_rowid(db: PSQLite3): Int64; cdecl; external sqlite3_lib; 289 | 290 | function sqlite3_changes(db: PSQLite3): Integer; cdecl; external sqlite3_lib; 291 | 292 | function sqlite3_total_changes(db: PSQLite3): Integer; cdecl; external sqlite3_lib; 293 | 294 | procedure sqlite3_interrupt(db: PSQLite3); cdecl; external sqlite3_lib; 295 | 296 | function sqlite3_complete(const sql: PAnsiChar): Integer; cdecl; external sqlite3_lib; 297 | function sqlite3_complete16(const sql: PWideChar): Integer; cdecl; external sqlite3_lib; 298 | 299 | type 300 | TSQLite3BusyCallback = function(ptr: Pointer; count: Integer): Integer; cdecl; 301 | 302 | function sqlite3_busy_handler(db: PSQLite3; xBusy: TSQLite3BusyCallback; pArg: Pointer): Integer; cdecl; external sqlite3_lib; 303 | 304 | function sqlite3_busy_timeout(db: PSQLite3; ms: Integer): Integer; cdecl; external sqlite3_lib; 305 | 306 | function sqlite3_get_table(db: PSQLite3; const zSql: PAnsiChar; var pazResult: PPAnsiCharArray; pnRow: PInteger; pnColumn: PInteger; pzErrmsg: PPAnsiChar): Integer; cdecl; external sqlite3_lib; 307 | procedure sqlite3_free_table(result: PPAnsiCharArray); cdecl; external sqlite3_lib; 308 | 309 | function sqlite3_mprintf(const zFormat: PAnsiChar{; ...}): PAnsiChar; cdecl; external sqlite3_lib; 310 | function sqlite3_vmprintf(const zFormat: PAnsiChar; ap: Pointer{va_list}): PAnsiChar; cdecl; external sqlite3_lib; 311 | function sqlite3_snprintf(n: Integer; zBuf: PAnsiChar; const zFormat: PAnsiChar{; ...}): PAnsiChar; cdecl; external sqlite3_lib; 312 | 313 | function sqlite3_malloc(n: Integer): Pointer; cdecl; external sqlite3_lib; 314 | function sqlite3_realloc(pOld: Pointer; n: Integer): Pointer; cdecl; external sqlite3_lib; 315 | procedure sqlite3_free(p: Pointer); cdecl; external sqlite3_lib; 316 | 317 | function sqlite3_memory_used: Int64; cdecl; external sqlite3_lib; 318 | function sqlite3_memory_highwater(resetFlag: Integer): Int64; cdecl; external sqlite3_lib; 319 | 320 | procedure sqlite3_randomness(N: Integer; P: Pointer); cdecl; external sqlite3_lib; 321 | 322 | type 323 | TSQLite3AuthorizerCallback = function(pAuthArg: Pointer; code: Integer; const zTab: PAnsiChar; const zCol: PAnsiChar; const zDb: PAnsiChar; const zAuthContext: PAnsiChar): Integer; cdecl; 324 | 325 | function sqlite3_set_authorizer(db: PSQLite3; xAuth: TSQLite3AuthorizerCallback; pUserData: Pointer): Integer; cdecl; external sqlite3_lib; 326 | 327 | const 328 | SQLITE_DENY = 1; 329 | SQLITE_IGNORE = 2; 330 | 331 | const 332 | SQLITE_CREATE_INDEX = 1; 333 | SQLITE_CREATE_TABLE = 2; 334 | SQLITE_CREATE_TEMP_INDEX = 3; 335 | SQLITE_CREATE_TEMP_TABLE = 4; 336 | SQLITE_CREATE_TEMP_TRIGGER = 5; 337 | SQLITE_CREATE_TEMP_VIEW = 6; 338 | SQLITE_CREATE_TRIGGER = 7; 339 | SQLITE_CREATE_VIEW = 8; 340 | SQLITE_DELETE = 9; 341 | SQLITE_DROP_INDEX = 10; 342 | SQLITE_DROP_TABLE = 11; 343 | SQLITE_DROP_TEMP_INDEX = 12; 344 | SQLITE_DROP_TEMP_TABLE = 13; 345 | SQLITE_DROP_TEMP_TRIGGER = 14; 346 | SQLITE_DROP_TEMP_VIEW = 15; 347 | SQLITE_DROP_TRIGGER = 16; 348 | SQLITE_DROP_VIEW = 17; 349 | SQLITE_INSERT = 18; 350 | SQLITE_PRAGMA = 19; 351 | SQLITE_READ = 20; 352 | SQLITE_SELECT = 21; 353 | SQLITE_TRANSACTION = 22; 354 | SQLITE_UPDATE = 23; 355 | SQLITE_ATTACH = 24; 356 | SQLITE_DETACH = 25; 357 | SQLITE_ALTER_TABLE = 26; 358 | SQLITE_REINDEX = 27; 359 | SQLITE_ANALYZE = 28; 360 | SQLITE_CREATE_VTABLE = 29; 361 | SQLITE_DROP_VTABLE = 30; 362 | SQLITE_FUNCTION = 31; 363 | SQLITE_SAVEPOINT = 32; 364 | SQLITE_COPY = 0; 365 | 366 | {$IFDEF SQLITE_EXPERIMENTAL} 367 | type 368 | TSQLite3TraceCallback = procedure(pTraceArg: Pointer; const zTrace: PAnsiChar); cdecl; 369 | TSQLite3ProfileCallback = procedure(pProfileArg: Pointer; const zSql: PAnsiChar; elapseTime: UInt64); cdecl; 370 | 371 | function sqlite3_trace(db: PSQLite3; xTrace: TSQLite3TraceCallback; pArg: Pointer): Pointer; cdecl; external sqlite3_lib; 372 | function sqlite3_profile(db: PSQLite3; xProfile: TSQLite3ProfileCallback; pArg: Pointer): Pointer; cdecl; external sqlite3_lib; 373 | {$ENDIF} 374 | 375 | type 376 | TSQLite3ProgressCallback = function(pProgressArg: Pointer): Integer; cdecl; 377 | 378 | procedure sqlite3_progress_handler(db: PSQLite3; nOps: Integer; xProgress: TSQLite3ProgressCallback; pArg: Pointer); cdecl; external sqlite3_lib; 379 | 380 | function sqlite3_open(const filename: PAnsiChar; var ppDb: PSQLite3): Integer; cdecl; external sqlite3_lib; 381 | function sqlite3_open16(const filename: PWideChar; var ppDb: PSQLite3): Integer; cdecl; external sqlite3_lib; 382 | function sqlite3_open_v2(const filename: PAnsiChar; var ppDb: PSQLite3; flags: Integer; const zVfs: PAnsiChar): Integer; cdecl; external sqlite3_lib; 383 | 384 | function sqlite3_errcode(db: PSQLite3): Integer; cdecl; external sqlite3_lib; 385 | function sqlite3_extended_errcode(db: PSQLite3): Integer; cdecl; external sqlite3_lib; 386 | function sqlite3_errmsg(db: PSQLite3): PAnsiChar; cdecl; external sqlite3_lib; 387 | function sqlite3_errmsg16(db: PSQLite3): PWideChar; cdecl; external sqlite3_lib; 388 | 389 | type 390 | PSQLite3Stmt = type Pointer; 391 | 392 | function sqlite3_limit(db: PSQLite3; limitId: Integer; newLimit: Integer): Integer; cdecl; external sqlite3_lib; 393 | 394 | const 395 | SQLITE_LIMIT_LENGTH = 0; 396 | SQLITE_LIMIT_SQL_LENGTH = 1; 397 | SQLITE_LIMIT_COLUMN = 2; 398 | SQLITE_LIMIT_EXPR_DEPTH = 3; 399 | SQLITE_LIMIT_COMPOUND_SELECT = 4; 400 | SQLITE_LIMIT_VDBE_OP = 5; 401 | SQLITE_LIMIT_FUNCTION_ARG = 6; 402 | SQLITE_LIMIT_ATTACHED = 7; 403 | SQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8; 404 | SQLITE_LIMIT_VARIABLE_NUMBER = 9; 405 | SQLITE_LIMIT_TRIGGER_DEPTH = 10; 406 | 407 | function sqlite3_prepare(db: PSQLite3; const zSql: PAnsiChar; nByte: Integer; var ppStmt: PSQLite3Stmt; const pzTail: PPAnsiChar): Integer; cdecl; external sqlite3_lib; 408 | function sqlite3_prepare_v2(db: PSQLite3; const zSql: PAnsiChar; nByte: Integer; var ppStmt: PSQLite3Stmt; const pzTail: PPAnsiChar): Integer; cdecl; external sqlite3_lib; 409 | function sqlite3_prepare16(db: PSQLite3; const zSql: PWideChar; nByte: Integer; var ppStmt: PSQLite3Stmt; const pzTail: PPWideChar): Integer; cdecl; external sqlite3_lib; 410 | function sqlite3_prepare16_v2(db: PSQLite3; const zSql: PWideChar; nByte: Integer; var ppStmt: PSQLite3Stmt; const pzTail: PPWideChar): Integer; cdecl; external sqlite3_lib; 411 | 412 | function sqlite3_sql(pStmt: PSQLite3Stmt): PAnsiChar; cdecl; external sqlite3_lib; 413 | 414 | type 415 | PSQLite3Value = ^TSQLite3Value; 416 | sqlite3_value = type Pointer; 417 | TSQLite3Value = sqlite3_value; 418 | 419 | PPSQLite3ValueArray = ^TPSQLite3ValueArray; 420 | TPSQLite3ValueArray = array[0..MaxInt div SizeOf(PSQLite3Value) - 1] of PSQLite3Value; 421 | 422 | type 423 | PSQLite3Context = type Pointer; 424 | 425 | type 426 | TSQLite3DestructorType = procedure(p: Pointer); cdecl; 427 | 428 | const 429 | SQLITE_STATIC = Pointer(0); 430 | SQLITE_TRANSIENT = Pointer(-1); 431 | 432 | function sqlite3_bind_blob(pStmt: PSQLite3Stmt; i: Integer; const zData: Pointer; n: Integer; xDel: TSQLite3DestructorType): Integer; cdecl; external sqlite3_lib; 433 | function sqlite3_bind_double(pStmt: PSQLite3Stmt; i: Integer; rValue: Double): Integer; cdecl; external sqlite3_lib; 434 | function sqlite3_bind_int(p: PSQLite3Stmt; i: Integer; iValue: Integer): Integer; cdecl; external sqlite3_lib; 435 | function sqlite3_bind_int64(pStmt: PSQLite3Stmt; i: Integer; iValue: Int64): Integer; cdecl; external sqlite3_lib; 436 | function sqlite3_bind_null(pStmt: PSQLite3Stmt; i: Integer): Integer; cdecl; external sqlite3_lib; 437 | function sqlite3_bind_text(pStmt: PSQLite3Stmt; i: Integer; const zData: PAnsiChar; n: Integer; xDel: TSQLite3DestructorType): Integer; cdecl; external sqlite3_lib; 438 | function sqlite3_bind_text16(pStmt: PSQLite3Stmt; i: Integer; const zData: PWideChar; nData: Integer; xDel: TSQLite3DestructorType): Integer; cdecl; external sqlite3_lib; 439 | function sqlite3_bind_value(pStmt: PSQLite3Stmt; i: Integer; const pValue: PSQLite3Value): Integer; cdecl; external sqlite3_lib; 440 | function sqlite3_bind_zeroblob(pStmt: PSQLite3Stmt; i: Integer; n: Integer): Integer; cdecl; external sqlite3_lib; 441 | 442 | function sqlite3_bind_parameter_count(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 443 | 444 | function sqlite3_bind_parameter_name(pStmt: PSQLite3Stmt; i: Integer): PAnsiChar; cdecl; external sqlite3_lib; 445 | 446 | function sqlite3_bind_parameter_index(pStmt: PSQLite3Stmt; const zName: PAnsiChar): Integer; cdecl; external sqlite3_lib; 447 | 448 | function sqlite3_clear_bindings(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 449 | 450 | function sqlite3_column_count(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 451 | 452 | function sqlite3_column_name(pStmt: PSQLite3Stmt; N: Integer): PAnsiChar; cdecl; external sqlite3_lib; 453 | function sqlite3_column_name16(pStmt: PSQLite3Stmt; N: Integer): PWideChar; cdecl; external sqlite3_lib; 454 | 455 | {$IFDEF SQLITE_ENABLE_COLUMN_METADATA} 456 | function sqlite3_column_database_name(pStmt: PSQLite3Stmt; N: Integer): PAnsiChar; cdecl; external sqlite3_lib; 457 | function sqlite3_column_database_name16(pStmt: PSQLite3Stmt; N: Integer): PWideChar; cdecl; external sqlite3_lib; 458 | function sqlite3_column_table_name(pStmt: PSQLite3Stmt; N: Integer): PAnsiChar; cdecl; external sqlite3_lib; 459 | function sqlite3_column_table_name16(pStmt: PSQLite3Stmt; N: Integer): PWideChar; cdecl; external sqlite3_lib; 460 | function sqlite3_column_origin_name(pStmt: PSQLite3Stmt; N: Integer): PAnsiChar; cdecl; external sqlite3_lib; 461 | function sqlite3_column_origin_name16(pStmt: PSQLite3Stmt; N: Integer): PWideChar; cdecl; external sqlite3_lib; 462 | {$ENDIF} 463 | 464 | function sqlite3_column_decltype(pStmt: PSQLite3Stmt; N: Integer): PAnsiChar; cdecl; external sqlite3_lib; 465 | function sqlite3_column_decltype16(pStmt: PSQLite3Stmt; N: Integer): PWideChar; cdecl; external sqlite3_lib; 466 | 467 | function sqlite3_step(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 468 | 469 | function sqlite3_data_count(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 470 | 471 | const 472 | SQLITE_INTEGER = 1; 473 | SQLITE_FLOAT = 2; 474 | SQLITE_BLOB = 4; 475 | SQLITE_NULL = 5; 476 | SQLITE_TEXT = 3; 477 | SQLITE3_TEXT = 3; 478 | 479 | function sqlite3_column_blob(pStmt: PSQLite3Stmt; iCol: Integer): Pointer; cdecl; external sqlite3_lib; 480 | function sqlite3_column_bytes(pStmt: PSQLite3Stmt; iCol: Integer): Integer; cdecl; external sqlite3_lib; 481 | function sqlite3_column_bytes16(pStmt: PSQLite3Stmt; iCol: Integer): Integer; cdecl; external sqlite3_lib; 482 | function sqlite3_column_double(pStmt: PSQLite3Stmt; iCol: Integer): Double; cdecl; external sqlite3_lib; 483 | function sqlite3_column_int(pStmt: PSQLite3Stmt; iCol: Integer): Integer; cdecl; external sqlite3_lib; 484 | function sqlite3_column_int64(pStmt: PSQLite3Stmt; iCol: Integer): Int64; cdecl; external sqlite3_lib; 485 | function sqlite3_column_text(pStmt: PSQLite3Stmt; iCol: Integer): PAnsiChar; cdecl; external sqlite3_lib; 486 | function sqlite3_column_text16(pStmt: PSQLite3Stmt; iCol: Integer): PWideChar; cdecl; external sqlite3_lib; 487 | function sqlite3_column_type(pStmt: PSQLite3Stmt; iCol: Integer): Integer; cdecl; external sqlite3_lib; 488 | function sqlite3_column_value(pStmt: PSQLite3Stmt; iCol: Integer): PSQLite3Value; cdecl; external sqlite3_lib; 489 | 490 | function sqlite3_finalize(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 491 | 492 | function sqlite3_reset(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 493 | 494 | type 495 | TSQLite3RegularFunction = procedure(ctx: PSQLite3Context; n: Integer; apVal: PPSQLite3ValueArray); cdecl; 496 | TSQLite3AggregateStep = procedure(ctx: PSQLite3Context; n: Integer; apVal: PPSQLite3ValueArray); cdecl; 497 | TSQLite3AggregateFinalize = procedure(ctx: PSQLite3Context); cdecl; 498 | 499 | function sqlite3_create_function(db: PSQLite3; const zFunctionName: PAnsiChar; nArg: Integer; eTextRep: Integer; pApp: Pointer; xFunc: TSQLite3RegularFunction; xStep: TSQLite3AggregateStep; xFinal: TSQLite3AggregateFinalize): Integer; cdecl; external sqlite3_lib; 500 | function sqlite3_create_function16(db: PSQLite3; const zFunctionName: PWideChar; nArg: Integer; eTextRep: Integer; pApp: Pointer; xFunc: TSQLite3RegularFunction; xStep: TSQLite3AggregateStep; xFinal: TSQLite3AggregateFinalize): Integer; cdecl; external sqlite3_lib; 501 | 502 | const 503 | SQLITE_UTF8 = 1; 504 | SQLITE_UTF16LE = 2; 505 | SQLITE_UTF16BE = 3; 506 | SQLITE_UTF16 = 4; 507 | SQLITE_ANY = 5; 508 | SQLITE_UTF16_ALIGNED = 8; 509 | 510 | {$IFDEF SQLITE_DEPRECATED} 511 | type 512 | TSQLite3MemoryAlarmCallback = procedure(pArg: Pointer; used: Int64; N: Integer); cdecl; 513 | 514 | function sqlite3_aggregate_count(p: PSQLite3Context): Integer; cdecl; external sqlite3_lib; 515 | function sqlite3_expired(pStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 516 | function sqlite3_transfer_bindings(pFromStmt: PSQLite3Stmt; pToStmt: PSQLite3Stmt): Integer; cdecl; external sqlite3_lib; 517 | function sqlite3_global_recover: Integer; cdecl; external sqlite3_lib; 518 | procedure sqlite3_thread_cleanup; cdecl; external sqlite3_lib; 519 | function sqlite3_memory_alarm(xCallback: TSQLite3MemoryAlarmCallback; pArg: Pointer; iThreshold: Int64): Integer; cdecl; external sqlite3_lib; 520 | {$ENDIF} 521 | 522 | function sqlite3_value_blob(pVal: PSQLite3Value): Pointer; cdecl; external sqlite3_lib; 523 | function sqlite3_value_bytes(pVal: PSQLite3Value): Integer; cdecl; external sqlite3_lib; 524 | function sqlite3_value_bytes16(pVal: PSQLite3Value): Integer; cdecl; external sqlite3_lib; 525 | function sqlite3_value_double(pVal: PSQLite3Value): Double; cdecl; external sqlite3_lib; 526 | function sqlite3_value_int(pVal: PSQLite3Value): Integer; cdecl; external sqlite3_lib; 527 | function sqlite3_value_int64(pVal: PSQLite3Value): Int64; cdecl; external sqlite3_lib; 528 | function sqlite3_value_text(pVal: PSQLite3Value): PAnsiChar; cdecl; external sqlite3_lib; 529 | function sqlite3_value_text16(pVal: PSQLite3Value): PWideChar; cdecl; external sqlite3_lib; 530 | function sqlite3_value_text16le(pVal: PSQLite3Value): Pointer; cdecl; external sqlite3_lib; 531 | function sqlite3_value_text16be(pVal: PSQLite3Value): Pointer; cdecl; external sqlite3_lib; 532 | function sqlite3_value_type(pVal: PSQLite3Value): Integer; cdecl; external sqlite3_lib; 533 | function sqlite3_value_numeric_type(pVal: PSQLite3Value): Integer; cdecl; external sqlite3_lib; 534 | 535 | function sqlite3_aggregate_context(p: PSQLite3Context; nBytes: Integer): Pointer; cdecl; external sqlite3_lib; 536 | 537 | function sqlite3_user_data(p: PSQLite3Context): Pointer; cdecl; external sqlite3_lib; 538 | 539 | function sqlite3_context_db_handle(p: PSQLite3Context): PSQLite3; cdecl; external sqlite3_lib; 540 | 541 | type 542 | TSQLite3AuxDataDestructor = procedure(pAux: Pointer); cdecl; 543 | 544 | function sqlite3_get_auxdata(pCtx: PSQLite3Context; N: Integer): Pointer; cdecl; external sqlite3_lib; 545 | procedure sqlite3_set_auxdata(pCtx: PSQLite3Context; N: Integer; pAux: Pointer; xDelete: TSQLite3AuxDataDestructor); cdecl; external sqlite3_lib; 546 | 547 | procedure sqlite3_result_blob(pCtx: PSQLite3Context; const z: Pointer; n: Integer; xDel: TSQLite3DestructorType); cdecl; external sqlite3_lib; 548 | procedure sqlite3_result_double(pCtx: PSQLite3Context; rVal: Double); cdecl; external sqlite3_lib; 549 | procedure sqlite3_result_error(pCtx: PSQLite3Context; const z: PAnsiChar; n: Integer); cdecl; external sqlite3_lib; 550 | procedure sqlite3_result_error16(pCtx: PSQLite3Context; const z: PWideChar; n: Integer); cdecl; external sqlite3_lib; 551 | procedure sqlite3_result_error_toobig(pCtx: PSQLite3Context); cdecl; external sqlite3_lib; 552 | procedure sqlite3_result_error_nomem(pCtx: PSQLite3Context); cdecl; external sqlite3_lib; 553 | procedure sqlite3_result_error_code(pCtx: PSQLite3Context; errCode: Integer); cdecl; external sqlite3_lib; 554 | procedure sqlite3_result_int(pCtx: PSQLite3Context; iVal: Integer); cdecl; external sqlite3_lib; 555 | procedure sqlite3_result_int64(pCtx: PSQLite3Context; iVal: Int64); cdecl; external sqlite3_lib; 556 | procedure sqlite3_result_null(pCtx: PSQLite3Context); cdecl; external sqlite3_lib; 557 | procedure sqlite3_result_text(pCtx: PSQLite3Context; const z: PAnsiChar; n: Integer; xDel: TSQLite3DestructorType); cdecl; external sqlite3_lib; 558 | procedure sqlite3_result_text16(pCtx: PSQLite3Context; const z: PWideChar; n: Integer; xDel: TSQLite3DestructorType); cdecl; external sqlite3_lib; 559 | procedure sqlite3_result_text16le(pCtx: PSQLite3Context; const z: Pointer; n: Integer; xDel: TSQLite3DestructorType); cdecl; external sqlite3_lib; 560 | procedure sqlite3_result_text16be(pCtx: PSQLite3Context; const z: Pointer; n: Integer; xDel: TSQLite3DestructorType); cdecl; external sqlite3_lib; 561 | procedure sqlite3_result_value(pCtx: PSQLite3Context; pValue: PSQLite3Value); cdecl; external sqlite3_lib; 562 | procedure sqlite3_result_zeroblob(pCtx: PSQLite3Context; n: Integer); cdecl; external sqlite3_lib; 563 | 564 | type 565 | TSQLite3CollationCompare = function(pUser: Pointer; n1: Integer; const z1: Pointer; n2: Integer; const z2: Pointer): Integer; cdecl; 566 | TSQLite3CollationDestructor = procedure(pUser: Pointer); cdecl; 567 | 568 | function sqlite3_create_collation(db: PSQLite3; const zName: PAnsiChar; eTextRep: Integer; pUser: Pointer; xCompare: TSQLite3CollationCompare): Integer; cdecl; external sqlite3_lib; 569 | function sqlite3_create_collation_v2(db: PSQLite3; const zName: PAnsiChar; eTextRep: Integer; pUser: Pointer; xCompare: TSQLite3CollationCompare; xDestroy: TSQLite3CollationDestructor): Integer; cdecl; external sqlite3_lib; 570 | function sqlite3_create_collation16(db: PSQLite3; const zName: PWideChar; eTextRep: Integer; pUser: Pointer; xCompare: TSQLite3CollationCompare): Integer; cdecl; external sqlite3_lib; 571 | 572 | type 573 | TSQLite3CollationNeededCallback = procedure(pCollNeededArg: Pointer; db: PSQLite3; eTextRep: Integer; const zExternal: PAnsiChar); cdecl; 574 | TSQLite3CollationNeededCallback16 = procedure(pCollNeededArg: Pointer; db: PSQLite3; eTextRep: Integer; const zExternal: PWideChar); cdecl; 575 | 576 | function sqlite3_collation_needed(db: PSQLite3; pCollNeededArg: Pointer; xCollNeeded: TSQLite3CollationNeededCallback): Integer; cdecl; external sqlite3_lib; 577 | function sqlite3_collation_needed16(db: PSQLite3; pCollNeededArg: Pointer; xCollNeeded16: TSQLite3CollationNeededCallback16): Integer; cdecl; external sqlite3_lib; 578 | 579 | //function sqlite3_key(db: PSQLite3; const pKey: Pointer; nKey: Integer): Integer; cdecl; external sqlite3_lib; 580 | 581 | //function sqlite3_rekey(db: PSQLite3; const pKey: Pointer; nKey: Integer): Integer; cdecl; external sqlite3_lib; 582 | 583 | function sqlite3_sleep(ms: Integer): Integer; cdecl; external sqlite3_lib; 584 | 585 | //var sqlite3_temp_directory: PAnsiChar; 586 | 587 | function sqlite3_get_autocommit(db: PSQLite3): Integer; cdecl; external sqlite3_lib; 588 | 589 | function sqlite3_db_handle(pStmt: PSQLite3Stmt): PSQLite3; cdecl; external sqlite3_lib; 590 | 591 | function sqlite3_next_stmt(pDb: PSQLite3; pStmt: PSQLite3Stmt): PSQLite3Stmt; cdecl; external sqlite3_lib; 592 | 593 | type 594 | TSQLite3CommitCallback = function(pCommitArg: Pointer): Integer; cdecl; 595 | TSQLite3RollbackCallback = procedure(pRollbackArg: Pointer); cdecl; 596 | 597 | function sqlite3_commit_hook(db: PSQLite3; xCallback: TSQLite3CommitCallback; pArg: Pointer): Pointer; cdecl; external sqlite3_lib; 598 | function sqlite3_rollback_hook(db: PSQLite3; xCallback: TSQLite3RollbackCallback; pArg: Pointer): Pointer; cdecl; external sqlite3_lib; 599 | 600 | type 601 | TSQLite3UpdateCallback = procedure(pUpdateArg: Pointer; op: Integer; const zDb: PAnsiChar; const zTbl: PAnsiChar; iKey: Int64); cdecl; 602 | 603 | function sqlite3_update_hook(db: PSQLite3; xCallback: TSQLite3UpdateCallback; pArg: Pointer): Pointer; cdecl; external sqlite3_lib; 604 | 605 | function sqlite3_enable_shared_cache(enable: Integer): Integer; cdecl; external sqlite3_lib; 606 | 607 | function sqlite3_release_memory(n: Integer): Integer; cdecl; external sqlite3_lib; 608 | 609 | procedure sqlite3_soft_heap_limit(n: Integer); cdecl; external sqlite3_lib; 610 | 611 | {$IFDEF SQLITE_ENABLE_COLUMN_METADATA} 612 | function sqlite3_table_column_metadata(db: PSQLite3; const zDbName: PAnsiChar; const zTableName: PAnsiChar; const zColumnName: PAnsiChar; const pzDataType: PPAnsiChar; const pzCollSeq: PPAnsiChar; pNotNull: PInteger; pPrimaryKey: PInteger; pAutoinc: PInteger): Integer; cdecl; external sqlite3_lib; 613 | {$ENDIF} 614 | 615 | function sqlite3_load_extension(db: PSQLite3; const zFile: PAnsiChar; const zProc: PAnsiChar; pzErrMsg: PPAnsiChar): Integer; cdecl; external sqlite3_lib; 616 | 617 | function sqlite3_enable_load_extension(db: PSQLite3; onoff: Integer): Integer; cdecl; external sqlite3_lib; 618 | 619 | type 620 | TSQLiteAutoExtensionEntryPoint = procedure; cdecl; 621 | 622 | function sqlite3_auto_extension(xEntryPoint: TSQLiteAutoExtensionEntryPoint): Integer; cdecl; external sqlite3_lib; 623 | 624 | procedure sqlite3_reset_auto_extension; cdecl; external sqlite3_lib; 625 | 626 | {$IFDEF SQLITE_EXPERIMENTAL} 627 | type 628 | TSQLite3FTS3Func = procedure(pContext: PSQLite3Context; argc: Integer; argv: PPSQLite3ValueArray); cdecl; 629 | 630 | type 631 | PSQLite3VTab = ^TSQLite3VTab; 632 | PSQLite3IndexInfo = ^TSQLite3IndexInfo; 633 | PSQLite3VTabCursor = ^TSQLite3VTabCursor; 634 | PSQLite3Module = ^TSQLite3Module; 635 | 636 | sqlite3_module = record 637 | iVersion: Integer; 638 | xCreate: function(db: PSQLite3; pAux: Pointer; argc: Integer; const argv: PPAnsiCharArray; var ppVTab: PSQLite3VTab; var pzErr: PAnsiChar): Integer; cdecl; 639 | xConnect: function(db: PSQLite3; pAux: Pointer; argc: Integer; const argv: PPAnsiCharArray; var ppVTab: PSQLite3VTab; var pzErr: PAnsiChar): Integer; cdecl; 640 | xBestIndex: function(pVTab: PSQLite3VTab; pInfo: PSQLite3IndexInfo): Integer; cdecl; 641 | xDisconnect: function(pVTab: PSQLite3VTab): Integer; cdecl; 642 | xDestroy: function(pVTab: PSQLite3VTab): Integer; cdecl; 643 | xOpen: function(pVTab: PSQLite3VTab; var ppCursor: PSQLite3VTabCursor): Integer; cdecl; 644 | xClose: function(pVtabCursor: PSQLite3VTabCursor): Integer; cdecl; 645 | xFilter: function(pVtabCursor: PSQLite3VTabCursor; idxNum: Integer; const idxStr: PAnsiChar; argc: Integer; argv: PPSQLite3ValueArray): Integer; cdecl; 646 | xNext: function(pVtabCursor: PSQLite3VTabCursor): Integer; cdecl; 647 | xEof: function(pVtabCursor: PSQLite3VTabCursor): Integer; cdecl; 648 | xColumn: function(pVtabCursor: PSQLite3VTabCursor; sContext: PSQLite3Context; p2: Integer): Integer; cdecl; 649 | xRowid: function(pVtabCursor: PSQLite3VTabCursor; var pRowid: Int64): Integer; cdecl; 650 | xUpdate: function(pVtab: PSQLite3VTab; nArg: Integer; ppArg: PPSQLite3ValueArray; var pRowid: Int64): Integer; cdecl; 651 | xBegin: function(pVTab: PSQLite3VTab): Integer; cdecl; 652 | xSync: function(pVTab: PSQLite3VTab): Integer; cdecl; 653 | xCommit: function(pVTab: PSQLite3VTab): Integer; cdecl; 654 | xRollback: function(pVTab: PSQLite3VTab): Integer; cdecl; 655 | xFindFunction: function(pVtab: PSQLite3VTab; nArg: Integer; const zName: PAnsiChar; var pxFunc: TSQLite3FTS3Func; var ppArg: Pointer): Integer; cdecl; 656 | xRename: function(pVtab: PSQLite3VTab; const zNew: PAnsiChar): Integer; cdecl; 657 | end; 658 | TSQLite3Module = sqlite3_module; 659 | 660 | sqlite3_index_constraint = record 661 | iColumn: Integer; 662 | op: Byte; 663 | usable: Byte; 664 | iTermOffset: Integer; 665 | end; 666 | TSQLite3IndexConstraint = sqlite3_index_constraint; 667 | 668 | PSQLite3IndexConstraintArray = ^TSQLite3IndexConstraintArray; 669 | TSQLite3IndexConstraintArray = array[0..MaxInt div SizeOf(TSQLite3IndexConstraint) - 1] of TSQLite3IndexConstraint; 670 | 671 | sqlite3_index_orderby = record 672 | iColumn: Integer; 673 | desc: Byte; 674 | end; 675 | TSQLite3IndexOrderBy = sqlite3_index_orderby; 676 | 677 | PSQLite3IndexOrderByArray = ^TSQLite3IndexOrderByArray; 678 | TSQLite3IndexOrderByArray = array[0..MaxInt div SizeOf(TSQLite3IndexOrderBy) - 1] of TSQLite3IndexOrderBy; 679 | 680 | sqlite3_index_constraint_usage = record 681 | argvIndex: Integer; 682 | omit: Byte; 683 | end; 684 | TSQLite3IndexConstraintUsage = sqlite3_index_constraint_usage; 685 | 686 | PSQLite3IndexConstraintUsageArray = ^TSQLite3IndexConstraintUsageArray; 687 | TSQLite3IndexConstraintUsageArray = array[0..MaxInt div SizeOf(TSQLite3IndexConstraintUsage) - 1] of TSQLite3IndexConstraintUsage; 688 | 689 | sqlite3_index_info = record 690 | nConstraint: Integer; 691 | aConstraint: PSQLite3IndexConstraintArray; 692 | nOrderBy: Integer; 693 | aOrderBy: PSQLite3IndexOrderByArray; 694 | aConstraintUsage: PSQLite3IndexConstraintUsageArray; 695 | idxNum: Integer; 696 | idxStr: PAnsiChar; 697 | needToFreeIdxStr: Integer; 698 | orderByConsumed: Integer; 699 | estimatedCost: Double; 700 | end; 701 | TSQLite3IndexInfo = sqlite3_index_info; 702 | 703 | sqlite3_vtab = record 704 | pModule: PSQLite3Module; 705 | nRef: Integer; 706 | zErrMsg: PAnsiChar; 707 | end; 708 | TSQLite3VTab = sqlite3_vtab; 709 | 710 | sqlite3_vtab_cursor = record 711 | pVtab: PSQLite3VTab; 712 | end; 713 | TSQLite3VTabCursor = sqlite3_vtab_cursor; 714 | 715 | const 716 | SQLITE_INDEX_CONSTRAINT_EQ = 2; 717 | SQLITE_INDEX_CONSTRAINT_GT = 4; 718 | SQLITE_INDEX_CONSTRAINT_LE = 8; 719 | SQLITE_INDEX_CONSTRAINT_LT = 16; 720 | SQLITE_INDEX_CONSTRAINT_GE = 32; 721 | SQLITE_INDEX_CONSTRAINT_MATCH = 64; 722 | 723 | function sqlite3_create_module(db: PSQLite3; const zName: PAnsiChar; const p: PSQLite3Module; pClientData: Pointer): Integer; cdecl; external sqlite3_lib; 724 | 725 | type 726 | TSQLite3ModuleDestructor = procedure(pAux: Pointer); cdecl; 727 | 728 | function sqlite3_create_module_v2(db: PSQLite3; const zName: PAnsiChar; const p: PSQLite3Module; pClientData: Pointer; xDestroy: TSQLite3ModuleDestructor): Integer; cdecl; external sqlite3_lib; 729 | 730 | function sqlite3_declare_vtab(db: PSQLite3; const zSQL: PAnsiChar): Integer; cdecl; external sqlite3_lib; 731 | 732 | function sqlite3_overload_function(db: PSQLite3; const zFuncName: PAnsiChar; nArg: Integer): Integer; cdecl; external sqlite3_lib; 733 | {$ENDIF} 734 | 735 | type 736 | PSQLite3Blob = type Pointer; 737 | 738 | function sqlite3_blob_open(db: PSQLite3; const zDb: PAnsiChar; const zTable: PAnsiChar; const zColumn: PAnsiChar; iRow: Int64; flags: Integer; var ppBlob: PSQLite3Blob): Integer; cdecl; external sqlite3_lib; 739 | 740 | function sqlite3_blob_close(pBlob: PSQLite3Blob): Integer; cdecl; external sqlite3_lib; 741 | 742 | function sqlite3_blob_bytes(pBlob: PSQLite3Blob): Integer; cdecl; external sqlite3_lib; 743 | 744 | function sqlite3_blob_read(pBlob: PSQLite3Blob; Z: Pointer; N: Integer; iOffset: Integer): Integer; cdecl; external sqlite3_lib; 745 | 746 | function sqlite3_blob_write(pBlob: PSQLite3Blob; const z: Pointer; n: Integer; iOffset: Integer): Integer; cdecl; external sqlite3_lib; 747 | 748 | function sqlite3_vfs_find(const zVfsName: PAnsiChar): PSQLite3VFS; cdecl; external sqlite3_lib; 749 | function sqlite3_vfs_register(pVfs: PSQLite3VFS; makeDflt: Integer): Integer; cdecl; external sqlite3_lib; 750 | function sqlite3_vfs_unregister(pVfs: PSQLite3VFS): Integer; cdecl; external sqlite3_lib; 751 | 752 | function sqlite3_mutex_alloc(id: Integer): PSQLite3Mutex; cdecl; external sqlite3_lib; 753 | procedure sqlite3_mutex_free(p: PSQLite3Mutex); cdecl; external sqlite3_lib; 754 | procedure sqlite3_mutex_enter(p: PSQLite3Mutex); cdecl; external sqlite3_lib; 755 | function sqlite3_mutex_try(p: PSQLite3Mutex): Integer; cdecl; external sqlite3_lib; 756 | procedure sqlite3_mutex_leave(p: PSQLite3Mutex); cdecl; external sqlite3_lib; 757 | 758 | {$IFDEF SQLITE_EXPERIMENTAL} 759 | type 760 | sqlite3_mutex_methods = record 761 | xMutexInit: function: Integer; cdecl; 762 | xMutexEnd: function: Integer; cdecl; 763 | xMutexAlloc: function(id: Integer): PSQLite3Mutex; cdecl; 764 | xMutexFree: procedure(p: PSQLite3Mutex); cdecl; 765 | xMutexEnter: procedure(p: PSQLite3Mutex); cdecl; 766 | xMutexTry: function(p: PSQLite3Mutex): Integer; cdecl; 767 | xMutexLeave: procedure(p: PSQLite3Mutex); cdecl; 768 | xMutexHeld: function(p: PSQLite3Mutex): Integer; cdecl; 769 | xMutexNotheld: function(p: PSQLite3Mutex): Integer; cdecl; 770 | end; 771 | TSQLite3MutexMethods = sqlite3_mutex_methods; 772 | {$ENDIF} 773 | 774 | {$IFDEF SQLITE_DEBUG} 775 | function sqlite3_mutex_held(p: PSQLite3Mutex): Integer; cdecl; external sqlite3_lib; 776 | function sqlite3_mutex_notheld(p: PSQLite3Mutex): Integer; cdecl; external sqlite3_lib; 777 | {$ENDIF} 778 | 779 | const 780 | SQLITE_MUTEX_FAST = 0; 781 | SQLITE_MUTEX_RECURSIVE = 1; 782 | SQLITE_MUTEX_STATIC_MASTER = 2; 783 | SQLITE_MUTEX_STATIC_MEM = 3; 784 | SQLITE_MUTEX_STATIC_MEM2 = 4; 785 | SQLITE_MUTEX_STATIC_OPEN = 4; 786 | SQLITE_MUTEX_STATIC_PRNG = 5; 787 | SQLITE_MUTEX_STATIC_LRU = 6; 788 | SQLITE_MUTEX_STATIC_LRU2 = 7; 789 | 790 | function sqlite3_db_mutex(db: PSQLite3): PSQLite3Mutex; cdecl; external sqlite3_lib; 791 | 792 | function sqlite3_file_control(db: PSQLite3; const zDbName: PAnsiChar; op: Integer; pArg: Pointer): Integer; cdecl; external sqlite3_lib; 793 | 794 | function sqlite3_test_control(op: Integer{; ...}): Integer; cdecl; external sqlite3_lib; 795 | 796 | const 797 | SQLITE_TESTCTRL_FIRST = 5; 798 | SQLITE_TESTCTRL_PRNG_SAVE = 5; 799 | SQLITE_TESTCTRL_PRNG_RESTORE = 6; 800 | SQLITE_TESTCTRL_PRNG_RESET = 7; 801 | SQLITE_TESTCTRL_BITVEC_TEST = 8; 802 | SQLITE_TESTCTRL_FAULT_INSTALL = 9; 803 | SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS = 10; 804 | SQLITE_TESTCTRL_PENDING_BYTE = 11; 805 | SQLITE_TESTCTRL_ASSERT = 12; 806 | SQLITE_TESTCTRL_ALWAYS = 13; 807 | SQLITE_TESTCTRL_RESERVE = 14; 808 | SQLITE_TESTCTRL_OPTIMIZATIONS = 15; 809 | SQLITE_TESTCTRL_ISKEYWORD = 16; 810 | SQLITE_TESTCTRL_LAST = 16; 811 | 812 | {$IFDEF SQLITE_EXPERIMENTAL} 813 | function sqlite3_status(op: Integer; var pCurrent: Integer; var pHighwater: Integer; resetFlag: Integer): Integer; cdecl; external sqlite3_lib; 814 | 815 | const 816 | SQLITE_STATUS_MEMORY_USED = 0; 817 | SQLITE_STATUS_PAGECACHE_USED = 1; 818 | SQLITE_STATUS_PAGECACHE_OVERFLOW = 2; 819 | SQLITE_STATUS_SCRATCH_USED = 3; 820 | SQLITE_STATUS_SCRATCH_OVERFLOW = 4; 821 | SQLITE_STATUS_MALLOC_SIZE = 5; 822 | SQLITE_STATUS_PARSER_STACK = 6; 823 | SQLITE_STATUS_PAGECACHE_SIZE = 7; 824 | SQLITE_STATUS_SCRATCH_SIZE = 8; 825 | 826 | function sqlite3_db_status(db: PSQLite3; op: Integer; var pCur: Integer; var pHiwtr: Integer; resetFlg: Integer): Integer; cdecl; external sqlite3_lib; 827 | 828 | const 829 | SQLITE_DBSTATUS_LOOKASIDE_USED = 0; 830 | 831 | function sqlite3_stmt_status(pStmt: PSQLite3Stmt; op: Integer; resetFlg: Integer): Integer; cdecl; external sqlite3_lib; 832 | 833 | const 834 | SQLITE_STMTSTATUS_FULLSCAN_STEP = 1; 835 | SQLITE_STMTSTATUS_SORT = 2; 836 | 837 | type 838 | PSQLite3PCache = type Pointer; 839 | 840 | type 841 | sqlite3_pcache_methods = record 842 | pArg: Pointer; 843 | xInit: function(pArg: Pointer): Integer; cdecl; 844 | xShutdown: procedure(pArg: Pointer); cdecl; 845 | xCreate: function(szPage: Integer; bPurgeable: Integer): PSQLite3PCache; cdecl; 846 | xCachesize: procedure(pCache: PSQLite3PCache; nCachesize: Integer); cdecl; 847 | xPagecount: function(pCache: PSQLite3PCache): Integer; cdecl; 848 | xFetch: function(pCache: PSQLite3PCache; key: Cardinal; createFlag: Integer): Pointer; cdecl; 849 | xUnpin: procedure(pCache: PSQLite3PCache; pPg: Pointer; discard: Integer); cdecl; 850 | xRekey: procedure(pCache: PSQLite3PCache; pPg: Pointer; oldKey: Cardinal; newKey: Cardinal); cdecl; 851 | xTruncate: procedure(pCache: PSQLite3PCache; iLimit: Cardinal); cdecl; 852 | xDestroy: procedure(pCache: PSQLite3PCache); cdecl; 853 | end; 854 | TSQLite3PCacheMethods = sqlite3_pcache_methods; 855 | 856 | type 857 | PSQLite3Backup = type Pointer; 858 | 859 | function sqlite3_backup_init(pDest: PSQLite3; const zDestName: PAnsiChar; pSource: PSQLite3; const zSourceName: PAnsiChar): PSQLite3Backup; cdecl; external sqlite3_lib; 860 | function sqlite3_backup_step(p: PSQLite3Backup; nPage: Integer): Integer; cdecl; external sqlite3_lib; 861 | function sqlite3_backup_finish(p: PSQLite3Backup): Integer; cdecl; external sqlite3_lib; 862 | function sqlite3_backup_remaining(p: PSQLite3Backup): Integer; cdecl; external sqlite3_lib; 863 | function sqlite3_backup_pagecount(p: PSQLite3Backup): Integer; cdecl; external sqlite3_lib; 864 | 865 | {$IFDEF SQLITE_ENABLE_UNLOCK_NOTIFY} 866 | type 867 | TSQLite3UnlockNotifyCallback = procedure(apArg: PPointerArray; nArg: Integer); cdecl; 868 | 869 | function sqlite3_unlock_notify(pBlocked: PSQLite3; xNotify: TSQLite3UnlockNotifyCallback; pNotifyArg: Pointer): Integer; cdecl; external sqlite3_lib; 870 | {$ENDIF} 871 | 872 | function sqlite3_strnicmp(const zLeft: PAnsiChar; const zRight: PAnsiChar; N: Integer): Integer; cdecl; external sqlite3_lib; 873 | {$ENDIF} 874 | 875 | //function sqlite3_win32_mbcs_to_utf8(const S: PAnsiChar): PAnsiChar; cdecl; external sqlite3_lib; 876 | 877 | implementation 878 | 879 | end. 880 | -------------------------------------------------------------------------------- /Source/SQLite3Utils.pas: -------------------------------------------------------------------------------- 1 | {* 2 | * SQLite for Delphi and FreePascal/Lazarus 3 | * 4 | * This unit contains miscellaneous utility functions. 5 | * 6 | * Copyright (c) 2013 Yuri Plashenkov 7 | * 8 | * MIT License 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy 11 | * of this software and associated documentation files (the "Software"), to deal 12 | * in the Software without restriction, including without limitation the rights 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | * copies of the Software, and to permit persons to whom the Software is 15 | * furnished to do so, subject to the following conditions: 16 | 17 | * The above copyright notice and this permission notice shall be included in all 18 | * copies or substantial portions of the Software. 19 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | * SOFTWARE. 27 | *} 28 | 29 | unit SQLite3Utils; 30 | 31 | {$IFDEF FPC} 32 | {$MODE DELPHI} 33 | {$ENDIF} 34 | 35 | interface 36 | 37 | function StrToUTF8(const S: WideString): AnsiString; 38 | function UTF8ToStr(const S: PAnsiChar; const Len: Integer = -1): WideString; 39 | function QuotedStr(const S: WideString): WideString; 40 | function FloatToSQLStr(Value: Extended): WideString; 41 | 42 | implementation 43 | 44 | uses 45 | {$IFNDEF FPC}Windows,{$ENDIF} SysUtils; 46 | 47 | function StrToUTF8(const S: WideString): AnsiString; 48 | begin 49 | Result := UTF8Encode(S); 50 | end; 51 | 52 | function UTF8ToStr(const S: PAnsiChar; const Len: Integer): WideString; 53 | var 54 | UTF8Str: AnsiString; 55 | begin 56 | if Len < 0 then 57 | begin 58 | Result := UTF8Decode(S); 59 | end 60 | else if Len > 0 then 61 | begin 62 | SetLength(UTF8Str, Len); 63 | Move(S^, UTF8Str[1], Len); 64 | Result := UTF8Decode(UTF8Str); 65 | end 66 | else Result := ''; 67 | end; 68 | 69 | function QuotedStr(const S: WideString): WideString; 70 | const 71 | Quote = #39; 72 | var 73 | I: Integer; 74 | begin 75 | Result := S; 76 | for I := Length(Result) downto 1 do 77 | if Result[I] = Quote then Insert(Quote, Result, I); 78 | Result := Quote + Result + Quote; 79 | end; 80 | 81 | function FloatToSQLStr(Value: Extended): WideString; 82 | var 83 | FS: TFormatSettings; 84 | begin 85 | {$IFDEF FPC} 86 | FS := DefaultFormatSettings; 87 | {$ELSE} 88 | GetLocaleFormatSettings(GetThreadLocale, FS); 89 | {$ENDIF} 90 | FS.DecimalSeparator := '.'; 91 | Result := FloatToStr(Value, FS); 92 | end; 93 | 94 | end. 95 | -------------------------------------------------------------------------------- /Source/SQLite3Wrap.pas: -------------------------------------------------------------------------------- 1 | {* 2 | * SQLite for Delphi and FreePascal/Lazarus 3 | * 4 | * This unit contains easy-to-use object wrapper over SQLite3 API functions. 5 | * 6 | * Copyright (c) 2013 Yuri Plashenkov 7 | * 8 | * MIT License 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy 11 | * of this software and associated documentation files (the "Software"), to deal 12 | * in the Software without restriction, including without limitation the rights 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | * copies of the Software, and to permit persons to whom the Software is 15 | * furnished to do so, subject to the following conditions: 16 | 17 | * The above copyright notice and this permission notice shall be included in all 18 | * copies or substantial portions of the Software. 19 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | * SOFTWARE. 27 | *} 28 | 29 | unit SQLite3Wrap; 30 | 31 | {$IFDEF FPC} 32 | {$MODE DELPHI} 33 | {$ENDIF} 34 | 35 | interface 36 | 37 | uses 38 | SysUtils, Classes, SQLite3; 39 | 40 | type 41 | ESQLite3Error = class(Exception); 42 | 43 | TSQLite3Statement = class; 44 | TSQLite3BlobHandler = class; 45 | 46 | { TSQLite3Database class } 47 | 48 | TSQLite3Database = class(TObject) 49 | private 50 | FHandle: PSQLite3; 51 | FStatementList: TList; 52 | FBlobHandlerList: TList; 53 | FTransactionOpen: Boolean; 54 | procedure Check(const ErrCode: Integer); 55 | procedure CheckHandle; 56 | public 57 | constructor Create; 58 | destructor Destroy; override; 59 | 60 | procedure Open(const FileName: WideString; Flags: Integer = 0); 61 | procedure Close; 62 | 63 | procedure Execute(const SQL: WideString); 64 | function LastInsertRowID: Int64; 65 | function Prepare(const SQL: WideString): TSQLite3Statement; 66 | function BlobOpen(const Table, Column: WideString; const RowID: Int64; const WriteAccess: Boolean = True): TSQLite3BlobHandler; 67 | 68 | procedure BeginTransaction; 69 | procedure Commit; 70 | procedure Rollback; 71 | 72 | property Handle: PSQLite3 read FHandle; 73 | property TransactionOpen: Boolean read FTransactionOpen; 74 | end; 75 | 76 | { TSQLite3Statement class } 77 | 78 | TSQLite3Statement = class(TObject) 79 | private 80 | FHandle: PSQLite3Stmt; 81 | FOwnerDatabase: TSQLite3Database; 82 | function ParamIndexByName(const ParamName: WideString): Integer; 83 | public 84 | constructor Create(OwnerDatabase: TSQLite3Database; const SQL: WideString); 85 | destructor Destroy; override; 86 | 87 | procedure BindInt(const ParamIndex: Integer; const Value: Integer); overload; 88 | procedure BindInt64(const ParamIndex: Integer; const Value: Int64); overload; 89 | procedure BindDouble(const ParamIndex: Integer; const Value: Double); overload; 90 | procedure BindText(const ParamIndex: Integer; const Value: WideString); overload; 91 | procedure BindNull(const ParamIndex: Integer); overload; 92 | procedure BindBlob(const ParamIndex: Integer; Data: Pointer; const Size: Integer); overload; 93 | procedure BindZeroBlob(const ParamIndex: Integer; const Size: Integer); overload; 94 | procedure BindInt(const ParamName: WideString; const Value: Integer); overload; 95 | procedure BindInt64(const ParamName: WideString; const Value: Int64); overload; 96 | procedure BindDouble(const ParamName: WideString; const Value: Double); overload; 97 | procedure BindText(const ParamName: WideString; const Value: WideString); overload; 98 | procedure BindNull(const ParamName: WideString); overload; 99 | procedure BindBlob(const ParamName: WideString; Data: Pointer; const Size: Integer); overload; 100 | procedure BindZeroBlob(const ParamName: WideString; const Size: Integer); overload; 101 | procedure ClearBindings; 102 | 103 | function Step: Integer; 104 | procedure Reset; 105 | function StepAndReset: Integer; 106 | 107 | function ColumnCount: Integer; 108 | function ColumnName(const ColumnIndex: Integer): WideString; 109 | function ColumnType(const ColumnIndex: Integer): Integer; 110 | function ColumnInt(const ColumnIndex: Integer): Integer; 111 | function ColumnInt64(const ColumnIndex: Integer): Int64; 112 | function ColumnDouble(const ColumnIndex: Integer): Double; 113 | function ColumnText(const ColumnIndex: Integer): WideString; 114 | function ColumnBlob(const ColumnIndex: Integer): Pointer; 115 | function ColumnBytes(const ColumnIndex: Integer): Integer; 116 | 117 | property Handle: PSQLite3Stmt read FHandle; 118 | property OwnerDatabase: TSQLite3Database read FOwnerDatabase; 119 | end; 120 | 121 | { TSQLite3BlobHandler class } 122 | 123 | TSQLite3BlobHandler = class(TObject) 124 | private 125 | FHandle: PSQLite3Blob; 126 | FOwnerDatabase: TSQLite3Database; 127 | public 128 | constructor Create(OwnerDatabase: TSQLite3Database; const Table, Column: WideString; const RowID: Int64; const WriteAccess: Boolean = True); 129 | destructor Destroy; override; 130 | 131 | function Bytes: Integer; 132 | procedure Read(Buffer: Pointer; const Size, Offset: Integer); 133 | procedure Write(Buffer: Pointer; const Size, Offset: Integer); 134 | 135 | property Handle: PSQLite3Blob read FHandle; 136 | property OwnerDatabase: TSQLite3Database read FOwnerDatabase; 137 | end; 138 | 139 | implementation 140 | 141 | uses 142 | SQLite3Utils; 143 | 144 | resourcestring 145 | SErrorMessage = 'SQLite3 error: %s'; 146 | SDatabaseNotConnected = 'SQLite3 error: database is not connected.'; 147 | STransactionAlreadyOpen = 'Transaction is already opened.'; 148 | SNoTransactionOpen = 'No transaction is open'; 149 | 150 | { TSQLite3Database } 151 | 152 | procedure TSQLite3Database.BeginTransaction; 153 | begin 154 | if not FTransactionOpen then 155 | begin 156 | Execute('BEGIN TRANSACTION;'); 157 | FTransactionOpen := True; 158 | end 159 | else 160 | raise ESQLite3Error.Create(STransactionAlreadyOpen); 161 | end; 162 | 163 | function TSQLite3Database.BlobOpen(const Table, Column: WideString; 164 | const RowID: Int64; const WriteAccess: Boolean): TSQLite3BlobHandler; 165 | begin 166 | Result := TSQLite3BlobHandler.Create(Self, Table, Column, RowID, WriteAccess); 167 | end; 168 | 169 | procedure TSQLite3Database.Check(const ErrCode: Integer); 170 | begin 171 | if ErrCode <> SQLITE_OK then 172 | raise ESQLite3Error.CreateFmt(SErrorMessage, [UTF8ToStr(sqlite3_errmsg(FHandle))]); 173 | end; 174 | 175 | procedure TSQLite3Database.CheckHandle; 176 | begin 177 | if FHandle = nil then 178 | raise ESQLite3Error.Create(SDatabaseNotConnected); 179 | end; 180 | 181 | procedure TSQLite3Database.Close; 182 | var 183 | I: Integer; 184 | begin 185 | if FHandle <> nil then 186 | begin 187 | if FTransactionOpen then 188 | Rollback; 189 | // Delete all statements 190 | for I := FStatementList.Count - 1 downto 0 do 191 | TSQLite3Statement(FStatementList[I]).Free; 192 | // Delete all blob handlers 193 | for I := FBlobHandlerList.Count - 1 downto 0 do 194 | TSQLite3BlobHandler(FBlobHandlerList[I]).Free; 195 | sqlite3_close(FHandle); 196 | FHandle := nil; 197 | end; 198 | end; 199 | 200 | procedure TSQLite3Database.Commit; 201 | begin 202 | if FTransactionOpen then 203 | begin 204 | Execute('COMMIT;'); 205 | FTransactionOpen := False; 206 | end 207 | else 208 | raise ESQLite3Error.Create(SNoTransactionOpen); 209 | end; 210 | 211 | constructor TSQLite3Database.Create; 212 | begin 213 | FHandle := nil; 214 | FStatementList := TList.Create; 215 | FBlobHandlerList := TList.Create; 216 | end; 217 | 218 | destructor TSQLite3Database.Destroy; 219 | begin 220 | Close; 221 | FBlobHandlerList.Free; 222 | FStatementList.Free; 223 | inherited; 224 | end; 225 | 226 | procedure TSQLite3Database.Execute(const SQL: WideString); 227 | begin 228 | CheckHandle; 229 | Check(sqlite3_exec(FHandle, PAnsiChar(StrToUTF8(SQL)), nil, nil, nil)); 230 | end; 231 | 232 | function TSQLite3Database.LastInsertRowID: Int64; 233 | begin 234 | CheckHandle; 235 | Result := sqlite3_last_insert_rowid(FHandle); 236 | end; 237 | 238 | procedure TSQLite3Database.Open(const FileName: WideString; Flags: Integer); 239 | begin 240 | Close; 241 | if Flags = 0 then 242 | Check(sqlite3_open(PAnsiChar(StrToUTF8(FileName)), FHandle)) 243 | else 244 | Check(sqlite3_open_v2(PAnsiChar(StrToUTF8(FileName)), FHandle, Flags, nil)); 245 | end; 246 | 247 | function TSQLite3Database.Prepare(const SQL: WideString): TSQLite3Statement; 248 | begin 249 | Result := TSQLite3Statement.Create(Self, SQL); 250 | end; 251 | 252 | procedure TSQLite3Database.Rollback; 253 | begin 254 | if FTransactionOpen then 255 | begin 256 | Execute('ROLLBACK;'); 257 | FTransactionOpen := False; 258 | end 259 | else 260 | raise ESQLite3Error.Create(SNoTransactionOpen); 261 | end; 262 | 263 | { TSQLite3Statement } 264 | 265 | procedure TSQLite3Statement.BindBlob(const ParamIndex: Integer; Data: Pointer; 266 | const Size: Integer); 267 | begin 268 | FOwnerDatabase.Check(sqlite3_bind_blob(FHandle, ParamIndex, Data, Size, SQLITE_TRANSIENT)); 269 | end; 270 | 271 | procedure TSQLite3Statement.BindDouble(const ParamIndex: Integer; 272 | const Value: Double); 273 | begin 274 | FOwnerDatabase.Check(sqlite3_bind_double(FHandle, ParamIndex, Value)); 275 | end; 276 | 277 | procedure TSQLite3Statement.BindInt(const ParamIndex, Value: Integer); 278 | begin 279 | FOwnerDatabase.Check(sqlite3_bind_int(FHandle, ParamIndex, Value)); 280 | end; 281 | 282 | procedure TSQLite3Statement.BindInt64(const ParamIndex: Integer; 283 | const Value: Int64); 284 | begin 285 | FOwnerDatabase.Check(sqlite3_bind_int64(FHandle, ParamIndex, Value)); 286 | end; 287 | 288 | procedure TSQLite3Statement.BindNull(const ParamIndex: Integer); 289 | begin 290 | FOwnerDatabase.Check(sqlite3_bind_null(FHandle, ParamIndex)); 291 | end; 292 | 293 | procedure TSQLite3Statement.BindText(const ParamIndex: Integer; 294 | const Value: WideString); 295 | var 296 | S: AnsiString; { UTF-8 string } 297 | begin 298 | S := StrToUTF8(Value); 299 | FOwnerDatabase.Check( 300 | sqlite3_bind_text(FHandle, ParamIndex, PAnsiChar(S), Length(S), SQLITE_TRANSIENT) 301 | ); 302 | end; 303 | 304 | procedure TSQLite3Statement.BindZeroBlob(const ParamIndex, Size: Integer); 305 | begin 306 | FOwnerDatabase.Check(sqlite3_bind_zeroblob(FHandle, ParamIndex, Size)); 307 | end; 308 | 309 | procedure TSQLite3Statement.ClearBindings; 310 | begin 311 | FOwnerDatabase.Check(sqlite3_clear_bindings(FHandle)); 312 | end; 313 | 314 | function TSQLite3Statement.ColumnBlob(const ColumnIndex: Integer): Pointer; 315 | begin 316 | Result := sqlite3_column_blob(FHandle, ColumnIndex); 317 | end; 318 | 319 | function TSQLite3Statement.ColumnBytes(const ColumnIndex: Integer): Integer; 320 | begin 321 | Result := sqlite3_column_bytes(FHandle, ColumnIndex); 322 | end; 323 | 324 | function TSQLite3Statement.ColumnCount: Integer; 325 | begin 326 | Result := sqlite3_column_count(FHandle); 327 | end; 328 | 329 | function TSQLite3Statement.ColumnDouble(const ColumnIndex: Integer): Double; 330 | begin 331 | Result := sqlite3_column_double(FHandle, ColumnIndex); 332 | end; 333 | 334 | function TSQLite3Statement.ColumnInt(const ColumnIndex: Integer): Integer; 335 | begin 336 | Result := sqlite3_column_int(FHandle, ColumnIndex); 337 | end; 338 | 339 | function TSQLite3Statement.ColumnInt64(const ColumnIndex: Integer): Int64; 340 | begin 341 | Result := sqlite3_column_int64(FHandle, ColumnIndex); 342 | end; 343 | 344 | function TSQLite3Statement.ColumnName(const ColumnIndex: Integer): WideString; 345 | begin 346 | Result := UTF8ToStr(sqlite3_column_name(FHandle, ColumnIndex)); 347 | end; 348 | 349 | function TSQLite3Statement.ColumnText(const ColumnIndex: Integer): WideString; 350 | var 351 | Len: Integer; 352 | begin 353 | Len := ColumnBytes(ColumnIndex); 354 | Result := UTF8ToStr(sqlite3_column_text(FHandle, ColumnIndex), Len); 355 | end; 356 | 357 | function TSQLite3Statement.ColumnType(const ColumnIndex: Integer): Integer; 358 | begin 359 | Result := sqlite3_column_type(FHandle, ColumnIndex); 360 | end; 361 | 362 | constructor TSQLite3Statement.Create(OwnerDatabase: TSQLite3Database; 363 | const SQL: WideString); 364 | begin 365 | FOwnerDatabase := OwnerDatabase; 366 | FOwnerDatabase.CheckHandle; 367 | FOwnerDatabase.Check( 368 | sqlite3_prepare_v2(FOwnerDatabase.Handle, PAnsiChar(StrToUTF8(SQL)), -1, FHandle, nil) 369 | ); 370 | FOwnerDatabase.FStatementList.Add(Self); 371 | end; 372 | 373 | destructor TSQLite3Statement.Destroy; 374 | begin 375 | FOwnerDatabase.FStatementList.Remove(Self); 376 | sqlite3_finalize(FHandle); 377 | inherited; 378 | end; 379 | 380 | function TSQLite3Statement.ParamIndexByName(const ParamName: WideString): Integer; 381 | begin 382 | Result := sqlite3_bind_parameter_index(FHandle, PAnsiChar(StrToUTF8(ParamName))); 383 | end; 384 | 385 | procedure TSQLite3Statement.Reset; 386 | begin 387 | sqlite3_reset(FHandle); 388 | end; 389 | 390 | function TSQLite3Statement.Step: Integer; 391 | begin 392 | Result := sqlite3_step(FHandle); 393 | end; 394 | 395 | function TSQLite3Statement.StepAndReset: Integer; 396 | begin 397 | Result := Step; 398 | Reset; 399 | end; 400 | 401 | procedure TSQLite3Statement.BindBlob(const ParamName: WideString; Data: Pointer; 402 | const Size: Integer); 403 | begin 404 | BindBlob(ParamIndexByName(ParamName), Data, Size); 405 | end; 406 | 407 | procedure TSQLite3Statement.BindDouble(const ParamName: WideString; 408 | const Value: Double); 409 | begin 410 | BindDouble(ParamIndexByName(ParamName), Value); 411 | end; 412 | 413 | procedure TSQLite3Statement.BindInt(const ParamName: WideString; 414 | const Value: Integer); 415 | begin 416 | BindInt(ParamIndexByName(ParamName), Value); 417 | end; 418 | 419 | procedure TSQLite3Statement.BindInt64(const ParamName: WideString; 420 | const Value: Int64); 421 | begin 422 | BindInt64(ParamIndexByName(ParamName), Value); 423 | end; 424 | 425 | procedure TSQLite3Statement.BindNull(const ParamName: WideString); 426 | begin 427 | BindNull(ParamIndexByName(ParamName)); 428 | end; 429 | 430 | procedure TSQLite3Statement.BindText(const ParamName, Value: WideString); 431 | begin 432 | BindText(ParamIndexByName(ParamName), Value); 433 | end; 434 | 435 | procedure TSQLite3Statement.BindZeroBlob(const ParamName: WideString; 436 | const Size: Integer); 437 | begin 438 | BindZeroBlob(ParamIndexByName(ParamName), Size); 439 | end; 440 | 441 | { TSQLite3BlobHandler } 442 | 443 | function TSQLite3BlobHandler.Bytes: Integer; 444 | begin 445 | Result := sqlite3_blob_bytes(FHandle); 446 | end; 447 | 448 | constructor TSQLite3BlobHandler.Create(OwnerDatabase: TSQLite3Database; const Table, 449 | Column: WideString; const RowID: Int64; const WriteAccess: Boolean); 450 | begin 451 | FOwnerDatabase := OwnerDatabase; 452 | FOwnerDatabase.CheckHandle; 453 | FOwnerDatabase.Check( 454 | sqlite3_blob_open(FOwnerDatabase.FHandle, 'main', PAnsiChar(StrToUTF8(Table)), 455 | PAnsiChar(StrToUTF8(Column)), RowID, Ord(WriteAccess), FHandle) 456 | ); 457 | FOwnerDatabase.FBlobHandlerList.Add(Self); 458 | end; 459 | 460 | destructor TSQLite3BlobHandler.Destroy; 461 | begin 462 | FOwnerDatabase.FBlobHandlerList.Remove(Self); 463 | sqlite3_blob_close(FHandle); 464 | inherited; 465 | end; 466 | 467 | procedure TSQLite3BlobHandler.Read(Buffer: Pointer; const Size, 468 | Offset: Integer); 469 | begin 470 | FOwnerDatabase.Check(sqlite3_blob_read(FHandle, Buffer, Size, Offset)); 471 | end; 472 | 473 | procedure TSQLite3BlobHandler.Write(Buffer: Pointer; const Size, 474 | Offset: Integer); 475 | begin 476 | FOwnerDatabase.Check(sqlite3_blob_write(FHandle, Buffer, Size, Offset)); 477 | end; 478 | 479 | end. 480 | --------------------------------------------------------------------------------