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. QSqlTableModel with SQLITE on multiple databases
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel with SQLITE on multiple databases

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.6k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    HKraft
    wrote on last edited by
    #1

    Hello,

    I'm using SQLITE with multiple databases so to reference a table usually [databasename].[tablename] is to be used. When using sqlite3 I additionally need to use ' characters to enclose the table names, e.g. 'testdb.users'. While trying to do:

    QSqlTableModel model = new QSqlTableModel (this, myDatabase);
    model->setTable ("testdb.users");
    model->setEditStrategy (QSqlTableModel::OnManualSubmit);
    if (model->select ())

    the select call returns false. I also tried:
    model->setTable "'testdb.users'"
    with the same result.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      How are you creating these databases ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HKraft
        wrote on last edited by
        #3

        Hi,
        thanks for reply. All databases are created by programm using QSqlQuery. At first the primary database and then the ones to attach, also using QSqlQuery to attach.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you provide a minimal code sample that shows how you initialise your database ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • H Offline
            H Offline
            HKraft
            wrote on last edited by
            #5

            Hi,
            I don't see much from that but here is a very shortened exampe:

            // attach different database
            if (dbName.compare (currentDbName) != 0)
            {
            QString qs = QString ("attach database '%1' as '%1').arg
            (dbName);
            QSqlQuery q (qs);
            // Error procressing

            }

            // create a table
            QString qs = QString ("create table if not exist '%1' (id int "
            "primary ke, name varchar(256))")
            .arg (getTableName (dbname, tableName));
            // getTable name delivers a QString like "myDatabase.maTable".
            QSqlQuer q (qs);
            // Error processing

            // insert values
            QString qs = QString ("insert into '%1' (%2) values (%3)")
            .arg (getTableName (dbName, tableName))
            .arg (headers.join (", "))
            arg (values.join (", "));
            // headers and values are QStringLists of equal size taht contain // the table headers to be filled with values
            // error processing

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrShawn
              wrote on last edited by
              #6

              Where are you getting the db name from? From what I see, SQLite does not support multiple databases within a .db file. You basically specify which db by which file you connect to.

              Try just:

              model->setTable("users");
              
              1 Reply Last reply
              0
              • H Offline
                H Offline
                HKraft
                wrote on last edited by
                #7

                Please reread the original posting. I never stated that multiple databases reside in a single file but in different ones.
                If you don't have any experiance with that matter why do you replay?

                jsulmJ 1 Reply Last reply
                -1
                • H HKraft

                  Please reread the original posting. I never stated that multiple databases reside in a single file but in different ones.
                  If you don't have any experiance with that matter why do you replay?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @HKraft Please be more kind! If somebody is trying to help you you should not react like this even if the post wasn't helpull...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  4

                  • Login

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