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 bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - Retrieve SQLite3 database user_version without library

1 Posts
1 Users
0 Likes
1,990 Views
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

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: