Results 1 to 10 of 10

Thread: QSql - "Error opening database", "unable to open database file"

  1. #1
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSql - "Error opening database", "unable to open database file"

    I have the following code:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSql>
    4. #include <QDebug>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. QSqlDatabase m = QSqlDatabase::addDatabase("QSQLITE");
    12. m.setDatabaseName(":baza:");
    13. bool ok = m.open();
    14. if (ok)
    15. {
    16. qDebug() << "connected" << endl;
    17. }
    18. else
    19. {
    20. qDebug() << "not Connected " << endl;
    21. qDebug() << QSqlDatabase::drivers() << endl;
    22. qDebug() << m.lastError();
    23. }
    24.  
    25. }
    26.  
    27. MainWindow::~MainWindow()
    28. {
    29. delete ui;
    30. }
    31.  
    32. void MainWindow::changeEvent(QEvent *e)
    33. {
    34. QMainWindow::changeEvent(e);
    35. switch (e->type()) {
    36. case QEvent::LanguageChange:
    37. ui->retranslateUi(this);
    38. break;
    39. default:
    40. break;
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    and it's returning:

    Qt Code:
    1. not Connected
    2.  
    3. ("QSQLITE", "QODBC3", "QODBC")
    4.  
    5. QSqlError(-1, "Error opening database", "unable to open database file")
    To copy to clipboard, switch view to plain text mode 

    What's wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSql - "Error opening database", "unable to open database file"

    Colons are forbidden in filenames

  3. #3
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql - "Error opening database", "unable to open database file"

    Hmmm.... It works, but... How to define temporary in-memory database? In QT Examples there is something like this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(":memory:");
    To copy to clipboard, switch view to plain text mode 
    And it works, but not in my program
    Last edited by SykeS; 19th May 2010 at 19:57.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSql - "Error opening database", "unable to open database file"

    What happens if you leave the database name empty. That should be the same as :memory: according to the SQLite docs.

    Anyway: the ":memory:" string is special. I don't know how Qt handles that, if at all.
    In any normal case, the databasename for a SQLite database is also the filename. And you can not have a colon in a filename.

  5. The following user says thank you to tbscope for this useful post:

    SykeS (19th May 2010)

  6. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql - "Error opening database", "unable to open database file"

    Not something like, but exactly like - to quote from the docs
    SQLite also supports in-memory databases, simply pass ":memory:" as the database name.
    Got to keep the loonies on the path ...

  7. The following 2 users say thank you to JD2000 for this useful post:

    insert (13th September 2010), SykeS (19th May 2010)

  8. #6
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql - "Error opening database", "unable to open database file"

    Thanks a lot!

    But now I have the following code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSql>
    4. #include <QDebug>
    5. #include <QMessageBox>
    6. #include <QSqlTableModel>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. initDatabase();
    14.  
    15. model.setTable("person");
    16. model.select();
    17.  
    18. model.setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
    19. model.setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
    20. model.setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    21. ui->tableView->setModel(&model);
    22. qDebug() << model.lastError();
    23. }
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28. }
    29.  
    30. void MainWindow::changeEvent(QEvent *e)
    31. {
    32. QMainWindow::changeEvent(e);
    33. switch (e->type()) {
    34. case QEvent::LanguageChange:
    35. ui->retranslateUi(this);
    36. break;
    37. default:
    38. break;
    39. }
    40. }
    41.  
    42. bool MainWindow::initDatabase()
    43. {
    44. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    45. db.setDatabaseName(":memory:");
    46.  
    47. if(!db.open()){
    48. QMessageBox::critical(this, tr("Błąd bazy danych"), tr("Nie można połączyć się z bazą danych."
    49. "\n%1").arg("test"));
    50. return false;
    51. }
    52.  
    53. QSqlQuery query;
    54. query.exec("create table person (id int primary key, "
    55. "firstname varchar(20), lastname varchar(20))");
    56. query.exec("insert into person values(101, 'Danny', 'Young')");
    57. query.exec("insert into person values(102, 'Christine', 'Holand')");
    58. query.exec("insert into person values(103, 'Lars', 'Gordon')");
    59. query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
    60. query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
    61. return true;
    62. }
    To copy to clipboard, switch view to plain text mode 

    and it prints window like in attachment. Why?
    Attached Images Attached Images

  9. #7
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql - "Error opening database", "unable to open database file"

    You need
    Qt Code:
    1. ui->tableView->show();
    To copy to clipboard, switch view to plain text mode 
    at the end of the MainWindow constructor to actually see the tableview.
    Got to keep the loonies on the path ...

  10. #8
    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: QSql - "Error opening database", "unable to open database file"

    "model" goes out of scope at the end of the constructor leaving nothing for the table view to show.

  11. #9
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql - "Error opening database", "unable to open database file"

    @JD2000 - I use Qt Creator.

    Quote Originally Posted by ChrisW67 View Post
    "model" goes out of scope at the end of the constructor leaving nothing for the table view to show.
    What is the solution?

  12. #10
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql - "Error opening database", "unable to open database file"

    Add this to mainwidow.h
    Qt Code:
    1. private:
    To copy to clipboard, switch view to plain text mode 
    Got to keep the loonies on the path ...

  13. The following user says thank you to JD2000 for this useful post:

    SykeS (20th May 2010)

Similar Threads

  1. Replies: 3
    Last Post: 15th February 2010, 18:27
  2. Replies: 3
    Last Post: 8th July 2008, 20:37
  3. "Treat wchar_t as Built-in Type" to "yes" link error
    By sungaoyong in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2008, 12:45
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

Tags for this Thread

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.