Important: Please read the Qt Code of Conduct -

SQLite - no query unable to fetch row

  • Hello, I am trying to add a row into SQLite database and getting above error... First part executes well but the second one databaseQuery() not... I don't understand why it works if I leave db open in first part, and not work if I close db in first part and then reopen it in the second... Is it safe to leave database open in the first part? What if application crashes?

    @MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint & ~Qt::WindowMaximizeButtonHint);
    db = QSqlDatabase::addDatabase("QSQLITE");
    bool open =;
    if (open)
        qDebug() << "Success";
    QSqlQuery query;
    query.exec&#40;"CREATE TABLE IF NOT EXISTS login(USERNAME, PASSWORD&#41;"&#41;;
    query.exec&#40;"CREATE TABLE IF NOT EXISTS party(NAME&#41;"&#41;;
    db.close(); // If I remove this line then query in function databaseQuery works@

    @void MainWindow::databaseQuery(QString s)
    QSqlQuery query(db);
    bool open =;
    if (open)
    qDebug() << "Success";
    query.prepare("INSERT INTO party VALUES ('test')");


Log in to reply