1

I think my question is related to this one, but that one is talking about a png and I'm not sure how to translate that to my case.

I have a database created with sqlite in a file ending with .db. Now I have a program that only reads from this database. Is there a way to include the databse file into the executable?

Now if I compile I have to keep the executable and file together. This dependency could be removed by having the database file at an absolute path, but I'm also working on hpc clusters so that doesn't work. I need a relative path such that the db file is at the same hierarchy as my main.cpp but I want to be able to copy the execuatably without coping the db file, so in some way the db file is already part of the executable.

C. Binair
  • 373
  • 3
  • 14

1 Answers1

1

Asssuming you embedded the SQLite database in your binary, you have an const char *embedded_start and size_t embedded_length that denote the start and length of the database.

The answer depends on how much work you're willing to put in it:

  1. The simplest approach is just to create a temporary file, dump the sqlite bytes in it, then treat it like any other SQLite database.
FILE *db = tmpfile();
fwrite(embedded_start, embedded_length, 1, db);
fflush(db);
std::string path = std::string{"/proc/self/fd/"} + std::to_string(fileno(db));
sqlite3_open_v2(path.c_str(), &dbconn, SQLITE_OPEN_READONLY, nullptr);
  1. If you don't want files lingering on disk, create a file descriptor with memfd_create, load the bytes into it, then let SQLite open /proc/self/fd/X.
  2. Implement a custom VFS that does everything in-memory. This link might be a start.
Botje
  • 21,384
  • 3
  • 27
  • 38
  • I would like to go for the simplest approach. Could you give a link or code example for the first option? – C. Binair Jul 23 '21 at 07:07