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

Lazarus - Retrieve SQLite3 database user_version without library


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
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(filenameinteger;
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

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: