2

i'm trying to output data from sqlite database. I did it like in PyQt's examples, but it does not work. It seems that the database is opened, but the code is giving errors, that it does not. Could you tell me why and how i can fix this?

#####################################################################
# test.py                                                           #
# ! /usr/bin/env python                                             #
#  -*- coding: utf-8 -*-                                            #
#####################################################################

from PyQt4 import QtGui, QtCore, QtSql
from src.database import DBase

class Test(QtGui.QMainWindow):

    def __init__(self):
        super(Test, self).__init__()

        self.tview = QtGui.QTableView()
        self.setCentralWidget(self.tview)
        model = QtSql.QSqlTableModel(self)
        model.setTable("person")
        model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        model.select()
        model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QObject.trUtf8(model, "№"))
        model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QObject.trUtf8(model, "Name"))
        model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QObject.trUtf8(model, "Lastname"))

        self.tview.setModel(model)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    db = DBase()
    db.connection_db()
    window = Test()
    window.resize(800, 600)
    window.show()
    sys.exit(app.exec_())

#####################################################################
# database.py                                                       #
# ! /usr/bin/env python                                             #
#  -*- coding: utf-8 -*-                                            #

from PyQt4 import QtSql, QtGui

class DBase():
    def connection_db(self):
        db=QtSql.QSqlDatabase.addDatabase("QSQLITE", "Base")
        db.setDatabaseName("db.sqlite")
        if db.open():
            print 'DataBase is now opened.'
            query = QtSql.QSqlQuery()
            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')")
            query.exec_("insert into person values(104, 'Roberto', 'Robitaille')")
        return True

In output it gives this:

C:\Python27\python.exe D:test.py
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
DataBase is now opened.
SaulTigh
  • 913
  • 2
  • 14
  • 27

1 Answers1

7

In your connection_db function when you call QtSql.QSqlDatabase.addDatabase("QSQLITE", "Base") the last parameter ("Base") is the connection name.

When you create your QSqlQuery you don't specify a database instance to use so it uses the application's default connection. Because you gave your connection a connection name your application does not have a default connection so the QSqlQuery does not see an open database.

Try changing query = QtSql.QSqlQuery() to query = QtSql.QSqlQuery(db) in your connection_db function. This lets the QSqlQuery know which connection it should be using.

Documentation:

QSqlDatabase.addDatabase

QSqlQuery

royatirek
  • 2,437
  • 2
  • 20
  • 34
Gary Hughes
  • 4,400
  • 1
  • 26
  • 40
  • Can I ask one more question? The `self.tview` still empty, why? – SaulTigh Mar 03 '12 at 07:03
  • 2
    It's the same thing - you need to pass your database instance to QSqlTableModel when creating it. Take a look at the documentation for QSqlTableModel.__init__(). If you're only using one database connection you might be better off not giving it a connection name when adding a database. That way queries and table models will not need to be told which connection to use, they'll just use the default (nameless) connection. You only really need to use connection names if you're opening multiple connections or using multiple databases. – Gary Hughes Mar 03 '12 at 15:07