I can't open the table PostgreSQL
-
Hi!
I can't open table PostgreSQLQSqlDatabase 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? -
Error indicates that Test2 table is not there. Can you recheck whether table exist ?
-
@Mikeeeeee Did you verify that the database has a table named Test2?
-
Hi,
Might be a silly question but are you sure the database connection is opened before the your create the
QSqlTableModel
object ? -
And are you sure the table is not inside a schema? tables() only returns the table names without the schema iirc.
-
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 useSearchTableModel->setTable("\"Test2\"");
Or simply
usedon't use upper-case in your table and column-names.