Page 1 of 1

Lazarus – Retrieve SQLite3 database user_version without library

Lazarus – Retrieve SQLite3 database user_version without library

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.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Software Development Delphi, Lazarus, Free Pascal Lazarus – Retrieve SQLite3 database user_version without library

  • This topic has 0 replies, 1 voice, and was last updated 1 month ago by Hans.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • 13266

    Hans
    Keymaster

    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; 

Viewing 1 post (of 1 total)



You must be logged in to reply to this topic.