Results 1 to 5 of 5

Thread: Problem for QSqlQueryModel and QTableView and SQLITE

  1. #1
    Join Date
    Aug 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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;
    Qt Code:
    1. #-*- coding: cp1254 -*-
    2. import sys
    3.  
    4. # import PyQt4 QtCore and QtGui modules
    5. from PyQt4.QtCore import *
    6. from PyQt4.QtGui import *
    7. from PyQt4.QtSql import *
    8.  
    9. class frmFirma(QDialog):
    10. """frmFirma inherits QDialog"""
    11.  
    12. def __init__(self, parent=None):
    13. QDialog.__init__(self, parent)
    14. self.setWindowTitle("Firma Tanımları")
    15. self.setGeometry(200, 200, 600, 510)
    16. self.setAttribute(Qt.WA_DeleteOnClose);
    17.  
    18. self.firmaFormOlustur()
    19. self.veritabaniBaglan()
    20. self.firmaListele()
    21.  
    22.  
    23. def firmaFormOlustur(self):
    24. self.FontAyar = QFont()
    25. self.FontAyar.setFamily("MS Shell Dlg 2")
    26. self.FontAyar.setPointSize(12)
    27.  
    28. self.pnlFirmalar= QGroupBox(" Firmalar ", self)
    29. self.pnlFirmalar.setGeometry(5, 5, 400, 500)
    30. self.pnlFirmalar.setFont(self.FontAyar)
    31.  
    32. self.lyTable = QVBoxLayout(self.pnlFirmalar)
    33.  
    34. self.grFirma = QTableView()
    35. #self.grFirma.setFont(self.FontAyar)
    36. self.lyTable.addWidget(self.grFirma)
    37.  
    38. self.pnlButonlar = QGroupBox(" Ä°ÅŸlemler ", self)
    39. self.pnlButonlar.setGeometry(410, 5, 185, 500)
    40. self.pnlButonlar.setFont(self.FontAyar)
    41.  
    42. self.btYeni = QPushButton(QIcon("icons/yeni.png"), "Yeni Firma")
    43. self.btYeni.setIconSize(QSize(48 ,48))
    44. self.btYeni.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    45.  
    46. self.btDuzelt = QPushButton(QIcon("icons/duzenle.png"), "Düzelt")
    47. self.btDuzelt.setIconSize(QSize(48 ,48))
    48. self.btDuzelt.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    49.  
    50. self.btSil = QPushButton(QIcon("icons/sil.png"), "Firma Sil")
    51. self.btSil.setIconSize(QSize(48 ,48))
    52. self.btSil.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    53.  
    54. self.btSatis = QPushButton(QIcon("icons/satis.png"), "Satış Yap")
    55. self.btSatis.setIconSize(QSize(48 ,48))
    56. self.btSatis.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    57.  
    58. self.btKapat = QPushButton(QIcon("icons/cikis.png"), "Çıkış")
    59. self.btKapat.setIconSize(QSize(48 ,48))
    60. self.btKapat.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    61.  
    62. self.lyButonlar = QVBoxLayout(self.pnlButonlar)
    63. self.lyButonlar.addWidget(self.btYeni)
    64. self.lyButonlar.addWidget(self.btDuzelt)
    65. self.lyButonlar.addWidget(self.btSil)
    66. self.lyButonlar.addWidget(self.btSatis)
    67. self.lyButonlar.addWidget(self.btKapat)
    68.  
    69. self.btKapat.clicked.connect(self.close)
    70.  
    71. def veritabaniBaglan(self):
    72. #connection.createConnection()
    73. self.db = QSqlDatabase.addDatabase("QSQLITE")
    74. self.db.setDatabaseName("ButceTakip.db3")
    75. self.db.open()
    76.  
    77. def firmaListele(self):
    78. self.qFirma = QSqlQuery(self.db)
    79. self.qFirma.exec('SELECT * FROM FIRMALAR')
    80.  
    81. self.firmaModel = QSqlQueryModel(self)
    82. if not self.firmaModel.setQuery(self.qFirma):
    83. print("sorgu hatalı")
    84. self.firmaModel.setHeaderData(0, Qt.Horizontal, "ID")
    85. self.firmaModel.setHeaderData(1, Qt.Horizontal, "First Name")
    86. self.firmaModel.setHeaderData(2, Qt.Horizontal, "Last Name")
    87.  
    88. self.grFirma.setModel(self.firmaModel)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem for QSqlQueryModel and QTableView and SQLITE

    See if this will work:
    Qt Code:
    1. def firmaListele(self):
    2. # self.qFirma = QSqlQuery(self.db)
    3. # self.qFirma.exec('SELECT * FROM FIRMALAR')
    4.  
    5. self.firmaModel = QSqlQueryModel(self)
    6. self.firmaModel.setQuery('select * from person')
    7. if self.firmaModel.lastError().isValid():
    8. print("sorgu hatalı")
    9. self.firmaModel.setHeaderData(0, Qt.Horizontal, "ID")
    10. self.firmaModel.setHeaderData(1, Qt.Horizontal, "First Name")
    11. self.firmaModel.setHeaderData(2, Qt.Horizontal, "Last Name")
    12.  
    13. self.grFirma.setModel(self.firmaModel)
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 10th August 2010 at 03:30.

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. if not self.db.open():
    2. print("Database Error"+self.db.lastError().text)
    3.  
    4. ...
    5.  
    6. if not self.firmaModel.setQuery('select * from firmalar'):
    7. print("Query Error :"+self.db.lastError().text())
    To copy to clipboard, switch view to plain text mode 

    I get the error message: Query Error

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

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default 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.

    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.

    Qt Code:
    1. if not self.firmaModel.setQuery('select * from firmalar'):
    2. print("Query Error :"+self.db.lastError().text())
    To copy to clipboard, switch view to plain text mode 
    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).
    Attached Files Attached Files

  5. #5
    Join Date
    Aug 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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...

Similar Threads

  1. SQL transactions with QSqlQueryModel/QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 18:52
  2. Replies: 1
    Last Post: 26th March 2009, 15:25
  3. SQLITE QTableView scrollTo problem
    By vogeljh in forum Newbie
    Replies: 7
    Last Post: 16th April 2008, 19:45
  4. QTableView/QSqlQueryModel
    By norobro in forum Qt Programming
    Replies: 7
    Last Post: 15th February 2008, 21:52
  5. QSqlQueryModel and QTableView question
    By wbt_ph in forum Newbie
    Replies: 4
    Last Post: 20th September 2006, 15:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.