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. [SOLVED] QSqlDatabase::addDatabase("QSQLITE","localdb"); problem with name
Forum Update on Monday, May 27th 2025

[SOLVED] QSqlDatabase::addDatabase("QSQLITE","localdb"); problem with name

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.3k 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.
  • C Offline
    C Offline
    conceptid
    wrote on 7 Dec 2013, 18:48 last edited by
    #1

    With this code in the constructor of my mainwindow:
    @
    db = QSqlDatabase::addDatabase("QSQLITE","localdb");
    db.setDatabaseName("../test/test.db");
    if (!db.open()) {
    QMessageBox::critical(0, QObject::tr("Database Error"),
    db.lastError().text());
    }

    model = new QSqlTableModel(this);
    model->setTable("items");
    model->select();
    qDebug() << "SQL LastError:" << model->lastError().text();
    

    @

    and in the destructor:

    @
    db.close();
    QSqlDatabase::removeDatabase("localdb");
    delete ui;
    @

    output at start:
    (I don't have the qmessagebox and my table view stay blank.)
    SQL LastError: " Unable to find table items"

    output at close app:
    QSqlDatabasePrivate::removeDatabase: connection 'localdb' is still in use, all queries will cease to work.

    If I remove the name of the connection, "localdb" and let the system use the default name, I have this:
    @ db = QSqlDatabase::addDatabase("QSQLITE");
    @

    SQL LastError: " "

    and I have my datas shown in the table view... ?!?!?
    No error at close.

    What am I doing wrong with the connection name?

    I need it because this app will have 2 separates connections, one local with sqlite and another to a remote db2 server.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on 7 Dec 2013, 19:55 last edited by
      #2

      Hi,

      For your first problem, you haven't included the database name in your QSqlTableModel constructor so the default connection is used. So create your model like this:
      @
      model = new QSqlTableModel(this,db);
      @

      For your second problem, try to remove the database like this:
      @
      db.close();
      db = QSqlDatabase();
      db.removeDatabase("localdb");
      @

      1 Reply Last reply
      0
      • C Offline
        C Offline
        conceptid
        wrote on 8 Dec 2013, 01:15 last edited by
        #3

        Work for both solutions :)

        I didn't know the model needed to specify the connection name :)

        I understand for the remove that I need to disconnect the db variable with the database name/connection I want to delete... And seems to not have a method to disconnect so, replacing the object with a default one have the same effect...

        Thank you very much :)

        1 Reply Last reply
        0
        • P Offline
          P Offline
          panosk
          wrote on 8 Dec 2013, 09:15 last edited by
          #4

          Nice :) . Don't forget to edit your initial post and add [SOLVED] to the title.

          1 Reply Last reply
          0

          1/4

          7 Dec 2013, 18:48

          • Login

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