Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite - no query unable to fetch row
Forum Updated to NodeBB v4.3 + New Features

SQLite - no query unable to fetch row

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QApplication::setStyle(QStyleFactory::create("Fusion"));
    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint & ~Qt::WindowMaximizeButtonHint);
    
    
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("data.db");
    db.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE=1");
    
    bool open = db.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 = db.open();
    if (open)
    qDebug() << "Success";
    query.prepare("INSERT INTO party VALUES ('test')");
    query.exec();
    db.close();

    }@

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved