SQLite
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至2015年已经有15个年头,SQLite也迎来了一个版本 SQLite 3已经发布。
工作原理
不像常见的客户-服务器范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分。所以主要的通信协议是在编程语言内的直接API调用。这在消耗总量、延迟时间和整体简单性上有积极的作用。整个数据库(定义、表、索引和数据本身)都在宿主主机上存储在一个单一的文件中。它的简单的设计是通过在开始一个事务的时候锁定整个数据文件而完成的。
SQLite的一些基本操作跟SQL很类似,基本上有SQL基础的都能看明白。
SQLite的图像查看工具有很多,比如SQLiteSpy、SQLiteBrowser、SQLiteStudio等。
SQLite默认是utf8编码,使用pragma encoding可以看出数据库的编码。
建立数据库后,可以直接输入“pragma encoding = UTF8/UTF16”来改变编码,但数据库有了数据以后,编码是不可以修改的。
SQLite的源码可以http://www.sqlite.org获得。关于SQLite的更进一步的语法和信息,请参http://www.sqlite.com.cn/http://www.sqlitecn.org。
delphi中使用sqlite3
这里有一个delphi中使用sqlite3的demo:A simple Delphi wrapper for Sqlite 3 | Tim Anderson's IT Writing
这个demo中包含了sqlite3.pas,sqlite3table.pas,sqlite.dll三个文件,里面包含了操作sqlite3的源代码,利用这三个文件,就不需要第三方组件了
添加步骤:
将simple sqlite 3.0 for delphi 中的 sqlite3.pas,sqlite3table.pas拷贝至工程所在的文件夹。并在工程中添加这两个个文件。
拷贝 sqlite.dll到编译生成exe文件的文件夹。这个根据个人的设定。
Sqlite3.pas 源码:
unit 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;
TSQLiteBackup = pointer;
type
PPAnsiCharArray = ^TPAnsiCharArray;
TPAnsiCharArray = array[0 … (MaxInt div SizeOf(PAnsiChar))-1] of PAnsiChar;
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;
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_Backup_Init(DestDb: TSQLiteDB; DestDbName: PAnsiChar; SourceDb: TSQLiteDB; SourceDbName: PAnsiChar): TSqliteBackup; cdecl; external SQLiteDLL name ‘sqlite3_backup_init’;
function SQLite3_Backup_Step(hBackup: TSQLiteBackup; nPage: integer): integer; cdecl; external SQLiteDLL name ‘sqlite3_backup_step’;
function SQLite3_Backup_Finish(hBackup: TSQLiteBackup): integer; cdecl; external SQLiteDLL name ‘sqlite3_backup_finish’;
function SQLite3_Backup_Remaining(hBackup: TSQLiteBackup): integer; cdecl; external SQLiteDLL name ‘sqlite3_backup_remaining’;
function SQLite3_Backup_Pagecount(hBackup: TSQLiteBackup): integer; cdecl; external SQLiteDLL name ‘sqlite3_backup_pagecount’;
//
// 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.
//
type
TSQLite3Destructor = procedure(Ptr: Pointer); cdecl;
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 SQLiteFieldType(SQLiteFieldTypeCode: Integer): AnsiString;
function SQLiteErrorStr(SQLiteErrorCode: Integer): AnsiString;
implementation
uses
SysUtils;
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 := '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 := '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.
SQLiteTable3.pas源码:
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:
- 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;
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 Backup(TargetDB: TSQLiteDatabase): integer; Overload;
function Backup(TargetDB: TSQLiteDatabase; targetName: Ansistring; sourceName: Ansistring): integer; Overload;
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 AddParamNull(name: string);
property DB: TSQLiteDB read fDB;
published
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: pointer); cdecl;
{$IFDEF WIN32}
function SystemCollate(Userdta: pointer; Buf1Len: integer; Buf1: pointer;
Buf2Len: integer; Buf2: pointer): integer; cdecl;
{$ENDIF}
implementation
procedure DisposePointer(ptr: pointer); cdecl;
begin
if assigned(ptr) then
freemem(ptr);
end;
{$IFDEF WIN32}
function SystemCollate(Userdta: pointer; Buf1Len: integer; Buf1: pointer;
Buf2Len: integer; Buf2: pointer): integer; cdecl;
begin
Result := CompareStringW(LOCALE_USER_DEFAULT, 0, PWideChar(Buf1), Buf1Len,
PWideChar(Buf2), Buf2Len) - 2;
end;
{$ENDIF}