PDA

View Full Version : Problem for QSqlQueryModel and QTableView and SQLITE



Dursun ÇAKIR
9th August 2010, 15:11
Hi,

I'm learning Python+Qt4 (PyQt4). I want to do a simple program with theese. I have a form (QDialog), QTableView and a few QPushbutton. I want to list record in the SQLite database. This database file is in same directory with application (.py file). I create QSqlQueryModel and QTableView. But, I can't list records. I can't find a problem.

Thanks for helps,

Error Message: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

Codes;

#-*- coding: cp1254 -*-
import sys

# import PyQt4 QtCore and QtGui modules
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *

class frmFirma(QDialog):
"""frmFirma inherits QDialog"""

def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setWindowTitle("Firma Tanımları")
self.setGeometry(200, 200, 600, 510)
self.setAttribute(Qt.WA_DeleteOnClose);

self.firmaFormOlustur()
self.veritabaniBaglan()
self.firmaListele()


def firmaFormOlustur(self):
self.FontAyar = QFont()
self.FontAyar.setFamily("MS Shell Dlg 2")
self.FontAyar.setPointSize(12)

self.pnlFirmalar= QGroupBox(" Firmalar ", self)
self.pnlFirmalar.setGeometry(5, 5, 400, 500)
self.pnlFirmalar.setFont(self.FontAyar)

self.lyTable = QVBoxLayout(self.pnlFirmalar)

self.grFirma = QTableView()
#self.grFirma.setFont(self.FontAyar)
self.lyTable.addWidget(self.grFirma)

self.pnlButonlar = QGroupBox(" Ä°ÅŸlemler ", self)
self.pnlButonlar.setGeometry(410, 5, 185, 500)
self.pnlButonlar.setFont(self.FontAyar)

self.btYeni = QPushButton(QIcon("icons/yeni.png"), "Yeni Firma")
self.btYeni.setIconSize(QSize(48 ,48))
self.btYeni.setSizePolicy(QSizePolicy.Expanding,QS izePolicy.Expanding)

self.btDuzelt = QPushButton(QIcon("icons/duzenle.png"), "Düzelt")
self.btDuzelt.setIconSize(QSize(48 ,48))
self.btDuzelt.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

self.btSil = QPushButton(QIcon("icons/sil.png"), "Firma Sil")
self.btSil.setIconSize(QSize(48 ,48))
self.btSil.setSizePolicy(QSizePolicy.Expanding,QSi zePolicy.Expanding)

self.btSatis = QPushButton(QIcon("icons/satis.png"), "Satış Yap")
self.btSatis.setIconSize(QSize(48 ,48))
self.btSatis.setSizePolicy(QSizePolicy.Expanding,Q SizePolicy.Expanding)

self.btKapat = QPushButton(QIcon("icons/cikis.png"), "Çıkış")
self.btKapat.setIconSize(QSize(48 ,48))
self.btKapat.setSizePolicy(QSizePolicy.Expanding,Q SizePolicy.Expanding)

self.lyButonlar = QVBoxLayout(self.pnlButonlar)
self.lyButonlar.addWidget(self.btYeni)
self.lyButonlar.addWidget(self.btDuzelt)
self.lyButonlar.addWidget(self.btSil)
self.lyButonlar.addWidget(self.btSatis)
self.lyButonlar.addWidget(self.btKapat)

self.btKapat.clicked.connect(self.close)

def veritabaniBaglan(self):
#connection.createConnection()
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName("ButceTakip.db3")
self.db.open()

def firmaListele(self):
self.qFirma = QSqlQuery(self.db)
self.qFirma.exec('SELECT * FROM FIRMALAR')

self.firmaModel = QSqlQueryModel(self)
if not self.firmaModel.setQuery(self.qFirma):
print("sorgu hatalı")
self.firmaModel.setHeaderData(0, Qt.Horizontal, "ID")
self.firmaModel.setHeaderData(1, Qt.Horizontal, "First Name")
self.firmaModel.setHeaderData(2, Qt.Horizontal, "Last Name")

self.grFirma.setModel(self.firmaModel)

norobro
9th August 2010, 21:54
See if this will work:
def firmaListele(self):
# self.qFirma = QSqlQuery(self.db)
# self.qFirma.exec('SELECT * FROM FIRMALAR')

self.firmaModel = QSqlQueryModel(self)
self.firmaModel.setQuery('select * from person')
if self.firmaModel.lastError().isValid():
print("sorgu hatalı")
self.firmaModel.setHeaderData(0, Qt.Horizontal, "ID")
self.firmaModel.setHeaderData(1, Qt.Horizontal, "First Name")
self.firmaModel.setHeaderData(2, Qt.Horizontal, "Last Name")

self.grFirma.setModel(self.firmaModel)

Dursun ÇAKIR
10th August 2010, 07:41
Thank you,

Unfortunately it didn't run. I get error messages when the program closes. ""QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work." I wrote self.db.close() in closeEvent(). But it didn't work. I tried SQLITE2 database and another SQLITE database.

I wrote


if not self.db.open():
print("Database Error"+self.db.lastError().text)

...

if not self.firmaModel.setQuery('select * from firmalar'):
print("Query Error :"+self.db.lastError().text())


I get the error message: Query Error

self.db.lastError().text() is null

norobro
10th August 2010, 14:11
The attached, including a db from the Qt examples, works fine on my Debian machine running Python 2.6.5+. In addition to the changes posted above, I edited out the icons on the buttons and changed the database name.


QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work That is a warning a that should not keep your app from working.



if not self.firmaModel.setQuery('select * from firmalar'):
print("Query Error :"+self.db.lastError().text())I get the error message: Query Error

self.db.lastError().text() is null That is expected as QSqlQueryModel::setQuery() is a void function (i.e. no return value).

Dursun ÇAKIR
11th August 2010, 08:07
Thank you so much.

I could use your database. Then, I found the problem. Problem was in program which I use for creating a sqlite db (Sqlitepro2009). I used SqliteManager in Firefox and fix problem. :) I can use SQLite db in python.

Thank for helps...