Solved PostgreSql Query wont show tables form DB
-
Hello Everybody,
I Have the following part of code where i set my connection:QSqlDatabase DataBaseSample=QSqlDatabase::addDatabase("QPSQL","Sample"); void setSample(){ DataBaseSample.setDatabaseName("MyDbName"); DataBaseSample.setUserName("myuser"); DataBaseSample.setPassword("mypw"); DataBaseSample.setHostName("mysite.xy"); bool ok=DataBaseSample.open(); } ...
And after that i have the following code which is list of all the Tables from the given DB:
QStringList Tableslist(int DBID){ QSqlDatabase Current=db->ReturnDataBase(DBID); return Current.tables(); } ... QSqlTableModel *model=MC.TableReturn(Choose); model->setTable(Tabletoshow); model->select(); ui->tableView->setModel(model); ...
i wanted to check with wireshark where i got wrong but Postgre is encrypt better than mysql.
So code is working with MySql but not with PostgreSql what should i do?Thank you for your answer
-
Hi,
First thing would be to check the value returned by the open call and then check for errors on the other parts of your code.
-
According to qDebug that i didnt include in my code(i copied wrong):
void setSample(){ DataBaseSample.setDatabaseName("MyDbName"); DataBaseSample.setUserName("myuser"); DataBaseSample.setPassword("mypw"); DataBaseSample.setHostName("mysite.xy"); bool ok=DataBaseSample.open(); qDebug()<<"Connection established:"<<ok; qDebug()<<DataBaseSample.lastError().text(); }
which is returns with
true
and""
And i dont know any other problems because its work with mysql
-
You should also check the QSqlTableModel::lastError just in case.
And maybe a silly question but are you sure that your PostgreSQL database contains the same content as your MySQL database ?
-
Sorry for late answer.
LastError
dont report any problem either. In the Tables completely the same as the MySQL table scheme... If this application wasn't be used widely, i would not care about PostgreSQL .
It is started to frustrate me that i spend days to configure sql databases in different configuration and they're mess with me indirectly... -
Then maybe something specific to PostgreSQL, are you using the default schema ?
In doubt, this small blog article gives a nice overview of the PostgreSQL schema.
-
Can you show us the body of
db->ReturnDataBase(DBID);
andMC.TableReturn(Choose);
? -
@SGaist
i have a schame named public. with DataGrip show both way,select * from public.Table --or simply Table
QSqlTableModel *TableReturn(int DBID){ QSqlTableModel *model = new QSqlTableModel(nullptr,db->ReturnDataBase(DBID)); qDebug()<<model->tableName(); return model; }
QSqlDatabase ReturnDataBase(int DBID){ QSqlDatabase db; if(DBID==0){ setmydb1(); db=QSqlDatabase::database("mydb0"); return db; }else if (DBID==1) { setmydb1(); db=QSqlDatabase::database("mydb1"); return db; } ... return db; }
i dont know how efficient is this.
-
What does
setmydb1
do ? It's called for both databases. -
setmydb1
is likeSetSample
with different cities, (i don't want to use real data if that is not a problem). And i know it is harder to help when i do stuff like this, so sorry about that
EDIT1:I finally figured out... Sample is not equal sample... I named the DB as Sample and i wanted to return with sample
EDIT2: now its list the tables, but not the content of it...
EDIT3: for examplemodel->setTable("\"City\"");
does the trickThank you for your answers!
-
This post is deleted! -
Are you sure you setup the database properly ?
I find it very strange that you have to add double quotes around the table name to be able to access its content.
-
@SGaist
The Double quotes used by pgadmin 4 and DataGrip too. So probably the same as the`tablename`
in the Mysql syntax but its obligatory in PostgreSQL -
After verification, here you can find why double quotes should be used.
-
Thanks For you Help