0

I'm having trouble connecting an existing database to an instance of QSqlDatabase. Is it possible to do this?

I've read through a good amount of posts related to this issue; most of them imply that it's at least possible to do this. However, I haven't been able to do it. It seems like a pretty straightforward process as well.

    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName("moves.sqlite")  # this is the name of the database. It's on the same line directory as this code 
    # db.setDatabaseName(os.path.abspath("./moves.sqlite"))

    opened = db.open()
    if not opened:
        print("database not found!")
        return

I end up getting the print message "database not found!". I should be able to get the database opened and perform queries on it. Have I got the QSqlDatabase class completely wrong?

Edit: At the request of @musicmante in the comments below, I added these lines:

    print(db.lastError().databaseText())
    print(db.lastError().driverText())

For both, the output was "driver not loaded. I went ahead and added:

    print(QSqlDatabase.drivers())

This got me an empty list.

  • After the failed open, what is the output of `db.lastError().databaseText()` and `db.lastError().driverText()`? – musicamante Aug 31 '19 at 16:13
  • @musicamante I just ran that and the output of both those statements is "driver not loaded". – ThaNyneTray Aug 31 '19 at 16:22
  • 1
    @ThaNyneTray How strange, the sqlite driver should come by default – eyllanesc Aug 31 '19 at 16:54
  • @eyllanesc I think the problem came from the fact that I was working in a conda environment. Somehow, the QSqlDrivers are not installed with conda's PyQt5. I switched over to pip and it works perfectly now. – ThaNyneTray Aug 31 '19 at 19:44
  • @ThaNyneTray It is difficult to help you if you do not indicate that you are using conda, for the next occasion point it out in your question. I recommend you report the bug to conda. – eyllanesc Aug 31 '19 at 19:48
  • @eyllanesc I will point it out next time. I didn't think to point it out, but I will next time. Thanks – ThaNyneTray Aug 31 '19 at 20:09
  • @ThaNyneTray Instead of adding it as a comment in your post, place it as an answer – eyllanesc Aug 31 '19 at 20:22

1 Answers1

0

Ended up finding the root of the problem. I was working on in project within a conda environment previously. I switched over to a virtualenv environment, reinstalled PyQt5 with pip this time, and everything worked fine. It seems that the QSqlDatabase drivers are not included with the conda PyQt5 installation. However, it works fine when installed through pip.