Recent

Author Topic: [solved] SQLite DB on OSX cannot be created  (Read 1931 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[solved] SQLite DB on OSX cannot be created
« on: January 08, 2018, 11:09:49 am »
SQLite and OSX related query...can't get SQLite to create a database on OSX

My program works fine when launched within Lazarus on Windows and OSX. It manages to create a database and commit entries to it. No problem. But after compiling on OSX and creating an Application Bundle, and even after copying the binary to within the .app file to MyApp.app/Contents/MacOS and then copying it to a new DMG image where I have given the DMG full read/write permissions for all users, when I launch the application it tells me it found the SQLite library OK but it can't create the database.

I have even chmod +rwx MyApp.app and it still won't have it.

I have no idea why. But it works fine when launched within Lazarus on OSX. So it must be a permissions thing somewhere but I am out of ideas. Can anyone help?

The code the triggers the database stuff is :

Code: Pascal  [Select][+][-]
  1.  {$ifdef darwin}
  2.     SQLDBLibraryLoaderOSX.ConnectionType:='SQLite3';
  3.     if FileExists('/usr/lib/libsqlite3.dylib') then
  4.     begin
  5.       SQLDBLibraryLoaderOSX.LibraryName := '/usr/lib/libsqlite3.dylib';
  6.       SQLDBLibraryLoaderOSX.Enabled := true;
  7.       SQLDBLibraryLoaderOSX.LoadLibrary;
  8.       // Set the filename of the sqlite database
  9.       strFileNameRandomiser := FormatDateTime('YYYY-MM-DD_HH-MM-SS', Now); // use a randomised filename suffix to enable multiple instances
  10.       SQLite3Connection1.DatabaseName := 'DBOSX_' + strFileNameRandomiser + '.sqlite';
  11.       // Create the database
  12.       CreateDatabase(SQLite3Connection1.DatabaseName);  // this fails on OSX but not within Lazarus
  13.       if SQLIte3Connection1.Connected then lblConnectionStatus.Caption:= 'SQLite3 Database connection active';
  14.     end
  15.     else
  16.     begin
  17.       ShowMessage('Cannot create SQLite database. Probably SQLite is not installed on your system (should be /usr/lib/libsqlite3.dylib). Exiting');
  18.       abort;
  19.     end;          
  20.  
  21.  
  22. // Create a fresh SQLite database for each instance of the program
  23. procedure TfrmSQLiteDBases.CreateDatabase(DBaseName : string);
  24. begin
  25.   SQLite3Connection1.Close; // Ensure the connection is closed when we start
  26.   //SQLite3Connection1.Password := txtPass.Text;
  27.   try
  28.     // Make a new database and add the tables
  29.     try
  30.       SQLite3Connection1.Open;
  31.       SQLTransaction1.Active := true;
  32.  
  33.       // Periodically sort the database out to ensure it stays in tip top shape
  34.       // during heavy usage
  35.       SQLite3Connection1.ExecuteDirect('PRAGMA auto_vacuum = FULL;');
  36.       // .... and so on ...            
  37.  
« Last Edit: January 08, 2018, 12:11:04 pm by Gizmo »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: SQLite DB on OSX cannot be created
« Reply #1 on: January 08, 2018, 11:27:51 am »
The only thing that i'm able to deduct from docs is that here it is written how to obtain the path to the application bundle.

Assuming that you should create your database there, you would have to make sure that it is the directory where sqlite tries to create the database. Not providing a path at all could end up in nowhere's land (e.g. a directory where your application does not have read/write access)

edit:
In case the application bundle is not the path to store things then use GetUserDir or GetAppConfigDir (depending on the needs of your program/use).

Another thread with information on bundles and directories (with links) on MacOS.
« Last Edit: January 08, 2018, 11:49:57 am by molly »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: SQLite DB on OSX cannot be created
« Reply #2 on: January 08, 2018, 12:10:46 pm »
Molly...you saved me again!! I did think it would be a path related issue but hadn't considered than a .app file is not really the place where a new SQLite db can be written into!

So yes, I have used :

Code: Pascal  [Select][+][-]
  1. SafePlaceForDB := GetUserDir;
  2. SQLite3Connection1.DatabaseName := SafePlaceForDB + 'DBOSX_' + strFileNameRandomiser + '.sqlite';
  3.  


and now it just gets thrown in /Users/Username/. The program launches OK, created the DB, and then deletes it when the program closes. Superb. Might look at the other way and do it more cleanly soon but for now, that fixes my issue. Life saver Molly. Thanks

 

TinyPortal © 2005-2018