Problem for QSqlQueryModel and QTableView and SQLITE
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;
Code:
#-*- coding: cp1254 -*-
import sys
# import PyQt4 QtCore and QtGui modules
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
"""frmFirma inherits QDialog"""
def __init__(self, parent=None):
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.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.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.
setIconSize(QSize(48 ,
48))
self.
btDuzelt.
setIconSize(QSize(48 ,
48))
self.
btSil.
setIconSize(QSize(48 ,
48))
self.
btSatis.
setIconSize(QSize(48 ,
48))
self.
btKapat.
setIconSize(QSize(48 ,
48))
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.setDatabaseName("ButceTakip.db3")
self.db.open()
def firmaListele(self):
self.qFirma.exec('SELECT * FROM FIRMALAR')
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)
Re: Problem for QSqlQueryModel and QTableView and SQLITE
See if this will work:
Code:
def firmaListele(self):
# self.qFirma = QSqlQuery(self.db)
# self.qFirma.exec('SELECT * FROM FIRMALAR')
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)
Re: Problem for QSqlQueryModel and QTableView and SQLITE
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
Code:
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
1 Attachment(s)
Re: Problem for QSqlQueryModel and QTableView and SQLITE
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.
Quote:
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.
Quote:
Code:
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).
Re: Problem for QSqlQueryModel and QTableView and SQLITE
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...