antuniooh
antuniooh

Reputation: 107

How to create a table in qt with SQLite?

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 ??

Upvotes: 4

Views: 4293

Answers (1)

SME
SME

Reputation: 106

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.

Upvotes: 3

Related Questions