-1

I am working on a QT program where I have to create two SQLite tables and insert data into one of them. I have a void function called configuraBaseDatos that does it in runtime. How can I get it at compile time?

Here's what I tried:

void configuraBaseDatos()
{
    QSqlQuery q;
    q.exec("CREATE TABLE IF NOT EXISTS vendedor(id INTEGER PRIMARY KEY NOT NULL,"
           "nombre TEXT NOT NULL UNIQUE, clave TEXT NOT NULL, isAdmin TEXT NOT NULL)");

    q.exec("INSERT INTO vendedor (id, nombre, clave,isAdmin) VALUES (1,'admin', '123456', 'Administrador')");

    q.exec("CREATE TABLE IF NOT EXISTS producto(id INTEGER PRIMARY KEY NOT NULL,"
           "nombre TEXT NOT NULL, marca TEXT NOT NULL , precio DOUBLE NOT NULL, cantidad INT NOT NULL)");
}

I also tried to use constexpr but the QSqlQuery is not a literal type. I think that templates could be a solution but I can't figure out how I can use it.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
JC2102
  • 55
  • 2
  • 5
  • 1
    I think you won't be able to, why don't you verify that the table doesn't exist and then create it in runtime? – eyllanesc Jul 31 '20 at 23:00

1 Answers1

2

To have such external work done at compile time you will need to use (or create if one does not exist) a tool that can be run as part of your build process.

I do not use sqlite but it would not surprise me if there are existing tools for running script files. At that point it would then be a matter of generating a script with the data you want in your table and running the script. Such a script file could also be responsible for populating the DB schema, at which point the DB simply gets bundled into whatever gets distributed with your program rather than being created upon first run.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23