Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 307
antuniooh Avatar asked Oct 29 '25 12:10

antuniooh


1 Answers

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.

like image 79
SME Avatar answered Nov 01 '25 03:11

SME



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!