SQLite3.~pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. unit SQLite3;
  2. {
  3. Simplified interface for SQLite.
  4. Updated for Sqlite 3 by Tim Anderson (tim@itwriting.com)
  5. Note: NOT COMPLETE for version 3, just minimal functionality
  6. Adapted from file created by Pablo Pissanetzky (pablo@myhtpc.net)
  7. which was based on SQLite.pas by Ben Hochstrasser (bhoc@surfeu.ch)
  8. }
  9. {$IFDEF FPC}
  10. {$MODE DELPHI}
  11. {$H+} (* use AnsiString *)
  12. {$PACKENUM 4} (* use 4-byte enums *)
  13. {$PACKRECORDS C} (* C/C++-compatible record packing *)
  14. {$ELSE}
  15. {$MINENUMSIZE 4} (* use 4-byte enums *)
  16. {$ENDIF}
  17. interface
  18. const
  19. SQLiteDLL = 'libsqlite3.so.0.8.6';
  20. // Return values for sqlite3_exec() and sqlite3_step()
  21. SQLITE_OK = 0; // Successful result
  22. (* beginning-of-error-codes *)
  23. SQLITE_ERROR = 1; // SQL error or missing database
  24. SQLITE_INTERNAL = 2; // An internal logic error in SQLite
  25. SQLITE_PERM = 3; // Access permission denied
  26. SQLITE_ABORT = 4; // Callback routine requested an abort
  27. SQLITE_BUSY = 5; // The database file is locked
  28. SQLITE_LOCKED = 6; // A table in the database is locked
  29. SQLITE_NOMEM = 7; // A malloc() failed
  30. SQLITE_READONLY = 8; // Attempt to write a readonly database
  31. SQLITE_INTERRUPT = 9; // Operation terminated by sqlite3_interrupt()
  32. SQLITE_IOERR = 10; // Some kind of disk I/O error occurred
  33. SQLITE_CORRUPT = 11; // The database disk image is malformed
  34. SQLITE_NOTFOUND = 12; // (Internal Only) Table or record not found
  35. SQLITE_FULL = 13; // Insertion failed because database is full
  36. SQLITE_CANTOPEN = 14; // Unable to open the database file
  37. SQLITE_PROTOCOL = 15; // Database lock protocol error
  38. SQLITE_EMPTY = 16; // Database is empty
  39. SQLITE_SCHEMA = 17; // The database schema changed
  40. SQLITE_TOOBIG = 18; // Too much data for one row of a table
  41. SQLITE_CONSTRAINT = 19; // Abort due to contraint violation
  42. SQLITE_MISMATCH = 20; // Data type mismatch
  43. SQLITE_MISUSE = 21; // Library used incorrectly
  44. SQLITE_NOLFS = 22; // Uses OS features not supported on host
  45. SQLITE_AUTH = 23; // Authorization denied
  46. SQLITE_FORMAT = 24; // Auxiliary database format error
  47. SQLITE_RANGE = 25; // 2nd parameter to sqlite3_bind out of range
  48. SQLITE_NOTADB = 26; // File opened that is not a database file
  49. SQLITE_ROW = 100; // sqlite3_step() has another row ready
  50. SQLITE_DONE = 101; // sqlite3_step() has finished executing
  51. SQLITE_INTEGER = 1;
  52. SQLITE_FLOAT = 2;
  53. SQLITE_TEXT = 3;
  54. SQLITE_BLOB = 4;
  55. SQLITE_NULL = 5;
  56. SQLITE_UTF8 = 1;
  57. SQLITE_UTF16 = 2;
  58. SQLITE_UTF16BE = 3;
  59. SQLITE_UTF16LE = 4;
  60. SQLITE_ANY = 5;
  61. SQLITE_STATIC {: TSQLite3Destructor} = Pointer(0);
  62. SQLITE_TRANSIENT {: TSQLite3Destructor} = Pointer(-1);
  63. type
  64. TSQLiteDB = Pointer;
  65. TSQLiteResult = ^PAnsiChar;
  66. TSQLiteStmt = Pointer;
  67. type
  68. PPAnsiCharArray = ^TPAnsiCharArray;
  69. TPAnsiCharArray = array[0 .. (MaxInt div SizeOf(PAnsiChar))-1] of PAnsiChar;
  70. type
  71. TSQLiteExecCallback = function(UserData: Pointer; NumCols: integer; ColValues:
  72. PPAnsiCharArray; ColNames: PPAnsiCharArray): integer; cdecl;
  73. TSQLiteBusyHandlerCallback = function(UserData: Pointer; P2: integer): integer; cdecl;
  74. //function prototype for define own collate
  75. TCollateXCompare = function(UserData: pointer; Buf1Len: integer; Buf1: pointer;
  76. Buf2Len: integer; Buf2: pointer): integer; cdecl;
  77. function SQLite3_Open(filename: PAnsiChar; var db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_open';
  78. function SQLite3_Close(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_close';
  79. function SQLite3_Exec(db: TSQLiteDB; SQLStatement: PAnsiChar; CallbackPtr: TSQLiteExecCallback; UserData: Pointer; var ErrMsg: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_exec';
  80. function SQLite3_Version(): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_libversion';
  81. function SQLite3_ErrMsg(db: TSQLiteDB): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_errmsg';
  82. function SQLite3_ErrCode(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_errcode';
  83. procedure SQlite3_Free(P: PAnsiChar); cdecl; external SQLiteDLL name 'sqlite3_free';
  84. 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';
  85. procedure SQLite3_FreeTable(Table: TSQLiteResult); cdecl; external SQLiteDLL name 'sqlite3_free_table';
  86. function SQLite3_Complete(P: PAnsiChar): boolean; cdecl; external SQLiteDLL name 'sqlite3_complete';
  87. function SQLite3_LastInsertRowID(db: TSQLiteDB): int64; cdecl; external SQLiteDLL name 'sqlite3_last_insert_rowid';
  88. procedure SQLite3_Interrupt(db: TSQLiteDB); cdecl; external SQLiteDLL name 'sqlite3_interrupt';
  89. procedure SQLite3_BusyHandler(db: TSQLiteDB; CallbackPtr: TSQLiteBusyHandlerCallback; UserData: Pointer); cdecl; external SQLiteDLL name 'sqlite3_busy_handler';
  90. procedure SQLite3_BusyTimeout(db: TSQLiteDB; TimeOut: integer); cdecl; external SQLiteDLL name 'sqlite3_busy_timeout';
  91. function SQLite3_Changes(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_changes';
  92. function SQLite3_TotalChanges(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_total_changes';
  93. function SQLite3_Prepare(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_prepare';
  94. function SQLite3_Prepare_v2(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_prepare_v2';
  95. function SQLite3_ColumnCount(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_column_count';
  96. function SQLite3_ColumnName(hStmt: TSqliteStmt; ColNum: integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_name';
  97. function SQLite3_ColumnDeclType(hStmt: TSqliteStmt; ColNum: integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_decltype';
  98. function SQLite3_Step(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_step';
  99. function SQLite3_DataCount(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_data_count';
  100. function SQLite3_ColumnBlob(hStmt: TSqliteStmt; ColNum: integer): pointer; cdecl; external SQLiteDLL name 'sqlite3_column_blob';
  101. function SQLite3_ColumnBytes(hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_column_bytes';
  102. function SQLite3_ColumnDouble(hStmt: TSqliteStmt; ColNum: integer): double; cdecl; external SQLiteDLL name 'sqlite3_column_double';
  103. function SQLite3_ColumnInt(hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_column_int';
  104. function SQLite3_ColumnText(hStmt: TSqliteStmt; ColNum: integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_text';
  105. function SQLite3_ColumnType(hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_column_type';
  106. function SQLite3_ColumnInt64(hStmt: TSqliteStmt; ColNum: integer): Int64; cdecl; external SQLiteDLL name 'sqlite3_column_int64';
  107. function SQLite3_Finalize(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_finalize';
  108. function SQLite3_Reset(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_reset';
  109. //
  110. // In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(),
  111. // one or more literals can be replace by a wildcard "?" or ":N:" where
  112. // N is an integer. These value of these wildcard literals can be set
  113. // using the routines listed below.
  114. //
  115. // In every case, the first parameter is a pointer to the sqlite3_stmt
  116. // structure returned from sqlite3_prepare(). The second parameter is the
  117. // index of the wildcard. The first "?" has an index of 1. ":N:" wildcards
  118. // use the index N.
  119. //
  120. // The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and
  121. //sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  122. //text after SQLite has finished with it. If the fifth argument is the
  123. // special value SQLITE_STATIC, then the library assumes that the information
  124. // is in static, unmanaged space and does not need to be freed. If the
  125. // fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its
  126. // own private copy of the data.
  127. //
  128. // The sqlite3_bind_* routine must be called before sqlite3_step() after
  129. // an sqlite3_prepare() or sqlite3_reset(). Unbound wildcards are interpreted
  130. // as NULL.
  131. //
  132. type
  133. TSQLite3Destructor = procedure(Ptr: Pointer); cdecl;
  134. function sqlite3_bind_blob(hStmt: TSqliteStmt; ParamNum: integer;
  135. ptrData: pointer; numBytes: integer; ptrDestructor: TSQLite3Destructor): integer;
  136. cdecl; external SQLiteDLL name 'sqlite3_bind_blob';
  137. function sqlite3_bind_text(hStmt: TSqliteStmt; ParamNum: integer;
  138. Text: PAnsiChar; numBytes: integer; ptrDestructor: TSQLite3Destructor): integer;
  139. cdecl; external SQLiteDLL name 'sqlite3_bind_text';
  140. function sqlite3_bind_double(hStmt: TSqliteStmt; ParamNum: integer; Data: Double): integer;
  141. cdecl; external SQLiteDLL name 'sqlite3_bind_double';
  142. function sqlite3_bind_int(hStmt: TSqLiteStmt; ParamNum: integer; Data: integer): integer;
  143. cdecl; external SQLiteDLL name 'sqlite3_bind_int';
  144. function sqlite3_bind_int64(hStmt: TSqliteStmt; ParamNum: integer; Data: int64): integer;
  145. cdecl; external SQLiteDLL name 'sqlite3_bind_int64';
  146. function sqlite3_bind_null(hStmt: TSqliteStmt; ParamNum: integer): integer;
  147. cdecl; external SQLiteDLL name 'sqlite3_bind_null';
  148. function sqlite3_bind_parameter_index(hStmt: TSqliteStmt; zName: PAnsiChar): integer;
  149. cdecl; external SQLiteDLL name 'sqlite3_bind_parameter_index';
  150. function sqlite3_enable_shared_cache(Value: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_enable_shared_cache';
  151. //user collate definiton
  152. function SQLite3_create_collation(db: TSQLiteDB; Name: PAnsiChar; eTextRep: integer;
  153. UserData: pointer; xCompare: TCollateXCompare): integer; cdecl; external SQLiteDLL name 'sqlite3_create_collation';
  154. function SQLiteFieldType(SQLiteFieldTypeCode: Integer): AnsiString;
  155. function SQLiteErrorStr(SQLiteErrorCode: Integer): AnsiString;
  156. implementation
  157. uses
  158. SysUtils;
  159. function SQLiteFieldType(SQLiteFieldTypeCode: Integer): AnsiString;
  160. begin
  161. case SQLiteFieldTypeCode of
  162. SQLITE_INTEGER: Result := 'Integer';
  163. SQLITE_FLOAT: Result := 'Float';
  164. SQLITE_TEXT: Result := 'Text';
  165. SQLITE_BLOB: Result := 'Blob';
  166. SQLITE_NULL: Result := 'Null';
  167. else
  168. Result := 'Unknown SQLite Field Type Code "' + IntToStr(SQLiteFieldTypeCode) + '"';
  169. end;
  170. end;
  171. function SQLiteErrorStr(SQLiteErrorCode: Integer): AnsiString;
  172. begin
  173. case SQLiteErrorCode of
  174. SQLITE_OK: Result := 'Successful result';
  175. SQLITE_ERROR: Result := 'SQL error or missing database';
  176. SQLITE_INTERNAL: Result := 'An internal logic error in SQLite';
  177. SQLITE_PERM: Result := 'Access permission denied';
  178. SQLITE_ABORT: Result := 'Callback routine requested an abort';
  179. SQLITE_BUSY: Result := 'The database file is locked';
  180. SQLITE_LOCKED: Result := 'A table in the database is locked';
  181. SQLITE_NOMEM: Result := 'A malloc() failed';
  182. SQLITE_READONLY: Result := 'Attempt to write a readonly database';
  183. SQLITE_INTERRUPT: Result := 'Operation terminated by sqlite3_interrupt()';
  184. SQLITE_IOERR: Result := 'Some kind of disk I/O error occurred';
  185. SQLITE_CORRUPT: Result := 'The database disk image is malformed';
  186. SQLITE_NOTFOUND: Result := '(Internal Only) Table or record not found';
  187. SQLITE_FULL: Result := 'Insertion failed because database is full';
  188. SQLITE_CANTOPEN: Result := 'Unable to open the database file';
  189. SQLITE_PROTOCOL: Result := 'Database lock protocol error';
  190. SQLITE_EMPTY: Result := 'Database is empty';
  191. SQLITE_SCHEMA: Result := 'The database schema changed';
  192. SQLITE_TOOBIG: Result := 'Too much data for one row of a table';
  193. SQLITE_CONSTRAINT: Result := 'Abort due to contraint violation';
  194. SQLITE_MISMATCH: Result := 'Data type mismatch';
  195. SQLITE_MISUSE: Result := 'Library used incorrectly';
  196. SQLITE_NOLFS: Result := 'Uses OS features not supported on host';
  197. SQLITE_AUTH: Result := 'Authorization denied';
  198. SQLITE_FORMAT: Result := 'Auxiliary database format error';
  199. SQLITE_RANGE: Result := '2nd parameter to sqlite3_bind out of range';
  200. SQLITE_NOTADB: Result := 'File opened that is not a database file';
  201. SQLITE_ROW: Result := 'sqlite3_step() has another row ready';
  202. SQLITE_DONE: Result := 'sqlite3_step() has finished executing';
  203. else
  204. Result := 'Unknown SQLite Error Code "' + IntToStr(SQLiteErrorCode) + '"';
  205. end;
  206. end;
  207. function ColValueToStr(Value: PAnsiChar): AnsiString;
  208. begin
  209. if (Value = nil) then
  210. Result := 'NULL'
  211. else
  212. Result := Value;
  213. end;
  214. end.