Search code examples
perlsqlitemojolicious

Sqlite unable to find DB file with Mojolicious and Par Packer


I'm writing a program with Perl that uses Mojolicious and Sqlite. I will be using Par Packer to distribute it as an .exe file for deployment on other Windows machines.

I use Strawberry Perl on Windows for the development.

I use the following windows batch file with Par Packer to generate an .exe file:

pp -o hallo.exe ^
    -x ^
    -l libeay32__.dll ^
    -l zlib1__.dll ^
    -l ssleay32__.dll ^
    -a lib ^
    -a public ^
    -a templates ^
    -a "C:\strawberry\perl\site\lib\Mojo\entities.txt;Mojo\entities.txt" ^
    -a "C:\Strawberry\perl\vendor\lib\Mojolicious\resources\public;Mojolicious\public" ^
    -a "C:\Strawberry\perl\vendor\lib\Mojolicious\resources\templates;Mojolicious\templates" ^
    -a "D:\Perl\main.db" ^
    hallo.cgi

This above works perfectly but as soon as I bring in SQLite I got the following error when trying to run hello.exe :

DBD::SQLite::db prepare failed: no such table: libraries

hallo.cgi works fine when run normally. perl hallo.cgi for example. But when everything is compiled into an .exe file the Sqlite error occurs.

What happens here is that main.db (an SQL lite database file) is in the same location as hallo.cgi (or hallo.exe) but when I run hallo.exe SQLite cannot find main.db and thus creates a main.db on it's own but because it created it on it's own there is no data in the database as it cannot find the libraries table.

How would I go about to solve this problem?

In the batch file above that I use for compiling I include main.db for Par Packer but it would seem it completely ignores the line:

-a "D:\Perl\main.db" ^

Solution

  • I have solved this by looking at what $ENV{PWD} is. ($ENV{PWD} indicates the working directory) and using chdir to change to it. Once in it SQlite was able to get the file and work properly. Note that in my source code I have operations that includes libraries and modules that Par packer needs which created a situation where the working directory changed. Changing it to the original working directory solved this.