unit EM.Old.SQLite3;
{
  Simplified interface for SQLite.
  Updated for Sqlite 3 by Tim Anderson (tim@itwriting.com)
  Note: NOT COMPLETE for version 3, just minimal functionality
  Adapted from file created by Pablo Pissanetzky (pablo@myhtpc.net)
  which was based on SQLite.pas by Ben Hochstrasser (bhoc@surfeu.ch)
}
{$IFDEF FPC}
  {$MODE DELPHI}
  {$H+}            (* use AnsiString *)
  {$PACKENUM 4}    (* use 4-byte enums *)
  {$PACKRECORDS C} (* C/C++-compatible record packing *)
{$ELSE}
  {$MINENUMSIZE 4} (* use 4-byte enums *)
{$ENDIF}
interface
const
{$IF Defined(MSWINDOWS)}
  SQLiteDLL = 'sqlite3.dll';
{$ELSEIF Defined(DARWIN)}
  SQLiteDLL = 'libsqlite3.dylib';
  {$linklib libsqlite3}
{$ELSEIF Defined(UNIX)}
  SQLiteDLL = 'sqlite3.so';
{$IFEND}
// Return values for sqlite3_exec() and sqlite3_step()
const
  SQLITE_OK          =  0; // Successful result
  (* beginning-of-error-codes *)
  SQLITE_ERROR       =  1; // SQL error or missing database
  SQLITE_INTERNAL    =  2; // An internal logic error in SQLite
  SQLITE_PERM        =  3; // Access permission denied
  SQLITE_ABORT       =  4; // Callback routine requested an abort
  SQLITE_BUSY        =  5; // The database file is locked
  SQLITE_LOCKED      =  6; // A table in the database is locked
  SQLITE_NOMEM       =  7; // A malloc() failed
  SQLITE_READONLY    =  8; // Attempt to write a readonly database
  SQLITE_INTERRUPT   =  9; // Operation terminated by sqlite3_interrupt()
  SQLITE_IOERR       = 10; // Some kind of disk I/O error occurred
  SQLITE_CORRUPT     = 11; // The database disk image is malformed
  SQLITE_NOTFOUND    = 12; // (Internal Only) Table or record not found
  SQLITE_FULL        = 13; // Insertion failed because database is full
  SQLITE_CANTOPEN    = 14; // Unable to open the database file
  SQLITE_PROTOCOL    = 15; // Database lock protocol error
  SQLITE_EMPTY       = 16; // Database is empty
  SQLITE_SCHEMA      = 17; // The database schema changed
  SQLITE_TOOBIG      = 18; // Too much data for one row of a table
  SQLITE_CONSTRAINT  = 19; // Abort due to contraint violation
  SQLITE_MISMATCH    = 20; // Data type mismatch
  SQLITE_MISUSE      = 21; // Library used incorrectly
  SQLITE_NOLFS       = 22; // Uses OS features not supported on host
  SQLITE_AUTH        = 23; // Authorization denied
  SQLITE_FORMAT      = 24; // Auxiliary database format error
  SQLITE_RANGE       = 25; // 2nd parameter to sqlite3_bind out of range
  SQLITE_NOTADB      = 26; // File opened that is not a database file
  SQLITE_ROW         = 100; // sqlite3_step() has another row ready
  SQLITE_DONE        = 101; // sqlite3_step() has finished executing
  SQLITE_INTEGER = 1;
  SQLITE_FLOAT   = 2;
  SQLITE_TEXT    = 3;
  SQLITE_BLOB    = 4;
  SQLITE_NULL    = 5;
  SQLITE_UTF8     = 1;
  SQLITE_UTF16    = 2;
  SQLITE_UTF16BE  = 3;
  SQLITE_UTF16LE  = 4;
  SQLITE_ANY      = 5;
  SQLITE_STATIC    {: TSQLite3Destructor} = Pointer(0);
  SQLITE_TRANSIENT {: TSQLite3Destructor} = Pointer(-1);
type
  TSQLiteDB = Pointer;
  TSQLiteResult = ^PAnsiChar;
  TSQLiteStmt = Pointer;
type
  PPAnsiCharArray = ^TPAnsiCharArray; 
  TPAnsiCharArray = array[0 .. (MaxInt div SizeOf(PAnsiChar))-1] of PAnsiChar;
  TSQLite3Destructor = procedure(Ptr: Pointer); cdecl;
type
  TSQLiteExecCallback = function(UserData: Pointer; NumCols: Integer; ColValues:
    PPAnsiCharArray; ColNames: PPAnsiCharArray): Integer; cdecl;
  TSQLiteBusyHandlerCallback = function(UserData: Pointer; P2: Integer): Integer; cdecl;
  //function prototype for define own collate
  TCollateXCompare = function(UserData: Pointer; Buf1Len: Integer; Buf1: Pointer;
    Buf2Len: Integer; Buf2: Pointer): Integer; cdecl;
  TSQLite3_Open = function(filename: PAnsiChar; var db: TSQLiteDB): Integer; cdecl;
  TSQLite3_Close = function(db: TSQLiteDB): Integer; cdecl;
  TSQLite3_Exec = function(db: TSQLiteDB; SQLStatement: PAnsiChar; CallbackPtr: TSQLiteExecCallback; UserData: Pointer; var ErrMsg: PAnsiChar): Integer; cdecl;
  TSQLite3_Version = function: PAnsiChar; cdecl;
  TSQLite3_ErrMsg = function(db: TSQLiteDB): PAnsiChar; cdecl;
  TSQLite3_ErrCode = function(db: TSQLiteDB): Integer; cdecl;
  TSQlite3_Free = procedure(P: PAnsiChar); cdecl;
  TSQLite3_GetTable = function(db: TSQLiteDB; SQLStatement: PAnsiChar; var ResultPtr: TSQLiteResult; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PAnsiChar): Integer; cdecl;
  TSQLite3_FreeTable = procedure(Table: TSQLiteResult); cdecl;
  TSQLite3_Complete = function(P: PAnsiChar): Boolean; cdecl;
  TSQLite3_LastInsertRowID = function(db: TSQLiteDB): Int64; cdecl;
  TSQLite3_Interrupt = procedure(db: TSQLiteDB); cdecl;
  TSQLite3_BusyHandler = procedure(db: TSQLiteDB; CallbackPtr: TSQLiteBusyHandlerCallback; UserData: Pointer); cdecl;
  TSQLite3_BusyTimeout = procedure(db: TSQLiteDB; TimeOut: Integer); cdecl;
  TSQLite3_Changes = function(db: TSQLiteDB): Integer; cdecl;
  TSQLite3_TotalChanges = function(db: TSQLiteDB): Integer; cdecl;
  TSQLite3_Prepare = function(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer; cdecl;
  TSQLite3_Prepare_v2 = function(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer; cdecl;
  TSQLite3_ColumnCount = function(hStmt: TSqliteStmt): Integer; cdecl;
  TSQLite3_ColumnName = function(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar; cdecl;
  TSQLite3_ColumnDeclType = function(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar; cdecl;
  TSQLite3_Step = function(hStmt: TSqliteStmt): Integer; cdecl;
  TSQLite3_DataCount = function(hStmt: TSqliteStmt): Integer; cdecl;
  TSQLite3_ColumnBlob = function(hStmt: TSqliteStmt; ColNum: Integer): Pointer; cdecl;
  TSQLite3_ColumnBytes = function(hStmt: TSqliteStmt; ColNum: Integer): Integer; cdecl;
  TSQLite3_ColumnDouble = function(hStmt: TSqliteStmt; ColNum: Integer): Double; cdecl;
  TSQLite3_ColumnInt = function(hStmt: TSqliteStmt; ColNum: Integer): Integer; cdecl;
  TSQLite3_ColumnText = function(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar; cdecl;
  TSQLite3_ColumnType = function(hStmt: TSqliteStmt; ColNum: Integer): Integer; cdecl;
  TSQLite3_ColumnInt64 = function(hStmt: TSqliteStmt; ColNum: Integer): Int64; cdecl;
  TSQLite3_Finalize = function(hStmt: TSqliteStmt): Integer; cdecl;
  TSQLite3_Reset = function(hStmt: TSqliteStmt): Integer; cdecl;
  Tsqlite3_bind_blob = function(hStmt: TSqliteStmt; ParamNum: Integer;
                                ptrData: Pointer; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer; cdecl;
  Tsqlite3_bind_text = function(hStmt: TSqliteStmt; ParamNum: Integer;
                                Text: PAnsiChar; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer; cdecl;
  Tsqlite3_bind_double = function(hStmt: TSqliteStmt; ParamNum: Integer; Data: Double): Integer; cdecl;
  Tsqlite3_bind_int = function(hStmt: TSqLiteStmt; ParamNum: Integer; Data: Integer): Integer; cdecl;
  Tsqlite3_bind_int64 = function(hStmt: TSqliteStmt; ParamNum: Integer; Data: int64): Integer; cdecl;
  Tsqlite3_bind_null = function(hStmt: TSqliteStmt; ParamNum: Integer): Integer; cdecl;
  Tsqlite3_bind_parameter_index = function(hStmt: TSqliteStmt; zName: PAnsiChar): Integer; cdecl;
  Tsqlite3_enable_shared_cache = function(Value: Integer): Integer; cdecl;
//user collate definiton
  TSQLite3_create_collation = function(db: TSQLiteDB; Name: PAnsiChar; eTextRep: Integer;
                                    UserData: Pointer; xCompare: TCollateXCompare): Integer; cdecl;

//function SQLite3_Open(filename: PAnsiChar; var db: TSQLiteDB): Integer; cdecl; external SQLiteDLL name 'sqlite3_open';
//function SQLite3_Close(db: TSQLiteDB): Integer; cdecl; external SQLiteDLL name 'sqlite3_close';
//function SQLite3_Exec(db: TSQLiteDB; SQLStatement: PAnsiChar; CallbackPtr: TSQLiteExecCallback; UserData: Pointer; var ErrMsg: PAnsiChar): Integer; cdecl; external SQLiteDLL name 'sqlite3_exec';
//function SQLite3_Version(): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_libversion';
//function SQLite3_ErrMsg(db: TSQLiteDB): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_errmsg';
//function SQLite3_ErrCode(db: TSQLiteDB): Integer; cdecl; external SQLiteDLL name 'sqlite3_errcode';
//procedure SQlite3_Free(P: PAnsiChar); cdecl; external SQLiteDLL name 'sqlite3_free';
//function SQLite3_GetTable(db: TSQLiteDB; SQLStatement: PAnsiChar; var ResultPtr: TSQLiteResult; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PAnsiChar): Integer; cdecl; external SQLiteDLL name 'sqlite3_get_table';
//procedure SQLite3_FreeTable(Table: TSQLiteResult); cdecl; external SQLiteDLL name 'sqlite3_free_table';
//function SQLite3_Complete(P: PAnsiChar): boolean; cdecl; external SQLiteDLL name 'sqlite3_complete';
//function SQLite3_LastInsertRowID(db: TSQLiteDB): int64; cdecl; external SQLiteDLL name 'sqlite3_last_insert_rowid';
//procedure SQLite3_Interrupt(db: TSQLiteDB); cdecl; external SQLiteDLL name 'sqlite3_interrupt';
//procedure SQLite3_BusyHandler(db: TSQLiteDB; CallbackPtr: TSQLiteBusyHandlerCallback; UserData: Pointer); cdecl; external SQLiteDLL name 'sqlite3_busy_handler';
//procedure SQLite3_BusyTimeout(db: TSQLiteDB; TimeOut: Integer); cdecl; external SQLiteDLL name 'sqlite3_busy_timeout';
//function SQLite3_Changes(db: TSQLiteDB): Integer; cdecl; external SQLiteDLL name 'sqlite3_changes';
//function SQLite3_TotalChanges(db: TSQLiteDB): Integer; cdecl; external SQLiteDLL name 'sqlite3_total_changes';
//function SQLite3_Prepare(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer; cdecl; external SQLiteDLL name 'sqlite3_prepare';
//function SQLite3_Prepare_v2(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer; cdecl; external SQLiteDLL name 'sqlite3_prepare_v2';
//function SQLite3_ColumnCount(hStmt: TSqliteStmt): Integer; cdecl; external SQLiteDLL name 'sqlite3_column_count';
//function SQLite3_ColumnName(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_name';
//function SQLite3_ColumnDeclType(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_decltype';
//function SQLite3_Step(hStmt: TSqliteStmt): Integer; cdecl; external SQLiteDLL name 'sqlite3_step';
//function SQLite3_DataCount(hStmt: TSqliteStmt): Integer; cdecl; external SQLiteDLL name 'sqlite3_data_count';
//
//function SQLite3_ColumnBlob(hStmt: TSqliteStmt; ColNum: Integer): Pointer; cdecl; external SQLiteDLL name 'sqlite3_column_blob';
//function SQLite3_ColumnBytes(hStmt: TSqliteStmt; ColNum: Integer): Integer; cdecl; external SQLiteDLL name 'sqlite3_column_bytes';
//function SQLite3_ColumnDouble(hStmt: TSqliteStmt; ColNum: Integer): Double; cdecl; external SQLiteDLL name 'sqlite3_column_double';
//function SQLite3_ColumnInt(hStmt: TSqliteStmt; ColNum: Integer): Integer; cdecl; external SQLiteDLL name 'sqlite3_column_int';
//function SQLite3_ColumnText(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_text';
//function SQLite3_ColumnType(hStmt: TSqliteStmt; ColNum: Integer): Integer; cdecl; external SQLiteDLL name 'sqlite3_column_type';
//function SQLite3_ColumnInt64(hStmt: TSqliteStmt; ColNum: Integer): Int64; cdecl; external SQLiteDLL name 'sqlite3_column_int64';
//function SQLite3_Finalize(hStmt: TSqliteStmt): Integer; cdecl; external SQLiteDLL name 'sqlite3_finalize';
//function SQLite3_Reset(hStmt: TSqliteStmt): Integer; cdecl; external SQLiteDLL name 'sqlite3_reset';
function SQLite3_Open(filename: PAnsiChar; var db: TSQLiteDB): Integer;
function SQLite3_Close(db: TSQLiteDB): Integer;
function SQLite3_Exec(db: TSQLiteDB; SQLStatement: PAnsiChar; CallbackPtr: TSQLiteExecCallback; UserData: Pointer; var ErrMsg: PAnsiChar): Integer;
function SQLite3_Version: PAnsiChar;
function SQLite3_ErrMsg(db: TSQLiteDB): PAnsiChar;
function SQLite3_ErrCode(db: TSQLiteDB): Integer;
procedure SQlite3_Free(P: PAnsiChar);
function SQLite3_GetTable(db: TSQLiteDB; SQLStatement: PAnsiChar; var ResultPtr: TSQLiteResult; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PAnsiChar): Integer;
procedure SQLite3_FreeTable(Table: TSQLiteResult);
function SQLite3_Complete(P: PAnsiChar): boolean;
function SQLite3_LastInsertRowID(db: TSQLiteDB): int64;
procedure SQLite3_Interrupt(db: TSQLiteDB);
procedure SQLite3_BusyHandler(db: TSQLiteDB; CallbackPtr: TSQLiteBusyHandlerCallback; UserData: Pointer);
procedure SQLite3_BusyTimeout(db: TSQLiteDB; TimeOut: Integer);
function SQLite3_Changes(db: TSQLiteDB): Integer;
function SQLite3_TotalChanges(db: TSQLiteDB): Integer;
function SQLite3_Prepare(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer;
function SQLite3_Prepare_v2(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer;
function SQLite3_ColumnCount(hStmt: TSqliteStmt): Integer;
function SQLite3_ColumnName(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar;
function SQLite3_ColumnDeclType(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar;
function SQLite3_Step(hStmt: TSqliteStmt): Integer;
function SQLite3_DataCount(hStmt: TSqliteStmt): Integer;
function SQLite3_ColumnBlob(hStmt: TSqliteStmt; ColNum: Integer): Pointer;
function SQLite3_ColumnBytes(hStmt: TSqliteStmt; ColNum: Integer): Integer;
function SQLite3_ColumnDouble(hStmt: TSqliteStmt; ColNum: Integer): Double;
function SQLite3_ColumnInt(hStmt: TSqliteStmt; ColNum: Integer): Integer;
function SQLite3_ColumnText(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar;
function SQLite3_ColumnType(hStmt: TSqliteStmt; ColNum: Integer): Integer;
function SQLite3_ColumnInt64(hStmt: TSqliteStmt; ColNum: Integer): Int64;
function SQLite3_Finalize(hStmt: TSqliteStmt): Integer;
function SQLite3_Reset(hStmt: TSqliteStmt): Integer;
// 
// In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(),
// one or more literals can be replace by a wildcard "?" or ":N:" where
// N is an Integer.  These value of these wildcard literals can be set
// using the routines listed below.
// 
// In every case, the first parameter is a Pointer to the sqlite3_stmt
// structure returned from sqlite3_prepare().  The second parameter is the
// index of the wildcard.  The first "?" has an index of 1.  ":N:" wildcards
// use the index N.
// 
// The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and
//sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
//text after SQLite has finished with it.  If the fifth argument is the
// special value SQLITE_STATIC, then the library assumes that the information
// is in static, unmanaged space and does not need to be freed.  If the
// fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its
// own private copy of the data.
// 
// The sqlite3_bind_* routine must be called before sqlite3_step() after
// an sqlite3_prepare() or sqlite3_reset().  Unbound wildcards are interpreted
// as NULL.
//
//function sqlite3_bind_blob(hStmt: TSqliteStmt; ParamNum: Integer;
//  ptrData: Pointer; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer;
//cdecl; external SQLiteDLL name 'sqlite3_bind_blob';
//function sqlite3_bind_text(hStmt: TSqliteStmt; ParamNum: Integer;
//  Text: PAnsiChar; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer;
//cdecl; external SQLiteDLL name 'sqlite3_bind_text';
//function sqlite3_bind_double(hStmt: TSqliteStmt; ParamNum: Integer; Data: Double): Integer;
//  cdecl; external SQLiteDLL name 'sqlite3_bind_double';
//function sqlite3_bind_int(hStmt: TSqLiteStmt; ParamNum: Integer; Data: Integer): Integer;
//  cdecl; external SQLiteDLL name 'sqlite3_bind_int';
//function sqlite3_bind_int64(hStmt: TSqliteStmt; ParamNum: Integer; Data: int64): Integer;
//  cdecl; external SQLiteDLL name 'sqlite3_bind_int64';
//function sqlite3_bind_null(hStmt: TSqliteStmt; ParamNum: Integer): Integer;
//  cdecl; external SQLiteDLL name 'sqlite3_bind_null';
//
//function sqlite3_bind_parameter_index(hStmt: TSqliteStmt; zName: PAnsiChar): Integer;
//  cdecl; external SQLiteDLL name 'sqlite3_bind_parameter_index';
//
//function sqlite3_enable_shared_cache(Value: Integer): Integer; cdecl; external SQLiteDLL name 'sqlite3_enable_shared_cache';
//
////user collate definiton
//function SQLite3_create_collation(db: TSQLiteDB; Name: PAnsiChar; eTextRep: Integer;
//  UserData: Pointer; xCompare: TCollateXCompare): Integer; cdecl; external SQLiteDLL name 'sqlite3_create_collation';
function sqlite3_bind_blob(hStmt: TSqliteStmt; ParamNum: Integer;
  ptrData: Pointer; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer;
function sqlite3_bind_text(hStmt: TSqliteStmt; ParamNum: Integer;
  Text: PAnsiChar; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer;
function sqlite3_bind_double(hStmt: TSqliteStmt; ParamNum: Integer; Data: Double): Integer;
function sqlite3_bind_int(hStmt: TSqLiteStmt; ParamNum: Integer; Data: Integer): Integer;
function sqlite3_bind_int64(hStmt: TSqliteStmt; ParamNum: Integer; Data: int64): Integer;
function sqlite3_bind_null(hStmt: TSqliteStmt; ParamNum: Integer): Integer;
function sqlite3_bind_parameter_index(hStmt: TSqliteStmt; zName: PAnsiChar): Integer;
function sqlite3_enable_shared_cache(Value: Integer): Integer;
//user collate definiton
function SQLite3_create_collation(db: TSQLiteDB; Name: PAnsiChar; eTextRep: Integer;
  UserData: Pointer; xCompare: TCollateXCompare): Integer;
function SQLiteFieldType(SQLiteFieldTypeCode: Integer): AnsiString;
function SQLiteErrorStr(SQLiteErrorCode: Integer): AnsiString;
implementation
uses
  SysUtils, Winapi.Windows;
var
  _hSql3: THandle = 0;
  _SQLite3_Open: TSQLite3_Open = nil;
  _SQLite3_Close: TSQLite3_Close = nil;
  _SQLite3_Exec: TSQLite3_Exec = nil;
  _SQLite3_Version: TSQLite3_Version = nil;
  _SQLite3_ErrMsg: TSQLite3_ErrMsg = nil;
  _SQLite3_ErrCode: TSQLite3_ErrCode = nil;
  _SQlite3_Free: TSQlite3_Free = nil;
  _SQLite3_GetTable: TSQLite3_GetTable = nil;
  _SQLite3_FreeTable: TSQLite3_FreeTable = nil;
  _SQLite3_Complete: TSQLite3_Complete = nil;
  _SQLite3_LastInsertRowID: TSQLite3_LastInsertRowID = nil;
  _SQLite3_Interrupt: TSQLite3_Interrupt = nil;
  _SQLite3_BusyHandler: TSQLite3_BusyHandler = nil;
  _SQLite3_BusyTimeout: TSQLite3_BusyTimeout = nil;
  _SQLite3_Changes: TSQLite3_Changes = nil;
  _SQLite3_TotalChanges: TSQLite3_TotalChanges = nil;
  _SQLite3_Prepare: TSQLite3_Prepare = nil;
  _SQLite3_Prepare_v2: TSQLite3_Prepare_v2 = nil;
  _SQLite3_ColumnCount: TSQLite3_ColumnCount = nil;
  _SQLite3_ColumnName: TSQLite3_ColumnName = nil;
  _SQLite3_ColumnDeclType: TSQLite3_ColumnDeclType = nil;
  _SQLite3_Step: TSQLite3_Step = nil;
  _SQLite3_DataCount: TSQLite3_DataCount = nil;
  _SQLite3_ColumnBlob: TSQLite3_ColumnBlob = nil;
  _SQLite3_ColumnBytes: TSQLite3_ColumnBytes = nil;
  _SQLite3_ColumnDouble: TSQLite3_ColumnDouble = nil;
  _SQLite3_ColumnInt: TSQLite3_ColumnInt = nil;
  _SQLite3_ColumnText: TSQLite3_ColumnText = nil;
  _SQLite3_ColumnType: TSQLite3_ColumnType = nil;
  _SQLite3_ColumnInt64: TSQLite3_ColumnInt64 = nil;
  _SQLite3_Finalize: TSQLite3_Finalize = nil;
  _SQLite3_Reset: TSQLite3_Reset = nil;
  _sqlite3_bind_blob: Tsqlite3_bind_blob = nil;
  _sqlite3_bind_text: Tsqlite3_bind_text = nil;
  _sqlite3_bind_double: Tsqlite3_bind_double = nil;
  _sqlite3_bind_int: Tsqlite3_bind_int = nil;
  _sqlite3_bind_int64: Tsqlite3_bind_int64 = nil;
  _sqlite3_bind_null: Tsqlite3_bind_null = nil;
  _sqlite3_bind_parameter_index: Tsqlite3_bind_parameter_index = nil;
  _sqlite3_enable_shared_cache: Tsqlite3_enable_shared_cache = nil;
  _SQLite3_create_collation: TSQLite3_create_collation = nil;
function InitSqlite3Procedure: Boolean;
begin
  if _hSql3 = 0 then
  begin
    _hSql3 := SafeLoadLibrary(SQLiteDLL);
    if _hSql3 <> 0 then
    begin
      @_SQLite3_Open := GetProcAddress(_hSql3, 'sqlite3_open');
      @_SQLite3_Close := GetProcAddress(_hSql3, 'sqlite3_close');
      @_SQLite3_Exec := GetProcAddress(_hSql3, 'sqlite3_exec');
      @_SQLite3_Version := GetProcAddress(_hSql3, 'sqlite3_libversion');
      @_SQLite3_ErrMsg := GetProcAddress(_hSql3, 'sqlite3_errmsg');
      @_SQLite3_ErrCode := GetProcAddress(_hSql3, 'sqlite3_errcode');
      @_SQlite3_Free := GetProcAddress(_hSql3, 'sqlite3_free');
      @_SQLite3_GetTable := GetProcAddress(_hSql3, 'sqlite3_get_table');
      @_SQLite3_FreeTable := GetProcAddress(_hSql3, 'sqlite3_free_table');
      @_SQLite3_Complete := GetProcAddress(_hSql3, 'sqlite3_complete');
      @_SQLite3_LastInsertRowID := GetProcAddress(_hSql3, 'sqlite3_last_insert_rowid');
      @_SQLite3_Interrupt := GetProcAddress(_hSql3, 'sqlite3_interrupt');
      @_SQLite3_BusyHandler := GetProcAddress(_hSql3, 'sqlite3_busy_handler');
      @_SQLite3_BusyTimeout := GetProcAddress(_hSql3, 'sqlite3_busy_timeout');
      @_SQLite3_Changes := GetProcAddress(_hSql3, 'sqlite3_changes');
      @_SQLite3_TotalChanges := GetProcAddress(_hSql3, 'sqlite3_total_changes');
      @_SQLite3_Prepare := GetProcAddress(_hSql3, 'sqlite3_prepare');
      @_SQLite3_Prepare_v2 := GetProcAddress(_hSql3, 'sqlite3_prepare_v2');
      @_SQLite3_ColumnCount := GetProcAddress(_hSql3, 'sqlite3_column_count');
      @_SQLite3_ColumnName := GetProcAddress(_hSql3, 'sqlite3_column_name');
      @_SQLite3_ColumnDeclType := GetProcAddress(_hSql3, 'sqlite3_column_decltype');
      @_SQLite3_Step := GetProcAddress(_hSql3, 'sqlite3_step');
      @_SQLite3_DataCount := GetProcAddress(_hSql3, 'sqlite3_data_count');
      @_SQLite3_ColumnBlob := GetProcAddress(_hSql3, 'sqlite3_column_blob');
      @_SQLite3_ColumnBytes := GetProcAddress(_hSql3, 'sqlite3_column_bytes');
      @_SQLite3_ColumnDouble := GetProcAddress(_hSql3, 'sqlite3_column_double');
      @_SQLite3_ColumnInt := GetProcAddress(_hSql3, 'sqlite3_column_int');
      @_SQLite3_ColumnText := GetProcAddress(_hSql3, 'sqlite3_column_text');
      @_SQLite3_ColumnType := GetProcAddress(_hSql3, 'sqlite3_column_type');
      @_SQLite3_ColumnInt64 := GetProcAddress(_hSql3, 'sqlite3_column_int64');
      @_SQLite3_Finalize := GetProcAddress(_hSql3, 'sqlite3_finalize');
      @_SQLite3_Reset := GetProcAddress(_hSql3, 'sqlite3_reset');
      @_sqlite3_bind_blob := GetProcAddress(_hSql3, 'sqlite3_bind_blob');
      @_sqlite3_bind_text := GetProcAddress(_hSql3, 'sqlite3_bind_text');
      @_sqlite3_bind_double := GetProcAddress(_hSql3, 'sqlite3_bind_double');
      @_sqlite3_bind_int := GetProcAddress(_hSql3, 'sqlite3_bind_int');
      @_sqlite3_bind_int64 := GetProcAddress(_hSql3, 'sqlite3_bind_int64');
      @_sqlite3_bind_null := GetProcAddress(_hSql3, 'sqlite3_bind_null');
      @_sqlite3_bind_parameter_index := GetProcAddress(_hSql3, 'sqlite3_bind_parameter_index');
      @_sqlite3_enable_shared_cache := GetProcAddress(_hSql3, 'sqlite3_enable_shared_cache');
      @_SQLite3_create_collation := GetProcAddress(_hSql3, 'sqlite3_create_collation');
    end;
  end;
  Result := _hSql3 <> 0;
end;
function SQLite3_Open(filename: PAnsiChar; var db: TSQLiteDB): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Open) then
    Result := _SQLite3_Open(filename, db);
end;
function SQLite3_Close(db: TSQLiteDB): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Close) then
    Result := _SQLite3_Close(db);
end;
function SQLite3_Exec(db: TSQLiteDB; SQLStatement: PAnsiChar; CallbackPtr: TSQLiteExecCallback; UserData: Pointer; var ErrMsg: PAnsiChar): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Exec) then
    Result := _SQLite3_Exec(db, SQLStatement, CallbackPtr, UserData, ErrMsg);
end;
function SQLite3_Version: PAnsiChar;
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_Version) then
    _SQLite3_Version;
end;
function SQLite3_ErrMsg(db: TSQLiteDB): PAnsiChar;
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_ErrMsg) then
    _SQLite3_ErrMsg(db);
end;
function SQLite3_ErrCode(db: TSQLiteDB): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ErrCode) then
    Result := _SQLite3_ErrCode(db);
end;
procedure SQlite3_Free(P: PAnsiChar);
begin
  if InitSqlite3Procedure and Assigned(_SQlite3_Free) then
    _SQlite3_Free(P);
end;
function SQLite3_GetTable(db: TSQLiteDB; SQLStatement: PAnsiChar; var ResultPtr: TSQLiteResult; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PAnsiChar): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_GetTable) then
    Result := _SQLite3_GetTable(db, SQLStatement, ResultPtr, RowCount, ColCount, ErrMsg);
end;
procedure SQLite3_FreeTable(Table: TSQLiteResult);
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_FreeTable) then
    _SQLite3_FreeTable(Table);
end;
function SQLite3_Complete(P: PAnsiChar): boolean;
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_Complete) then
    Result := _SQLite3_Complete(P);
end;
function SQLite3_LastInsertRowID(db: TSQLiteDB): Int64;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_LastInsertRowID) then
    Result := _SQLite3_LastInsertRowID(db);
end;
procedure SQLite3_Interrupt(db: TSQLiteDB);
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_Interrupt) then
    _SQLite3_Interrupt(db);
end;
procedure SQLite3_BusyHandler(db: TSQLiteDB; CallbackPtr: TSQLiteBusyHandlerCallback; UserData: Pointer);
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_BusyHandler) then
    _SQLite3_BusyHandler(db, CallbackPtr, UserData);
end;
procedure SQLite3_BusyTimeout(db: TSQLiteDB; TimeOut: Integer);
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_BusyTimeout) then
    _SQLite3_BusyTimeout(db, TimeOut);
end;
function SQLite3_Changes(db: TSQLiteDB): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Changes) then
    Result := _SQLite3_Changes(db);
end;
function SQLite3_TotalChanges(db: TSQLiteDB): Integer;
begin
  if InitSqlite3Procedure and Assigned(_SQLite3_TotalChanges) then
    Result := _SQLite3_TotalChanges(db);
end;
function SQLite3_Prepare(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Prepare) then
    Result := _SQLite3_Prepare(db, SQLStatement, nBytes, hStmt, pzTail);
end;
function SQLite3_Prepare_v2(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: Integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Prepare_v2) then
    Result := _SQLite3_Prepare_v2(db, SQLStatement, nBytes, hStmt, pzTail);
end;
function SQLite3_ColumnCount(hStmt: TSqliteStmt): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnCount) then
    Result := _SQLite3_ColumnCount(hStmt);
end;
function SQLite3_ColumnName(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar;
begin
  Result := nil;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnName) then
    Result := _SQLite3_ColumnName(hStmt, ColNum);
end;
function SQLite3_ColumnDeclType(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar;
begin
  Result := nil;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnDeclType) then
    Result := _SQLite3_ColumnDeclType(hStmt, ColNum);
end;
function SQLite3_Step(hStmt: TSqliteStmt): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Step) then
    Result := _SQLite3_Step(hStmt);
end;
function SQLite3_DataCount(hStmt: TSqliteStmt): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_DataCount) then
    Result := _SQLite3_DataCount(hStmt);
end;
function SQLite3_ColumnBlob(hStmt: TSqliteStmt; ColNum: Integer): Pointer;
begin
  Result := nil;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnBlob) then
    Result := _SQLite3_ColumnBlob(hStmt, ColNum);
end;
function SQLite3_ColumnBytes(hStmt: TSqliteStmt; ColNum: Integer): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnBytes) then
    Result := _SQLite3_ColumnBytes(hStmt, ColNum);
end;
function SQLite3_ColumnDouble(hStmt: TSqliteStmt; ColNum: Integer): Double;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnDouble) then
    Result := _SQLite3_ColumnDouble(hStmt, ColNum);
end;
function SQLite3_ColumnInt(hStmt: TSqliteStmt; ColNum: Integer): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnInt) then
    Result := _SQLite3_ColumnInt(hStmt, ColNum);
end;
function SQLite3_ColumnText(hStmt: TSqliteStmt; ColNum: Integer): PAnsiChar;
begin
  Result := nil;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnText) then
    Result := _SQLite3_ColumnText(hStmt, ColNum);
end;
function SQLite3_ColumnType(hStmt: TSqliteStmt; ColNum: Integer): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnType) then
    Result := _SQLite3_ColumnType(hStmt, ColNum);
end;
function SQLite3_ColumnInt64(hStmt: TSqliteStmt; ColNum: Integer): Int64;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_ColumnInt64) then
    Result := _SQLite3_ColumnInt64(hStmt, ColNum);
end;
function SQLite3_Finalize(hStmt: TSqliteStmt): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Finalize) then
    Result := _SQLite3_Finalize(hStmt);
end;
function SQLite3_Reset(hStmt: TSqliteStmt): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_Reset) then
    Result := _SQLite3_Reset(hStmt);
end;
function sqlite3_bind_blob(hStmt: TSqliteStmt; ParamNum: Integer;
  ptrData: Pointer; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_blob) then
    Result := _sqlite3_bind_blob(hStmt, ParamNum, ptrData, numBytes, ptrDestructor);
end;
function sqlite3_bind_text(hStmt: TSqliteStmt; ParamNum: Integer;
  Text: PAnsiChar; numBytes: Integer; ptrDestructor: TSQLite3Destructor): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_text) then
    Result := _sqlite3_bind_text(hStmt, ParamNum, Text, numBytes, ptrDestructor);
end;
function sqlite3_bind_double(hStmt: TSqliteStmt; ParamNum: Integer; Data: Double): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_double) then
    Result := _sqlite3_bind_double(hStmt, ParamNum, Data);
end;
function sqlite3_bind_int(hStmt: TSqLiteStmt; ParamNum: Integer; Data: Integer): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_int) then
    Result := _sqlite3_bind_int(hStmt, ParamNum, Data);
end;
function sqlite3_bind_int64(hStmt: TSqliteStmt; ParamNum: Integer; Data: int64): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_int64) then
    Result := _sqlite3_bind_int64(hStmt, ParamNum, Data);
end;
function sqlite3_bind_null(hStmt: TSqliteStmt; ParamNum: Integer): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_null) then
    Result := _sqlite3_bind_null(hStmt, ParamNum);
end;
function sqlite3_bind_parameter_index(hStmt: TSqliteStmt; zName: PAnsiChar): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_bind_parameter_index) then
    Result := _sqlite3_bind_parameter_index(hStmt, zName);
end;
function sqlite3_enable_shared_cache(Value: Integer): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_sqlite3_enable_shared_cache) then
    Result := _sqlite3_enable_shared_cache(Value);
end;
function SQLite3_create_collation(db: TSQLiteDB; Name: PAnsiChar; eTextRep: Integer;
  UserData: Pointer; xCompare: TCollateXCompare): Integer;
begin
  Result := -1;
  if InitSqlite3Procedure and Assigned(_SQLite3_create_collation) then
    Result := _SQLite3_create_collation(db, Name, eTextRep, UserData, xCompare);
end;
// -----------------------------------------------------------------------------
function SQLiteFieldType(SQLiteFieldTypeCode: Integer): AnsiString;
begin
  case SQLiteFieldTypeCode of
    SQLITE_INTEGER: Result := 'Integer';
    SQLITE_FLOAT: Result := 'Float';
    SQLITE_TEXT: Result := 'Text';
    SQLITE_BLOB: Result := 'Blob';
    SQLITE_NULL: Result := 'Null';
  else
    Result := AnsiString('Unknown SQLite Field Type Code "' + IntToStr(SQLiteFieldTypeCode) + '"');
  end;
end;
function SQLiteErrorStr(SQLiteErrorCode: Integer): AnsiString;
begin
  case SQLiteErrorCode of
    SQLITE_OK: Result := 'Successful result';
    SQLITE_ERROR: Result := 'SQL error or missing database';
    SQLITE_INTERNAL: Result := 'An internal logic error in SQLite';
    SQLITE_PERM: Result := 'Access permission denied';
    SQLITE_ABORT: Result := 'Callback routine requested an abort';
    SQLITE_BUSY: Result := 'The database file is locked';
    SQLITE_LOCKED: Result := 'A table in the database is locked';
    SQLITE_NOMEM: Result := 'A malloc() failed';
    SQLITE_READONLY: Result := 'Attempt to write a readonly database';
    SQLITE_INTERRUPT: Result := 'Operation terminated by sqlite3_interrupt()';
    SQLITE_IOERR: Result := 'Some kind of disk I/O error occurred';
    SQLITE_CORRUPT: Result := 'The database disk image is malformed';
    SQLITE_NOTFOUND: Result := '(Internal Only) Table or record not found';
    SQLITE_FULL: Result := 'Insertion failed because database is full';
    SQLITE_CANTOPEN: Result := 'Unable to open the database file';
    SQLITE_PROTOCOL: Result := 'Database lock protocol error';
    SQLITE_EMPTY: Result := 'Database is empty';
    SQLITE_SCHEMA: Result := 'The database schema changed';
    SQLITE_TOOBIG: Result := 'Too much data for one row of a table';
    SQLITE_CONSTRAINT: Result := 'Abort due to contraint violation';
    SQLITE_MISMATCH: Result := 'Data type mismatch';
    SQLITE_MISUSE: Result := 'Library used incorrectly';
    SQLITE_NOLFS: Result := 'Uses OS features not supported on host';
    SQLITE_AUTH: Result := 'Authorization denied';
    SQLITE_FORMAT: Result := 'Auxiliary database format error';
    SQLITE_RANGE: Result := '2nd parameter to sqlite3_bind out of range';
    SQLITE_NOTADB: Result := 'File opened that is not a database file';
    SQLITE_ROW: Result := 'sqlite3_step() has another row ready';
    SQLITE_DONE: Result := 'sqlite3_step() has finished executing';
  else
    Result := AnsiString('Unknown SQLite Error Code "' + IntToStr(SQLiteErrorCode) + '"');
  end;
end;
function ColValueToStr(Value: PAnsiChar): AnsiString;
begin
  if (Value = nil) then
    Result := 'NULL'
  else
    Result := Value;
end;

end.
