Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not work!





Share:
Notifications
Clear all

Lazarus - Retrieve SQLite3 database user_version without library  

  RSS

Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1267
January 17, 2020 1:27 AM  

For one of my applications I needed to determine the user_version of a database, without using the SQLite3 library.
This value is not used by SQLite3, but a user can use it to version their database. The default value is 0.

Setting this variable can be done in SQL with "PRAGMA user_version=x;" where "x" is an integer.
You can read the value with SQL as well: "PRAGMA user_version;".

In the SQLite3 specs it is defined as the integer at offset 60 (the bytes 60, 61, 62 and 63 = integer) in the database file.

So I wrote a quick function for this which returns an integer:

-1  : if the file does not exist
0  : if user_version was not set
x  : the user_version value 

Here is my function, hope this is useful to someone out there (uses SysUtils):

{
  Retrieve database user version value without using a library, this is a raw file read!
  This value can be set in SQlite using "PRAGMA user_version=x" where x = integer.
}
function GetSQLite3Database_UserVersion(filename:string):integer;
var
  FileHandle:THandle;
  dbUserVersion:array [1..4] of byte;
const
  SQLite3_ByteOffset_UserVersion = 60; // see:
begin
  Result := -1;
  if FileExists(filename) then
    begin
      FileHandle:=FileOpen(filename,fmOpenRead);
      if FileHandle<>-1 then
        begin
          FileSeek(FileHandle,SQLite3_ByteOffset_UserVersion,fsFromBeginning);
          FileRead(FileHandle,dbUserVersion[1],SizeOf(dbUserVersion));
          FileClose(FileHandle);
          Result := ( dbUserVersion[1]<<24 ) + ( dbUserVersion[2]<<16 ) + ( dbUserVersion[3]<<8 ) + dbUserVersion[4];
        end;
    end;
end; 


ReplyQuote
Share: