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 && setDatabase
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel && setDatabase

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.4k 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all,
    I'm using a QSqlTableModel in a project where I connect to a MySql DB this way:
    @
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QMYSQL");

    db.setHostName("localhost");
    
    db.setUserName(nome);
    db.setPassword(password);
    
    if(db.open())
    {
        .....
    }
    

    @

    as you can see I don't specify the DB name. This is because my application use two database named "db1" and "db2".

    When I execute a query I do this:
    @
    QSqlQuery query;
    query.exec("use db1");
    query.exec("select * from mytable; ");
    @
    or this:
    @
    QSqlQuery query;
    query.exec("select * from db1.mytable; ");
    @

    but how can I use a QSqlTableModel?
    I tried with:
    @
    tableModel->setTable("db1.mytable");
    @
    without success...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Scylla
      wrote on last edited by
      #2

      Create a QSqlTableModel attach the model to your table tableView->setModel(model). Then You can use model->query("you sql command"), Or use QSqlQuery query... and model->setQuery(query).
      After the query I'm always doing model->query("Select * FROM table") and set the headerview again.

      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