Results 1 to 7 of 7

Thread: sqlite - driver not loaded

  1. #1
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default sqlite - driver not loaded

    Hello,

    I'm using Qt to work with a sqlite database but I have an error. If I try to declare the handler for the QSqlDatabase object as a pointer I get the error: Driver not loaded. So if I use

    Qt Code:
    1. db = QSqlDatabase::addDatabase("QSQLITE");
    To copy to clipboard, switch view to plain text mode 

    everything is ok but if I'm trying to use a pointer

    Qt Code:
    1. db = new QSqlDatabase;
    2. db ->addDatabase("QSQLITE");
    To copy to clipboard, switch view to plain text mode 

    I get the error that the Driver is not loaded. Anyone has any idea why the second case doesn't work?

  2. #2
    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: sqlite - driver not loaded

    Have you deployed the SQL drivers into the appropriate subdirectory of your app directory, as specified in the docs?

  3. #3
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: sqlite - driver not loaded

    Well, I have no problem with the SQL driver for the first case (when I don't use a pointer) so I haven't changed the location for the SQL driver. But because I use a pointer you say I might need to have the dll file in the folder of my application?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sqlite - driver not loaded

    The second code snippet doesn't do anything with the return value of addDatabase().

    Quote Originally Posted by Saurian View Post
    Well, I have no problem with the SQL driver for the first case (when I don't use a pointer)
    The first case also uses the initialized database connection returned by addDatabase() and stores it in the local variable.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: sqlite - driver not loaded

    I don't understand what you mean. The code is a little more complex. So basically the code is something like that:

    Qt Code:
    1. int record = 0;
    2.  
    3. //inițializări
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. QSqlQuery *query;
    9. QMessageBox *messageBox = new QMessageBox;
    10.  
    11. ui->setupUi(this);
    12.  
    13.  
    14. //conectare la baza de date
    15. db = QSqlDatabase::addDatabase("QSQLITE");
    16. db.setDatabaseName("conturi.db");
    17. if (!db.open())
    18. {
    19. messageBox ->warning(this, tr("Database Error"), db.lastError().text());
    20. exit(0);
    21. }
    22. else
    23. {
    24. //adăugare listă conturi din baza de date
    25. query = new QSqlQuery(db);
    26. query ->exec("SELECT * FROM cont");
    27. if (!query ->isActive())
    28. {
    29. messageBox ->warning(this, tr("Database Error"), query ->lastError().text());
    30. exit(0);
    31. }
    32. else
    33. {
    34. while (query ->next())
    35. {
    36. ui ->comboBox ->addItem(query ->value(3).toString());
    37. }
    38. }
    39. }
    40. }
    41.  
    42. //curățare tot
    43. MainWindow::~MainWindow()
    44. {
    45. db.close();
    46. delete ui;
    47. }
    To copy to clipboard, switch view to plain text mode 

    And of course there are other line codes for other operations on the database. So what I changed was defining db as a pointer and made changes everywhere from "." to "->" and from "db" to "*db".

    Later edit: So using the method addDatabase I didn't assigned the driver QSQLITE to the variable db? I must use another function or something like that?


    Added after 15 minutes:


    Ok, so I found which was the solution: *db = QSqlDatabase::addDatabase("QSQLITE");
    Last edited by Saurian; 2nd July 2015 at 19:11.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sqlite - driver not loaded

    Quote Originally Posted by Saurian View Post
    So using the method addDatabase I didn't assigned the driver QSQLITE to the variable db?
    If you are still wondering, think about how the method could possibly be able to do that.
    It does know nothing about "db" and prior to your fix you discarded its return value.

    Cheers,
    _

  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: sqlite - driver not loaded

    Quote Originally Posted by Saurian View Post
    I get the error that the Driver is not loaded. Anyone has any idea why the second case doesn't work?
    To elaborate on what @anda_skoa said:

    QSqlDatabase::addDatabase is a static method and you are not operating on the db instance you think you are, because static member methods don't have a "this" pointer and thus do not operate on an instance of an object.

    QSqlDatabase::addDatabase returns a QSqlDatabase instance, but you're not assigning that to a variable, so your attempt to add the database connection is essentially a no-op. You are creating a database instance by calling QSqlDatabase::addDatabase, then you throw away the result, hence the no-op.

    The instance you allocated via "new QSqlDatabase()" is still an invalid database object since you have only invoked its default constructor.

    If you *must* create a QSqlDatabase instance on the heap, try something like this:

    Qt Code:
    1. QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
    To copy to clipboard, switch view to plain text mode 

    Good luck.

Similar Threads

  1. QT & SQLite - driver not loaded
    By Tomasz in forum Newbie
    Replies: 5
    Last Post: 15th June 2014, 11:59
  2. Driver not loaded with sqlite
    By SIFE in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2013, 12:40
  3. Driver not loaded (sqlite) at Ubuntu
    By Simplevolk in forum Newbie
    Replies: 1
    Last Post: 8th February 2013, 14:50
  4. Android / necessitas: SQLITE error ("Driver not loaded")
    By Al_ in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 28th August 2011, 15:06
  5. SQlite driver not loaded error
    By ibergmark in forum Installation and Deployment
    Replies: 2
    Last Post: 17th March 2008, 01:09

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.