[SOLVED] Exec full script by QSqlDatabase



  • Hi, dose any body can say me, what i do wrong?
    I have DB SQLite, and create DB like this:
    @m_database->m_db = QSqlDatabase::addDatabase("QSQLITE");@
    then, i wont exec a sql script witch create a tables and data in this tables, but when i exec a script - implemented just first command, script store at file, i read end exec a sql script by this code:
    @QString script;
    QFileInfo fi(QDir(DATA_FOLDER), DATABASE_INSTALL_SCRIPT);
    QString path = fi.absoluteFilePath();
    QFile file(path);
    if(file.exists()){
    file.open(QIODevice::Text | QIODevice::ReadOnly);
    QTextStream stream(&file);
    script = stream.readAll();
    m_db.exec(script);// when i set breakpoint hear, variable script is not empty, and have a needed script
    }@
    script file contains a text:
    @CREATE TABLE IF NOT EXISTS [tab1] (
    [S_ID] integer NOT NULL,
    [E_ID] int NOT NULL,
    [ALLSET] BOOLEAN NOT NULL,
    [DATETIME] datetime NOT NULL,
    [SOURCE] text NOT NULL,
    CONSTRAINT [PK_tab1] PRIMARY KEY ([S_ID], [E_ID], [ALLSET], [DATETIME])
    );

    CREATE TABLE IF NOT EXISTS [tab2] (
    [S_ID] smallint NOT NULL UNIQUE,
    [NAME] char(50) NOT NULL,
    );

    REPLACE INTO [tab2] (S_ID, NAME) VALUES (1, "Number 1");
    REPLACE INTO [tab2] (S_ID, NAME) VALUES (2, "Number 2");
    REPLACE INTO [tab2] (S_ID, NAME) VALUES (3, "Number 3");@



  • The QSqlDataBase::exec() function is just a convenience function to save you having to create a QSqlQuery object yourself. Reading the docs you'll see that it executes a single SQL statement not an entire sql script.

    You are responsible for passing the script statements to the QSqlDataBase yourself either using exec() or by creating QSqlQuery objects. Don't forget to check that each was successful before moving onto the next.



  • Tnx, i solve this problem that:
    @QSqlQuery query(m_db);
    QStringList queryes = script.split(QChar(';'));
    foreach(QString queryString, queryes){
    query.exec(queryString);
    }
    @



  • Don't forget to check for any errors after issuing each query.



  • [quote author="Maxim Prishchepa" date="1309855003"]Tnx, i solve this problem that:
    @QSqlQuery query(m_db);
    QStringList queryes = script.split(QChar(';'));
    foreach(QString queryString, queryes){
    query.exec(queryString);
    }
    @[/quote]

    This only works if you never have a semicolon (';') in your data.



  • Indeed. For safety, you need to do some more intelligent parsing for your split than just splitting on a semicolon.



  • Tnx a lot!
    I'm rewrite code to this:
    @QString queryToExec;
    foreach(QString queryString, queryes){
    queryToExec += queryString;
    if(queryString.endsWith(QChar(')')) == false){
    queryToExec += ";";
    continue;
    }
    query.exec(queryToExec);
    queryToExec = QString();
    }@
    I think in current data field never happen a string like ");"



  • @
    CREATE sequence foo;
    CREATE sequence bar;
    @

    bang



  • Ah... :)
    Why Qt dose not contains a SQL parser? :))



  • [quote author="Maxim Prishchepa" date="1309862429"]Ah... :)
    Why Qt dose not contains a SQL parser? :))[/quote]

    Why should it? It's hard to implement and used only for corner cases... (yes I saw the smiley :-) )



  • Note that for those corner cases, it may be acceptable to access the API of the underlying driver directly. There may be support for sending multiple statements in one go at that level. Otherwise, perhaps you can look into using Predicate. I believe that that SQL framework does include an SQL parser.


Log in to reply
 

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