mercredi 3 juin 2015

QSqlDatabase failing to open with no useful information using lastError()

I'm having trouble opening a QSqlDatabase (QSQLITE) in PyQT, and I don't seem to be getting a useful error telling me why it's failing. I'm using Python 3.4.3 and PyQt 4.11.3.

Here's a snippet of my code that I quickly hacked together just to try and get the thing working:

def __init__(self):
    self.__database = QtSql.QSqlDatabase.addDatabase("QSQLITE")

    if self.__database.isValid():
        print("DB is Valid")
    else:
        print("DB is Not Valid")

    if not self.__database.open():
        print("Could not open database.")
        print("Text: " + self.__database.lastError().text())
        print("Type: " + str(self.__database.lastError().type()))
        print("Number: " + str(self.__database.lastError().number()))

    query = QtSql.QSqlQuery(self.__database)
    query.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
    query.exec("insert into person values(101, 'Danny', 'Young')");
    query.exec("insert into person values(102, 'Christine', 'Holand')");
    query.exec("insert into person values(103, 'Lars', 'Gordon')");

And here's the output when the above is ran:

DB is Valid

Could not open database.

Text:

Type: 0

Number: -1

QSqlQuery::exec: database not open

QSqlQuery::exec: database not open

QSqlQuery::exec: database not open

QSqlQuery::exec: database not open

Any direction would be much appreciated! It's frustrating that the error messages aren't useful at all.

Aucun commentaire:

Enregistrer un commentaire