SQLiteTable3.pas ( File view )

  • By adm.maniaco 2014-09-01
  • View(s):170
  • Download(s):11
  • Point(s): 2
			unit SQLiteTable3;

{

  Simple classes for using SQLite's exec and get_table.

  TSQLiteDatabase wraps the calls to open and close an SQLite database.
  It also wraps SQLite_exec for queries that do not return a result set

  TSQLiteTable wraps execution of SQL query.
  It run query and read all returned rows to internal buffer.
  It allows accessing fields by name as well as index and can move through a
  result set forward and backwards, or randomly to any row.

  TSQLiteUniTable wraps execution of SQL query.
  It run query as TSQLiteTable, but reading just first row only!
  You can step to next row (until not EOF) by 'Next' method.
  You cannot step backwards! (So, it is called as UniDirectional result set.)
  It not using any internal buffering, this class is very close to Sqlite API.
  It allows accessing fields by name as well as index on actual row only.
  Very good and fast for sequentional scanning of large result sets with minimal
    memory footprint.

  Warning! Do not close TSQLiteDatabase before any TSQLiteUniTable,
    because query is closed on TSQLiteUniTable destructor and database connection
    is used during TSQLiteUniTable live!

  SQL parameter usage:
    You can add named parameter values by call set of AddParam* methods.
    Parameters will be used for first next SQL statement only.
    Parameter name must be prefixed by ':', '$' or '@' and same prefix must be
    used in SQL statement!
    Sample:
      table.AddParamText(':str', 'some value');
      s := table.GetTableString('SELECT value FROM sometable WHERE id=:str');

   Notes from Andrew Retmanski on prepared queries
   The changes are as follows:

   SQLiteTable3.pas
   - Added new boolean property Synchronised (this controls the SYNCHRONOUS pragma as I found that turning this OFF increased the write performance in my application)
   - Added new type TSQLiteQuery (this is just a simple record wrapper around the SQL string and a TSQLiteStmt pointer)
   - Added PrepareSQL method to prepare SQL query - returns TSQLiteQuery
   - Added ReleaseSQL method to release previously prepared query
   - Added overloaded BindSQL methods for Integer and String types - these set new values for the prepared query parameters
   - Added overloaded ExecSQL method to execute a prepared TSQLiteQuery

   Usage of the new methods should be self explanatory but the process is in essence:

   1. Call PrepareSQL to return TSQLiteQuery 2. Call BindSQL for each parameter in the prepared query 3. Call ExecSQL to run the prepared query 4. Repeat steps 2 & 3 as required 5. Call ReleaseSQL to free SQLite resources

   One other point - the Synchronised property throws an error if used inside a transaction.

   Acknowledments
   Adapted by Tim Anderson (tim@itwriting.com)
   Originally created by Pablo Pissanetzky (pablo@myhtpc.net)
   Modified and enhanced by Lukas Gebauer
   Modified and enhanced by Tobias Gunkel

}

interface

{
$IFDEF FPC
}
  {
$MODE Delphi
}{
$H+
}
{
$ENDIF
}

uses
  {
$IFDEF WIN32
}
  Windows,
  {
$ENDIF
}
  SQLite3, Classes, SysUtils;

const

  dtInt = 1;
  dtNumeric = 2;
  dtStr = 3;
  dtBlob = 4;
  dtNull = 5;

type

  ESQLiteException = class(Exception)
  end;

  TSQliteParam = class
  public
    name: string;
    valuetype: integer;
    valueinteger: int64;
    valuefloat: double;
    valuedata: string;
    valueBlob: TStream;
  end;

  THookQuery = procedure(Sender: TObject; SQL: String) of object;

  TSQLiteQuery = record
    SQL: String;
    Statement: TSQLiteStmt;
  end;

  TSQLiteTable = class;
  TSQLiteUniTable = class;

  TSQLiteDatabase = class
  private
    fDB: TSQLiteDB;
    fInTrans: boolean;
    fSync: boolean;
    fParams: TList;
    FOnQuery: THookQuery;
    procedure RaiseError(s: string; SQL: string);
    procedure SetParams(Stmt: TSQLiteStmt);
    procedure BindData(Stmt: TSQLiteStmt; const Bindings: array of const);
    function GetRowsChanged: integer;
  protected
    procedure SetSynchronised(Value: boolean);
    procedure DoQuery(value: string); 
  public
    constructor Create(const FileName: string);
    destructor Destroy; override;
    function GetTable(const SQL: Ansistring): TSQLiteTable; overload;
    function GetTable(const SQL: Ansistring; const Bindings: array of const): TSQLiteTable; overload;
    procedure ExecSQL(const SQL: Ansistring); overload;
    procedure ExecSQL(const SQL: Ansistring; const Bindings: array of const); overload;
    procedure ExecSQL(Query: TSQLiteQuery); overload;
    function PrepareSQL(const SQL: Ansistring): TSQLiteQuery;
    procedure BindSQL(Query: TSQLiteQuery; const Index: Integer; const Value: Integer); overload;
    procedure BindSQL(Query: TSQLiteQuery; const Index: Integer; const Value: String); overload;
    procedure ReleaseSQL(Query: TSQLiteQuery);
    function GetUniTable(const SQL: Ansistring): TSQLiteUniTable; overload;
    function GetUniTable(const SQL: Ansistring; const Bindings: array of const): TSQLiteUniTable; overload;
    function GetTableValue(const SQL: Ansistring): int64; overload;
    function GetTableValue(const SQL: Ansistring; const Bindings: array of const): int64; overload;
    function GetTableString(const SQL: Ansistring): string; overload;
    function GetTableString(const SQL: Ansistring; const Bindings: array of const): string; overload;
    procedure GetTableStrings(const SQL: Ansistring; const Value: TStrings);
    procedure UpdateBlob(const SQL: Ansistring; BlobData: TStream);
    procedure BeginTransaction;
    procedure Commit;
    procedure Rollback;
    function TableExists(TableName: string): boolean;
    function GetLastInsertRowID: int64;
    function GetLastChangedRows: int64;
    procedure SetTimeout(Value: integer);
    function Version: string;
    procedure AddCustomCollate(name: string; xCompare: TCollateXCompare);
    //adds collate named SYSTEM for correct data sorting by user's locale
    Procedure AddSystemCollate;
    procedure ParamsClear;
    procedure AddParamInt(name: string; value: int64);
    procedure AddParamFloat(name: string; value: double);
    procedure AddParamText(name: string; value: string);
    procedure AddParamBlob(name: string; value: TStream);
    procedure AddParamNull(name: string);
    property  DB: TSQLiteDB read fDB;

    property IsTransactionOpen: boolean read fInTrans;
    //database rows that were changed (or inserted or deleted) by the most recent SQL statement
    property RowsChanged : integer read getRowsChanged;
    property Synchronised: boolean read FSync write SetSynchronised;
    property OnQuery: THookQuery read FOnQuery write FOnQuery;
  end;

  TSQLiteTable = class
  private
    fResults: TList;
    fRowCount: cardinal;
    fColCount: cardinal;
    fCols: TStringList;
    fColTypes: TList;
    fRow: cardinal;
    function GetFields(I: cardinal): string;
    function GetEOF: boolean;
    function GetBOF: boolean;
    function GetColumns(I: integer): string;
    function GetFieldByName(FieldName: string): string;
    function GetFieldIndex(FieldName: string): integer;
    function GetCount: integer;
    function GetCountResult: integer;
  public
    constructor Create(DB: TSQLiteDatabase; const SQL: Ansistring); overload;
    constructor Create(DB: TSQLiteDatabase; const SQL: Ansistring; const Bindings: array of const); overload;
    destructor Destroy; override;
    function FieldAsInteger(I: cardinal): int64;
    function FieldAsBlob(I: cardinal): TMemoryStream;
    function FieldAsBlobText(I: cardinal): string;
    function FieldIsNull(I: cardinal): boolean;
    function FieldAsString(I: cardinal): string;
    function FieldAsDouble(I: cardinal): double;
    function Next: boolean;
    function Previous: boolean;
    property EOF: boolean read GetEOF;
    property BOF: boolean read GetBOF;
    property Fields[I: cardinal]: string read GetFields;
    property FieldByName[FieldName: string]: string read GetFieldByName;
    property FieldIndex[FieldName: string]: integer read GetFieldIndex;
    property Columns[I: integer]: string read GetColumns;
    property ColCount: cardinal read fColCount;
    property RowCount: cardinal read fRowCount;
    property Row: cardinal read fRow;
    function MoveFirst: boolean;
    function MoveLast: boolean;
    function MoveTo(position: cardinal): boolean;
    property Count: integer read GetCount;
    // The property CountResult is used when you execute count(*) queries.
    // It returns 0 if the result set is empty or the value of the
    // first field as an integer.
    property CountResult: integer read GetCountResult;
  end;

  TSQLiteUniTable = class
  private
    fColCount: cardinal;
    fCols: TStringList;
    fRow: cardinal;
    fEOF: boolean;
    fStmt: TSQLiteStmt;
    fDB: TSQLiteDatabase;
    fSQL: string;
    function GetFields(I: cardinal): string;
    function GetColumns(I: integer): string;
    function GetFieldByName(FieldName: string): string;
    function GetFieldIndex(FieldName: string): integer;
  public
    constructor Create(DB: TSQLiteDatabase; const SQL: Ansistring); overload;
    constructor Create(DB: TSQLiteDatabase; const SQL: Ansistring; const Bindings: array of const); overload;
    destructor Destroy; override;
    function FieldAsInteger(I: cardinal): int64;
    function FieldAsBlob(I: cardinal): TMemoryStream;
    function FieldAsBlobPtr(I: cardinal; out iNumBytes: integer): Pointer;
    function FieldAsBlobText(I: cardinal): string;
    function FieldIsNull(I: cardinal): boolean;
    function FieldAsString(I: cardinal): string;
    function FieldAsDouble(I: cardinal): double;
    function Next: boolean;
    property EOF: boolean read FEOF;
    property Fields[I: cardinal]: string read GetFields;
    property FieldByName[FieldName: string]: string read GetFieldByName;
    property FieldIndex[FieldName: string]: integer read GetFieldIndex;
    property Columns[I: integer]: string read GetColumns;
    property ColCount: cardinal read fColCount;
    property Row: cardinal read fRow;
  end;

procedure DisposePointer(ptr:
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 2

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
Aboutform.dcu7.81 kB29-05-10 03:22
AboutForm.dfm59.86 kB02-04-10 14:37
AboutForm.pas2.79 kB02-04-10 14:37
DataBase.dcu10.72 kB29-05-10 02:56
DataBase.pas7.02 kB02-04-10 14:37
NCore.dcu12.55 kB29-05-10 02:56
NCore.pas11.17 kB02-04-10 14:37
NDeviceManager.dcu24.08 kB29-05-10 02:56
NDeviceManager.pas20.58 kB26-05-10 01:21
NExtractors.dcu26.38 kB29-05-10 02:56
NExtractors.pas22.97 kB02-04-10 14:37
NImages.dcu18.89 kB29-05-10 02:56
NImages.pas16.86 kB02-04-10 14:37
NLicensing.dcu1.73 kB29-05-10 02:56
NLicensing.pas2.27 kB02-04-10 14:37
NMatchers.dcu10.16 kB29-05-10 02:56
NMatchers.pas8.69 kB02-04-10 14:37
NTemplates.dcu13.67 kB29-05-10 02:56
NTemplates.pas10.28 kB02-04-10 14:37
NUtils.dcu17.47 kB29-05-10 02:56
NUtils.pas14.14 kB02-04-10 14:37
SQLite3.dcu10.79 kB29-05-10 02:56
sqlite3.dll493.92 kB02-04-10 14:37
SQLite3.pas13.07 kB02-04-10 14:37
SQLiteTable3.dcu49.60 kB29-05-10 02:56
SQLiteTable3.pas44.43 kB02-04-10 14:37
<SQLite>0.00 B29-05-10 03:00
DataBase.pas.~1~7.02 kB02-04-10 14:37
DataBase.pas.~2~7.02 kB26-05-10 01:49
DataBase.pas.~3~7.03 kB26-05-10 01:51
NDeviceManager.pas.~1~20.57 kB02-04-10 14:37
<__history>0.00 B29-05-10 03:00
<Bibliotecas>0.00 B29-05-10 03:00
CameraForm.dcu7.54 kB29-05-10 03:22
CameraForm.dfm172.63 kB02-04-10 14:37
CameraForm.pas2.57 kB02-04-10 14:37
DataBase.dcu10.98 kB29-05-10 03:22
DataBase.pas7.02 kB02-04-10 14:37
EnrollmentForm.dcu4.85 kB29-05-10 03:22
EnrollmentForm.dfm171.08 kB02-04-10 14:37
EnrollmentForm.pas577.00 B02-04-10 14:37
FacesSamplePas.cfg473.00 B02-04-10 14:37
FacesSamplePas.dof2.57 kB02-04-10 14:37
FacesSamplePas.dpr1.00 kB02-04-10 14:37
FacesSamplePas.dproj5.96 kB29-05-10 03:20
FacesSamplePas.dproj.20074.74 kB02-04-10 14:37
FacesSamplePas.dproj.local62.00 B29-05-10 03:21
FacesSamplePas.identcache1.19 kB29-05-10 03:22
FacesSamplePas.res78.90 kB29-05-10 03:20
fPrincipal.dcu53.36 kB29-05-10 03:13
fPrincipal.dfm176.14 kB29-05-10 03:05
fPrincipal.pas47.37 kB29-05-10 03:05
MainForm.dcu53.56 kB29-05-10 03:22
MainForm.dfm176.34 kB29-05-10 02:38
MainForm.pas47.42 kB29-05-10 02:54
NCore.dcu12.56 kB29-05-10 03:15
NDeviceManager.dcu24.09 kB29-05-10 03:15
Neurotechnology.bmp19.17 kB02-04-10 14:37
Neurotechnology.ico77.80 kB02-04-10 14:37
NExtractors.dcu26.39 kB29-05-10 03:15
NImages.dcu18.90 kB29-05-10 03:15
NLicensing.dcu1.74 kB29-05-10 03:15
NMatchers.dcu10.17 kB29-05-10 03:15
NTemplates.dcu13.68 kB29-05-10 03:15
NUtils.dcu17.48 kB29-05-10 03:15
SettingsForm.dcu15.43 kB29-05-10 03:22
SettingsForm.dfm177.53 kB02-04-10 14:37
SettingsForm.pas7.09 kB02-04-10 14:37
SQLite3.dcu10.80 kB29-05-10 03:15
SQLiteTable3.dcu49.61 kB29-05-10 03:15
FacesSamplePas.dpr.~1~1.00 kB02-04-10 14:37
FacesSamplePas.dpr.~2~1.01 kB29-05-10 03:02
FacesSamplePas.dpr.~3~411.00 B29-05-10 03:02
fPrincipal.dfm.~1~176.34 kB29-05-10 02:38
fPrincipal.dfm.~2~176.35 kB29-05-10 03:02
fPrincipal.dfm.~3~176.32 kB29-05-10 03:04
fPrincipal.pas.~1~47.42 kB29-05-10 03:01
fPrincipal.pas.~2~47.59 kB29-05-10 03:02
fPrincipal.pas.~3~47.58 kB29-05-10 03:04
fPrincipal.pas.~4~47.43 kB29-05-10 03:05
MainForm.dfm.~1~176.32 kB02-04-10 14:37
MainForm.dfm.~2~176.34 kB26-05-10 01:10
MainForm.dfm.~3~176.34 kB26-05-10 01:45
MainForm.dfm.~4~176.35 kB26-05-10 23:51
MainForm.dfm.~5~176.35 kB27-05-10 01:38
MainForm.dfm.~6~176.35 kB29-05-10 01:37
MainForm.pas.~1~45.50 kB02-04-10 14:37
MainForm.pas.~2~45.50 kB26-05-10 01:26
MainForm.pas.~3~45.64 kB26-05-10 01:45
MainForm.pas.~4~47.41 kB27-05-10 00:51
MainForm.pas.~5~47.42 kB27-05-10 01:18
MainForm.pas.~6~47.42 kB29-05-10 01:40
<__history>0.00 B29-05-10 03:16
<Biometria>0.00 B0 0%
...
Sponsored links

SQLiteTable3.pas (1.58 MB)

Need 2 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D