Recent

Author Topic: Question about size of sql file with 3 tables but no records.  (Read 421 times)

jamie

  • Hero Member
  • *****
  • Posts: 7651
I am porting some old C++ code over and I saw that using the Sqlite3 from FreePascal/Laz when creating a new db file with only 3 tables which are the same tables that have been created with existing files from the original C++ code, they seem to be at least twice or more in file size ?

 The C++ of course used the CPP libraries to work with Sqlite but they are very noticeably smaller, however, when doing some checks the contents seem to be correct.

 So I took a simple example from the net using only the Sqlite3 unit and found the size was still 2 or more larger than I think should be.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,Sqlite3,FileUtil;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. Var
  35.   DB: PSQLite3;
  36.   ErrMsg: PChar;
  37.   SQL: String;
  38.   rc: Integer;
  39.   procedure ExecSQL(constRef Statement: String);
  40. begin
  41.   rc := sqlite3_exec(DB, PChar(Statement), nil, nil, @ErrMsg);
  42.   if rc <> SQLITE_OK then
  43.   begin
  44.     Memo1.Lines.Add('SQL error: '+ErrMsg^);
  45.     sqlite3_free(ErrMsg);
  46.     Exit;
  47.   end;
  48. end;
  49.  
  50. begin
  51.   Memo1.Clear;
  52.   rc := sqlite3_open('test.db', @DB);
  53.   try
  54.     // Create first table
  55.     SQL := 'CREATE TABLE IF NOT EXISTS users (' +
  56.            'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
  57.            'name TEXT NOT NULL,' +
  58.            'email TEXT UNIQUE NOT NULL);';
  59.     ExecSQL(SQL);
  60.      // Create second table
  61.     SQL := 'CREATE TABLE IF NOT EXISTS orders (' +
  62.            'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
  63.            'user_id INTEGER NOT NULL,' +
  64.            'product TEXT NOT NULL,' +
  65.            'FOREIGN KEY(user_id) REFERENCES users(id));';
  66.     ExecSQL(SQL);
  67.      // Create third table
  68.     SQL := 'CREATE TABLE IF NOT EXISTS products (' +
  69.            'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
  70.            'name TEXT NOT NULL,' +
  71.            'price REAL NOT NULL);';
  72.     ExecSQL(SQL);
  73.   finally
  74.     sqlite3_close(DB);
  75.     Memo1.Lines.Add('File Size :'+FileSize('test.db').Tostring);
  76.     Memo1.Lines.Add('Sqlite File Version :'+Sqlite3.sqlite3_libversion^);
  77.     DeleteFile('test.db');
  78.   end;
  79.  
  80. end;
  81.  
  82. end.
  83.  
  84.  

That generates ~ 24k of a file size, ~ the same as I get when using the components in lazarus for SQL functions applying the same Tables on new DBases but they are like I said like 3 times the size of what I think they should be.

is it possible the existing files are in Ansi and the newly created files are using UncodeString ?


Any input on this?

Jamie
The only true wisdom is knowing you know nothing

Wallaby

  • Guest
Re: Question about size of sql file with 3 tables but no records.
« Reply #1 on: April 04, 2026, 12:52:36 am »
is it possible the existing files are in Ansi and the newly created files are using UncodeString ?

Either that, or different page size - compare DB headers.


jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: Question about size of sql file with 3 tables but no records.
« Reply #2 on: April 04, 2026, 01:26:09 am »
Ok, Thank you for that header information.

Quote
File Size :24576
Sqlite File Version :3
Magic Header :SQLite format 3
Page Size used :4096

so I didn't check one of the older one's but I am sure its smaller.

The C++ is a 32 bit and I have been doing this via 64 bit, maybe the 32 bit will be smaller.

Jamie
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3349
Re: Question about size of sql file with 3 tables but no records.
« Reply #3 on: April 07, 2026, 08:51:18 am »
Also look at this:
https://sqlite.org/fileformat.html
Specifically Point 1.3.7
Quote
The 4-byte big-endian integer at offset 28 into the header stores the size of the database file in pages.
Look there how many pages are actually used
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018