PDA

View Full Version : Access mdb file - cant get list of tables or views with db.tables() list



GoranSimunic
4th March 2016, 19:55
Dear all,
what am I doing wrong in trying to get QString list of all tables in mdb file.
Im getting printed records from *mdb file, from table called labels, (there are two more empty tables in this file),
but list name returns me an empty list?

here is just .cpp


#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTime>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
qDebug() << QSqlDatabase::drivers();


if(!conOpen()){
qDebug() << "not connected " << "\n";
else {
qDebug() << "connected...";
}

vrijeme = QTime::currentTime().toString("hh:mm:ss:zzz");
qDebug() << vrijeme << "\n";

QSqlQuery sqlTrazi;

if(sqlTrazi.exec("select * from labels")){
while(sqlTrazi.next()){
qDebug() << sqlTrazi.value(0).toString() << " " << sqlTrazi.value(1).toString() ;
}
}

QStringList list = db.tables(QSql::AllTables);
qDebug() << "Views list\n " << list.join(",").toLocal8Bit().data() << "\n";
if(db.tables().isEmpty()){
qDebug() << "list empty - lista je prazna";
}
else {
qDebug() << "list not empty";
}
Widget::closeConnection();
vrijemeKraj = QTime::currentTime().toString("hh:mm:ss:zzz");
qDebug() << vrijemeKraj << "\n";
qDebug() << "connection closed ";

}

Widget::~Widget()
{
delete ui;
}

ChrisW67
4th March 2016, 21:09
Does it work with QSql::Tables instead of QSql::AllTables?

GoranSimunic
5th March 2016, 16:36
Nope,
I got empty list again.

ChrisW67
5th March 2016, 20:51
In the code above, where do you set the variable "db"?

GoranSimunic
6th March 2016, 14:58
Here is widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtSql/QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QStringList>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

public:
QString vrijeme;
QString vrijemeKraj;
QSqlDatabase db;
QSqlQuery sqlTrazi;
QStringList list;


bool conOpen(){

QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");

db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=D:/Programi/SynSetup/Synesis/Pupilla.mdb");

if(!db.open())
{

//qDebug() << "Database Error" << db.lastError(); //<< db.lastError().text();

return false;

}

else

{
return true;
}

}



void closeConnection(){
db.close();
}

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

d_stranz
6th March 2016, 17:10
Your problem is that on line 32, you declare another instance of your variable "db", which hides the one you declare as a member variable of your class. As soon as conOpen() ends, this instance of "db" goes out of scope and is destroyed. Your member variable "db" is never initialized to anything.

To fix it, simply remove the word "QSqlDatabase" from line 32.

GoranSimunic
6th March 2016, 19:21
Thank you very much for nice explantion. Definitely begginer mistake.(trying to find button solved)