| 1 | 
 unit SQLite3; | 
 
 
 
 
 
 | 2 | 
  | 
 
 
 
 
 
 | 3 | 
 { | 
 
 
 
 
 
 | 4 | 
   Simplified interface for SQLite. | 
 
 
 
 
 
 | 5 | 
   Updated for Sqlite 3 by Tim Anderson (tim@itwriting.com) | 
 
 
 
 
 
 | 6 | 
   Note: NOT COMPLETE for version 3, just minimal functionality | 
 
 
 
 
 
 | 7 | 
   Adapted from file created by Pablo Pissanetzky (pablo@myhtpc.net) | 
 
 
 
 
 
 | 8 | 
   which was based on SQLite.pas by Ben Hochstrasser (bhoc@surfeu.ch) | 
 
 
 
 
 
 | 9 | 
 } | 
 
 
 
 
 
 | 10 | 
  | 
 
 
 
 
 
 | 11 | 
 interface | 
 
 
 
 
 
 | 12 | 
  | 
 
 
 
 
 
 | 13 | 
 const | 
 
 
 
 
 
 | 14 | 
 // Return values for sqlite3_exec() and sqlite3_step() | 
 
 
 
 
 
 | 15 | 
  | 
 
 
 
 
 
 | 16 | 
 SQLITE_OK   =        0;   // Successful result  | 
 
 
 
 
 
 | 17 | 
 SQLITE_ERROR =       1;   // SQL error or missing database  | 
 
 
 
 
 
 | 18 | 
 SQLITE_INTERNAL  =   2;   // An internal logic error in SQLite  | 
 
 
 
 
 
 | 19 | 
 SQLITE_PERM  =       3 ;  // Access permission denied  | 
 
 
 
 
 
 | 20 | 
 SQLITE_ABORT =       4;   // Callback routine requested an abort  | 
 
 
 
 
 
 | 21 | 
 SQLITE_BUSY  =       5;   // The database file is locked  | 
 
 
 
 
 
 | 22 | 
 SQLITE_LOCKED  =     6;   // A table in the database is locked  | 
 
 
 
 
 
 | 23 | 
 SQLITE_NOMEM =       7;   // A malloc() failed  | 
 
 
 
 
 
 | 24 | 
 SQLITE_READONLY  =   8;   // Attempt to write a readonly database  | 
 
 
 
 
 
 | 25 | 
 SQLITE_INTERRUPT  =  9;   // Operation terminated by sqlite3_interrupt() | 
 
 
 
 
 
 | 26 | 
 SQLITE_IOERR =      10;   // Some kind of disk I/O error occurred  | 
 
 
 
 
 
 | 27 | 
 SQLITE_CORRUPT =    11;   // The database disk image is malformed  | 
 
 
 
 
 
 | 28 | 
 SQLITE_NOTFOUND =   12;   // (Internal Only) Table or record not found  | 
 
 
 
 
 
 | 29 | 
 SQLITE_FULL    =    13;   // Insertion failed because database is full  | 
 
 
 
 
 
 | 30 | 
 SQLITE_CANTOPEN =   14;   // Unable to open the database file  | 
 
 
 
 
 
 | 31 | 
 SQLITE_PROTOCOL =   15;   // Database lock protocol error  | 
 
 
 
 
 
 | 32 | 
 SQLITE_EMPTY  =     16;   // Database is empty  | 
 
 
 
 
 
 | 33 | 
 SQLITE_SCHEMA  =    17;   // The database schema changed  | 
 
 
 
 
 
 | 34 | 
 SQLITE_TOOBIG   =   18;   // Too much data for one row of a table  | 
 
 
 
 
 
 | 35 | 
 SQLITE_CONSTRAINT = 19;   // Abort due to contraint violation  | 
 
 
 
 
 
 | 36 | 
 SQLITE_MISMATCH =   20;   // Data type mismatch  | 
 
 
 
 
 
 | 37 | 
 SQLITE_MISUSE  =    21;   // Library used incorrectly  | 
 
 
 
 
 
 | 38 | 
 SQLITE_NOLFS     =  22;   // Uses OS features not supported on host  | 
 
 
 
 
 
 | 39 | 
 SQLITE_AUTH   =     23;   // Authorization denied  | 
 
 
 
 
 
 | 40 | 
 SQLITE_FORMAT =     24;   // Auxiliary database format error  | 
 
 
 
 
 
 | 41 | 
 SQLITE_RANGE   =    25;   // 2nd parameter to sqlite3_bind out of range  | 
 
 
 
 
 
 | 42 | 
 SQLITE_NOTADB    =  26;   // File opened that is not a database file  | 
 
 
 
 
 
 | 43 | 
 SQLITE_ROW   =      100;  // sqlite3_step() has another row ready  | 
 
 
 
 
 
 | 44 | 
 SQLITE_DONE   =     101;  // sqlite3_step() has finished executing | 
 
 
 
 
 
 | 45 | 
  | 
 
 
 
 
 
 | 46 | 
 SQLITE_INTEGER  = 1; | 
 
 
 
 
 
 | 47 | 
 SQLITE_FLOAT  =  2; | 
 
 
 
 
 
 | 48 | 
 SQLITE_TEXT = 3; | 
 
 
 
 
 
 | 49 | 
 SQLITE_BLOB  =   4; | 
 
 
 
 
 
 | 50 | 
 SQLITE_NULL  =   5; | 
 
 
 
 
 
 | 51 | 
   | 
 
 
 
 
 
 | 52 | 
 type | 
 
 
 
 
 
 | 53 | 
 TSQLiteDB     = Pointer; | 
 
 
 
 
 
 | 54 | 
 TSQLiteResult = ^PChar; | 
 
 
 
 
 
 | 55 | 
 TSQLiteStmt = Pointer; | 
 
 
 
 
 
 | 56 | 
  | 
 
 
 
 
 
 | 57 | 
 function  SQLite3_Open           (dbname: PChar; var db: TSqliteDB): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_open'; | 
 
 
 
 
 
 | 58 | 
 function SQLite3_Close          (db: TSQLiteDB): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_close'; | 
 
 
 
 
 
 | 59 | 
 function  SQLite3_Exec           (db: TSQLiteDB; SQLStatement: PChar; CallbackPtr: Pointer; Sender: TObject; var ErrMsg: PChar): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_exec'; | 
 
 
 
 
 
 | 60 | 
 function  SQLite3_Version        (): PChar; cdecl; external 'sqlite3.dll' name 'sqlite3_libversion'; | 
 
 
 
 
 
 | 61 | 
 function  SQLite3_ErrMsg    (db: TSQLiteDB): PChar; cdecl; external 'sqlite3.dll' name 'sqlite3_errmsg'; | 
 
 
 
 
 
 | 62 | 
 function SQLite3_ErrCode (db: TSQLiteDB): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_errcode'; | 
 
 
 
 
 
 | 63 | 
 procedure SQlite3_Free (P: PChar); cdecl; external 'sqlite3.dll' name 'sqlite3_free'; | 
 
 
 
 
 
 | 64 | 
 function  SQLite3_GetTable       (db: TSQLiteDB; SQLStatement: PChar; var ResultPtr: TSQLiteResult; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PChar): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_get_table'; | 
 
 
 
 
 
 | 65 | 
 procedure SQLite3_FreeTable      (Table:TSQLiteResult ); cdecl; external 'sqlite3.dll' name 'sqlite3_free_table'; | 
 
 
 
 
 
 | 66 | 
 function  SQLite3_Complete       (P: PChar): boolean; cdecl; external 'sqlite3.dll' name 'sqlite3_complete'; | 
 
 
 
 
 
 | 67 | 
 function  SQLite3_LastInsertRowID(db: TSQLiteDB): int64; cdecl; external 'sqlite3.dll' name 'sqlite3_last_insert_rowid'; | 
 
 
 
 
 
 | 68 | 
 procedure SQLite3_Interrupt         (db: TSQLiteDB); cdecl; external 'sqlite3.dll' name 'sqlite3_interrupt'; | 
 
 
 
 
 
 | 69 | 
 procedure SQLite3_BusyHandler    (db: TSQLiteDB; CallbackPtr: Pointer; Sender: TObject); cdecl; external 'sqlite3.dll' name'sqlite3_busy_handler' ; | 
 
 
 
 
 
 | 70 | 
 procedure SQLite3_BusyTimeout    (db: TSQLiteDB; TimeOut: integer); cdecl; external 'sqlite3.dll' name 'sqlite3_busy_timeout'; | 
 
 
 
 
 
 | 71 | 
 function  SQLite3_Changes        (db: TSQLiteDB): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_changes'; | 
 
 
 
 
 
 | 72 | 
 function  SQLite3_TotalChanges        (db: TSQLiteDB): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_total_changes'; | 
 
 
 
 
 
 | 73 | 
 function SQLite3_Prepare (db: TSQLiteDB; SQLStatement: PChar; nBytes: integer; var hStmt: TSqliteStmt; var pzTail: PChar): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_prepare'; | 
 
 
 
 
 
 | 74 | 
 function SQLite3_ColumnCount (hStmt: TSqliteStmt): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_column_count'; | 
 
 
 
 
 
 | 75 | 
 function Sqlite3_ColumnName (hStmt: TSqliteStmt; ColNum: integer): pchar; cdecl; external 'sqlite3.dll' name 'sqlite3_column_name'; | 
 
 
 
 
 
 | 76 | 
 function Sqlite3_ColumnDeclType (hStmt: TSqliteStmt; ColNum: integer): pchar; cdecl; external 'sqlite3.dll' name 'sqlite3_column_decltype'; | 
 
 
 
 
 
 | 77 | 
 function Sqlite3_Step (hStmt: TSqliteStmt): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_step'; | 
 
 
 
 
 
 | 78 | 
 function SQLite3_DataCount (hStmt: TSqliteStmt): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_data_count'; | 
 
 
 
 
 
 | 79 | 
  | 
 
 
 
 
 
 | 80 | 
 function Sqlite3_ColumnBlob (hStmt: TSqliteStmt; ColNum: integer):pointer; cdecl; external 'sqlite3.dll' name 'sqlite3_column_blob'; | 
 
 
 
 
 
 | 81 | 
 function Sqlite3_ColumnBytes (hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_column_bytes'; | 
 
 
 
 
 
 | 82 | 
 function Sqlite3_ColumnDouble (hStmt: TSqliteStmt; ColNum: integer): double; cdecl; external 'sqlite3.dll' name 'sqlite3_column_double'; | 
 
 
 
 
 
 | 83 | 
 function Sqlite3_ColumnInt (hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_column_int'; | 
 
 
 
 
 
 | 84 | 
 function Sqlite3_ColumnText (hStmt: TSqliteStmt; ColNum: integer): pchar; cdecl; external 'sqlite3.dll' name 'sqlite3_column_text'; | 
 
 
 
 
 
 | 85 | 
 function Sqlite3_ColumnType (hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_column_type'; | 
 
 
 
 
 
 | 86 | 
  // function Sqlite3_ColumnInt64 (hStmt: TSqliteStmt; ColNum: integer): SqliteInt64; cdecl; external 'sqlite3.dll' name 'sqlite3_column_int64'; | 
 
 
 
 
 
 | 87 | 
 function SQLite3_Finalize (hStmt: TSqliteStmt): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_finalize'; | 
 
 
 
 
 
 | 88 | 
 function SQLite3_Reset (hStmt: TSqliteStmt): integer; cdecl; external 'sqlite3.dll' name 'sqlite3_reset'; | 
 
 
 
 
 
 | 89 | 
  | 
 
 
 
 
 
 | 90 | 
 // | 
 
 
 
 
 
 | 91 | 
 // In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(), | 
 
 
 
 
 
 | 92 | 
 // one or more literals can be replace by a wildcard "?" or ":N:" where | 
 
 
 
 
 
 | 93 | 
 // N is an integer.  These value of these wildcard literals can be set | 
 
 
 
 
 
 | 94 | 
 // using the routines listed below. | 
 
 
 
 
 
 | 95 | 
 // | 
 
 
 
 
 
 | 96 | 
 // In every case, the first parameter is a pointer to the sqlite3_stmt | 
 
 
 
 
 
 | 97 | 
 // structure returned from sqlite3_prepare().  The second parameter is the | 
 
 
 
 
 
 | 98 | 
 // index of the wildcard.  The first "?" has an index of 1.  ":N:" wildcards | 
 
 
 
 
 
 | 99 | 
 // use the index N. | 
 
 
 
 
 
 | 100 | 
 // | 
 
 
 
 
 
 | 101 | 
  // The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and | 
 
 
 
 
 
 | 102 | 
  //sqlite3_bind_text16() is a destructor used to dispose of the BLOB or | 
 
 
 
 
 
 | 103 | 
 //text after SQLite has finished with it.  If the fifth argument is the | 
 
 
 
 
 
 | 104 | 
 // special value SQLITE_STATIC, then the library assumes that the information | 
 
 
 
 
 
 | 105 | 
 // is in static, unmanaged space and does not need to be freed.  If the | 
 
 
 
 
 
 | 106 | 
 // fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its | 
 
 
 
 
 
 | 107 | 
 // own private copy of the data. | 
 
 
 
 
 
 | 108 | 
 // | 
 
 
 
 
 
 | 109 | 
 // The sqlite3_bind_* routine must be called before sqlite3_step() after | 
 
 
 
 
 
 | 110 | 
 // an sqlite3_prepare() or sqlite3_reset().  Unbound wildcards are interpreted | 
 
 
 
 
 
 | 111 | 
 // as NULL. | 
 
 
 
 
 
 | 112 | 
 // | 
 
 
 
 
 
 | 113 | 
  | 
 
 
 
 
 
 | 114 | 
 function SQLite3_BindBlob(hStmt: TSqliteStmt; ParamNum: integer; | 
 
 
 
 
 
 | 115 | 
 ptrData: pointer; numBytes: integer; ptrDestructor: pointer): integer; | 
 
 
 
 
 
 | 116 | 
 cdecl; external 'sqlite3.dll' name 'sqlite3_bind_blob'; | 
 
 
 
 
 
 | 117 | 
  | 
 
 
 
 
 
 | 118 | 
 implementation | 
 
 
 
 
 
 | 119 | 
  | 
 
 
 
 
 
 | 120 | 
 end. |