简体   繁体   English

如何在编译时创建 SQLlite 表并在 QT 中插入数据?

[英]How can I Create a SQLlite table and insert data in it in QT at Compile time?

I am working on a QT program where I have to create two SQLite tables and insert data into one of them.我正在开发一个 QT 程序,我必须创建两个 SQLite 表并将数据插入其中一个。 I have a void function called configuraBaseDatos that does it in runtime.我有一个名为configuraBaseDatos的 void function 在运行时执行此操作。 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.我也尝试使用constexpr但 QSqlQuery 不是文字类型。 I think that templates could be a solution but I can't figure out how I can use it.我认为模板可能是一种解决方案,但我不知道如何使用它。

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.我不使用 sqlite 但如果有用于运行脚本文件的现有工具,我不会感到惊讶。 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.这样的脚本文件还可以负责填充 DB 模式,此时 DB 只是被捆绑到随程序分发的任何内容中,而不是在第一次运行时创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM