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. I can't open the table PostgreSQL
Forum Updated to NodeBB v4.3 + New Features

I can't open the table PostgreSQL

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 7 Posters 1.9k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    I can't open table PostgreSQL

    QSqlDatabase SearchDB = QSqlDatabase::addDatabase("QPSQL");
    SearchDB.setDatabaseName("TestDB1")
    SearchDB.setUserName("postgres");
    SearchDB.setPassword("1111");
    SearchDB.setPort(5432);
    if (SearchDB.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";}
    

    Here qDebag report: "db is opened"

    qDebug()<<SearchDB.tables();
    

    Here qDebag report: ("Test2", "123")

    QSqlTableModel *SearchTableModel = new QSqlTableModel(this);
    SearchTableModel->setTable("Test2");
    SearchTableModel->select(); 
    qDebug()<<SearchTableModel->lastError();
    

    Table Test2 not opened.
    Here qDebag report: SqlError("", "Unable to find table Test2", "")
    Howe can I open the table PostgreSQL?

    jsulmJ S 2 Replies Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Error indicates that Test2 table is not there. Can you recheck whether table exist ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • M Mikeeeeee

        Hi!
        I can't open table PostgreSQL

        QSqlDatabase SearchDB = QSqlDatabase::addDatabase("QPSQL");
        SearchDB.setDatabaseName("TestDB1")
        SearchDB.setUserName("postgres");
        SearchDB.setPassword("1111");
        SearchDB.setPort(5432);
        if (SearchDB.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";}
        

        Here qDebag report: "db is opened"

        qDebug()<<SearchDB.tables();
        

        Here qDebag report: ("Test2", "123")

        QSqlTableModel *SearchTableModel = new QSqlTableModel(this);
        SearchTableModel->setTable("Test2");
        SearchTableModel->select(); 
        qDebug()<<SearchTableModel->lastError();
        

        Table Test2 not opened.
        Here qDebag report: SqlError("", "Unable to find table Test2", "")
        Howe can I open the table PostgreSQL?

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

        @Mikeeeeee Did you verify that the database has a table named Test2?

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mikeeeeee
          wrote on last edited by
          #4

          I created this table in the database.
          Query: qDebug()<<SearchDB.tables();
          report: ("Test2", "123")
          The table is definitely there

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

            Hi,

            Might be a silly question but are you sure the database connection is opened before the your create the QSqlTableModel object ?

            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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              And are you sure the table is not inside a schema? tables() only returns the table names without the schema iirc.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by Mikeeeeee
                #7

                Yes.
                Upper my code.!0_1544634789159_11111.png

                1 Reply Last reply
                0
                • M Mikeeeeee

                  Hi!
                  I can't open table PostgreSQL

                  QSqlDatabase SearchDB = QSqlDatabase::addDatabase("QPSQL");
                  SearchDB.setDatabaseName("TestDB1")
                  SearchDB.setUserName("postgres");
                  SearchDB.setPassword("1111");
                  SearchDB.setPort(5432);
                  if (SearchDB.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";}
                  

                  Here qDebag report: "db is opened"

                  qDebug()<<SearchDB.tables();
                  

                  Here qDebag report: ("Test2", "123")

                  QSqlTableModel *SearchTableModel = new QSqlTableModel(this);
                  SearchTableModel->setTable("Test2");
                  SearchTableModel->select(); 
                  qDebug()<<SearchTableModel->lastError();
                  

                  Table Test2 not opened.
                  Here qDebag report: SqlError("", "Unable to find table Test2", "")
                  Howe can I open the table PostgreSQL?

                  S Offline
                  S Offline
                  Stoyan
                  wrote on last edited by
                  #8

                  @Mikeeeeee
                  You can try to specify also database:

                  QSqlTableModel *SearchTableModel = new QSqlTableModel(this, SearchDB);
                  

                  Do you use QSqlDatabase::addDatabase("QPSQL") anywhere else in your program?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on last edited by
                    #9

                    It's not work: QSqlTableModel *SearchTableModel = new QSqlTableModel(this, SearchDB);
                    I use heare:

                    QSqlDatabase SearchDB = QSqlDatabase::addDatabase("QPSQL");
                    
                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by Christian Ehrlicher
                      #10

                      Postgres is only case-sensitive when the identifier is escaped. Otherwise all is converted to lowercase. See https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
                      Therefore you have to use

                      SearchTableModel->setTable("\"Test2\"");
                      

                      Or simply use don't use upper-case in your table and column-names.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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