Create Table From Resources File



  • Hi everyone, I'm trying to run a SQL SCRIPT to Create all my tables. The script is a CreateTable.sql file, and I added this file to Resources.qrc.

    So, I read this file as a QString and try to exec, but the Sqlite Database gives me a error and don't create any table.

    CreateTable.sql:
    @
    CREATE TABLE comandos (
    id INTEGER NOT NULL,
    codigo INTEGER NOT NULL,
    descricao TEXT NOT NULL,
    PRIMARY KEY (id)
    );

    CREATE TABLE codigoResposta (
    id INTEGER NOT NULL,
    codigo INTEGER NOT NULL,
    descricao TEXT NOT NULL,
    PRIMARY KEY(id)
    );

    CREATE TABLE velocidadeCAN (
    id INTEGER NOT NULL,
    configuracao INTEGER NOT NULL,
    taxaTransmissao TEXT NOT NULL,
    padrao INTEGER,
    PRIMARY KEY (id)
    );

    CREATE TABLE parametroConfiguracao (
    id INTEGER NOT NULL,
    parametro INTEGER NOT NULL,
    atalho TEXT NOT NULL,
    descricaoGeral TEXT NOT NULL,
    descricao TEXT,
    valor_int INTEGER,
    valor_num NUMERIC,
    destino TEXT NOT NULL,
    padrao INTEGER,
    PRIMARY KEY (id)
    );

    CREATE TABLE notificacoes (
    id INTEGER NOT NULL,
    codigo INTEGER NOT NULL,
    descricao TEXT NOT NULL,
    PRIMARY KEY (id)
    );
    @

    The code:

    @
    ...
    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
    if (database.open()) {
    QSqlQuery query(database);
    if (query.exec( readCreateTable()))
    {
    ...

    static QString readCreateTable()
    {
    QFile file(":/database/CreateTable.sql");
    if(!file.open(QIODevice::ReadOnly))
    {
    qDebug() << "error opening file: " << file.error();
    return "";
    }
    QTextStream instream(&file);
    QString createTable = instream.readAll();
    qDebug() << "---> " << createTable;
    file.close();
    return createTable;
    }
    @

    @

    There is something wrong with my code?
    I test the SQL Script on SQLite Manager (Firefox Plugin) and works!



  • Sorry guys, I found the error using this:

    @
    qDebug() << "Erro ao criar tabelas. " << query.lastError();
    @

    The error:
    *QSqlError(21, "Unable to execute multiple statements at a time", "not an error")

    Why this limitation? I'm using the ";" between the create's! It's not possible to do that?



  • No, that is not possible. You can only send one command at a time. Separating with a semi-colon is not possible.



  • I see!
    I already resolved using "my separator" and making the split on QString.
    Thanks anyway.

    • I Just have on problem know, the resource files is making cache at the build. I change the file but in test the file still in old version.


  • Run qmake.



  • unfortunately, running qmake is not always sufficient. Just delete the qrc_*.cpp files, they will be regenerated then.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.