Results 1 to 7 of 7

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

  1. #1
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    2
    Qt products
    Qt5

    Default Access mdb file - cant get list of tables or views with db.tables() list

    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
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include <QDebug>
    4. #include <QTime>
    5.  
    6. Widget::Widget(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Widget)
    9. {
    10. ui->setupUi(this);
    11. qDebug() << QSqlDatabase::drivers();
    12.  
    13.  
    14. if(!conOpen()){
    15. qDebug() << "not connected " << "\n";
    16. else {
    17. qDebug() << "connected...";
    18. }
    19.  
    20. vrijeme = QTime::currentTime().toString("hh:mm:ss:zzz");
    21. qDebug() << vrijeme << "\n";
    22.  
    23. QSqlQuery sqlTrazi;
    24.  
    25. if(sqlTrazi.exec("select * from labels")){
    26. while(sqlTrazi.next()){
    27. qDebug() << sqlTrazi.value(0).toString() << " " << sqlTrazi.value(1).toString() ;
    28. }
    29. }
    30.  
    31. QStringList list = db.tables(QSql::AllTables);
    32. qDebug() << "Views list\n " << list.join(",").toLocal8Bit().data() << "\n";
    33. if(db.tables().isEmpty()){
    34. qDebug() << "list empty - lista je prazna";
    35. }
    36. else {
    37. qDebug() << "list not empty";
    38. }
    39. Widget::closeConnection();
    40. vrijemeKraj = QTime::currentTime().toString("hh:mm:ss:zzz");
    41. qDebug() << vrijemeKraj << "\n";
    42. qDebug() << "connection closed ";
    43.  
    44. }
    45.  
    46. Widget::~Widget()
    47. {
    48. delete ui;
    49. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th March 2016 at 17:17. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Access mdb file - cant get list of tables or views with db.tables() list

    Does it work with QSql::Tables instead of QSql::AllTables?

  3. #3
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    2
    Qt products
    Qt5

    Default Re: Access mdb file - cant get list of tables or views with db.tables() list

    Nope,
    I got empty list again.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Access mdb file - cant get list of tables or views with db.tables() list

    In the code above, where do you set the variable "db"?

  5. #5
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    2
    Qt products
    Qt5

    Default Re: Access mdb file - cant get list of tables or views with db.tables() list

    Here is widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QtSql/QSqlDatabase>
    6. #include <QSqlError>
    7. #include <QSqlQuery>
    8. #include <QStringList>
    9.  
    10. namespace Ui {
    11. class Widget;
    12. }
    13.  
    14. class Widget : public QWidget
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit Widget(QWidget *parent = 0);
    20. ~Widget();
    21.  
    22. public:
    23. QString vrijeme;
    24. QString vrijemeKraj;
    25. QSqlQuery sqlTrazi;
    26.  
    27.  
    28. bool conOpen(){
    29.  
    30. QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    31.  
    32. db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=D:/Programi/SynSetup/Synesis/Pupilla.mdb");
    33.  
    34. if(!db.open())
    35. {
    36.  
    37. //qDebug() << "Database Error" << db.lastError(); //<< db.lastError().text();
    38.  
    39. return false;
    40.  
    41. }
    42.  
    43. else
    44.  
    45. {
    46. return true;
    47. }
    48.  
    49. }
    50.  
    51.  
    52.  
    53. void closeConnection(){
    54. db.close();
    55. }
    56.  
    57. private:
    58. Ui::Widget *ui;
    59. };
    60.  
    61. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th March 2016 at 17:16. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Access mdb file - cant get list of tables or views with db.tables() list

    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.

  7. The following user says thank you to d_stranz for this useful post:

    GoranSimunic (6th March 2016)

  8. #7
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    2
    Qt products
    Qt5

    Default Re: Access mdb file - cant get list of tables or views with db.tables() list

    Thank you very much for nice explantion. Definitely begginer mistake.(trying to find button solved)

Similar Threads

  1. list tables column name (database)
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2012, 12:18
  2. List of Views for a model
    By gkarthick5 in forum Newbie
    Replies: 3
    Last Post: 23rd June 2011, 09:06
  3. Replies: 4
    Last Post: 13th July 2010, 06:17
  4. Replies: 1
    Last Post: 16th May 2010, 20:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.