I have a property of class named m_database.
class SqlThread : public QThread
{
public:
SqlThread();
void run();
private:
QSqlDatabase   m_database;
QString        m_dbfilename;
};
and i instantiate in constructor like the example below:
SqlThread::SqlThread()
{
 m_database = QSqlDatabase::addDatabase("QSQLITE");
}
And i create database in run function, like that (The m_dbFileName i created with other class):
    m_database.setDatabaseName(m_dbfilename);
    if (!m_database.open())
    {
        qWarning("%s", m_database.lastError().text().toLocal8Bit().data());
        return;
    }
    QSqlQuery databaseQuery(m_database);
    databaseQuery.prepare("CREATE TABLE data (id int not null primary key, tu text, data BLOB, puits integer);");
    if (!databaseQuery.exec())
    {
        qWarning("%s", databaseQuery.lastError().text().toLocal8Bit().data());
        return;
    }
Why i receive error message: No query Unable to fetch row ??
Replace the query string to:
databaseQuery.prepare("CREATE TABLE IF NOT EXISTS data (id int not null primary key, tu text, data BLOB, puits integer);");
Otherwise the execution fails if the table already exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With