Sven van den Boogaart
Sven van den Boogaart

Reputation: 12327

Sqlite rename empty table

Im trying to run the following line:

"ALTER TABLE flocksStartWeight RENAME TO flocksStartWeight_old"

Where flocksStartWeight is an empty sqlite(3.15.2) table. When I run this in sqlite db browser it renames fine but when I try to run it from my qt application it fails with the warning.:

"WARNING: DatabaseConnectionImplementation::executeQuery: Failed for query "ALTER TABLE flocksStartWeight RENAME TO flocksStartWeight_old", () WARNING: "No query Unable to fetch row"

relevant code:

executeQuery call

bool query2Successful = db->executeQuery("ALTER TABLE flocksStartWeight RENAME TO flocksStartWeight_old")->isValid();

The method

DatabaseResultUPtr DatabaseConnectionImplementation::executeQuery(const QString& query, const QVariantList& args)
{
    QMutexLocker tsLocker(&tsLock);

    if (!db.isOpen())
    {
        hDebug(TRACING_CONTEXT_RUNTIME_WARNING) << "DatabaseConnectionImplementation::executeQuery: Failed, database " << dbName << " not open";
        return DatabaseResultUPtr(new DatabaseResult());
    }

    QSqlQuery q(db);
    q.prepare(query);
    for(int i = 0 ; i < args.size() ; i++)
        q.bindValue(i, args.value(i));
    if (!q.exec())
    {
        hDebug(TRACING_CONTEXT_RUNTIME_WARNING) << "DatabaseConnectionImplementation::executeQuery: Failed for query " << q.executedQuery() << ", " << args;
        hDebug(TRACING_CONTEXT_RUNTIME_WARNING) << q.lastError().text();
        return DatabaseResultUPtr(new DatabaseResult());
    }

    QMutexLocker scoped_lock(&queryMutex);
    ++queryCount;

    return DatabaseResultUPtr(new DatabaseResult(q, this));
}

I do many similar renames and they all worked fine. The only difference was that flocksStartWeight did not contain any data. So I manually added one row into the table and tried again. This time it worked fine. How should I rename an empty table? EDIT:

I added:

if(!q.prepare(query))
{
    qDebug() << "Preparing query failed for " << query;
}

It hits the qDebug line for the query, so It is already failing at the prepare method.

EDIT: When I asked the question I was assuming a database state that was wrong. the problem can not be reproduced, question can be removed.

Upvotes: 1

Views: 209

Answers (1)

pablo285
pablo285

Reputation: 2663

When calling q.exec() last error for the query is reset. You should show the last error before exec(). Since you added a check for prepare() it's there where you should show it.

Upvotes: 2

Related Questions