SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runsqlex.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python
  2. import sys
  3. from python_tqt.qt import *
  4. from python_tqt.qtsql import *
  5. from sqlex import SqlEx
  6. from connect import ConnectDialog
  7. from dbpar import *
  8. TRUE = 1
  9. FALSE = 0
  10. def showError(err, parent):
  11. errStr = TQString("The database reported an error:\n\n")
  12. if not err.databaseText().isEmpty():
  13. errStr.append(err.databaseText())
  14. errStr.append("\n")
  15. if not err.driverText().isEmpty():
  16. errStr.append(err.driverText())
  17. errStr.append("\n")
  18. TQMessageBox.warning(parent, "Error", errStr)
  19. class CustomSqlCursor(TQSqlCursor):
  20. def __init__(self, query = None, autopopulate = TRUE, db = None):
  21. TQSqlCursor.__init__(self, None, autopopulate, db)
  22. self.execQuery(query)
  23. if self.isSelect() and autopopulate:
  24. fields = self.driver().recordInfo(self)
  25. for f in fields:
  26. self.append(f)
  27. self.setMode(TQSqlCursor.ReadOnly)
  28. def select(self, filter, sort = TQSqlIndex()):
  29. return self.execQuery(self.lastQuery())
  30. def primaryIndex(self, prime = TRUE):
  31. return TQSqlIndex()
  32. def insert(self, invalidate = TRUE):
  33. return FALSE
  34. def update(self, invalidate = TRUE):
  35. return FALSE
  36. def delRecords(self, invalidate = TRUE):
  37. return FALSE
  38. def setName(self, name, autopopulate = TRUE):
  39. return
  40. class MainWindow(SqlEx):
  41. def __init__(self,parent = None,name = None,fl = 0):
  42. SqlEx.__init__(self,parent,name,fl)
  43. self.conDiag = ConnectDialog(self, "Connection Dialog", TRUE)
  44. self.firstconn = TRUE
  45. def dbConnect(self):
  46. if self.firstconn:
  47. self.firstconn = FALSE
  48. self.conDiag.editUsername.setText(DB_USERNAME)
  49. self.conDiag.editPassword.setText(DB_PASSWORD)
  50. self.conDiag.editHostname.setText(DB_HOSTNAMES[0])
  51. self.conDiag.editDatabase.setText(DB_DATABASES[0])
  52. for i in range(self.conDiag.comboDriver.count()):
  53. if str(self.conDiag.comboDriver.text(i)) == DB_DRIVER:
  54. self.conDiag.comboDriver.setCurrentItem(i)
  55. break
  56. if self.conDiag.exec_loop() != TQDialog.Accepted:
  57. return
  58. if self.dt.sqlCursor():
  59. self.dt.setSqlCursor()
  60. # close old connection (if any)
  61. if TQSqlDatabase.contains("SqlEx"):
  62. oldDb = TQSqlDatabase.database("SqlEx")
  63. oldDb.close()
  64. TQSqlDatabase.removeDatabase("SqlEx")
  65. # open the new connection
  66. db = TQSqlDatabase.addDatabase(self.conDiag.comboDriver.currentText(), "SqlEx")
  67. if not db:
  68. TQMessageBox.warning(self, "Error", "Could not open database")
  69. return
  70. db.setHostName(self.conDiag.editHostname.text())
  71. db.setDatabaseName(self.conDiag.editDatabase.text())
  72. db.setPort(self.conDiag.portSpinBox.value())
  73. if not db.open(self.conDiag.editUsername.text(),
  74. self.conDiag.editPassword.text()):
  75. showError(db.lastError(), self)
  76. return
  77. self.lbl.setText("Double-Click on a table-name to view the contents")
  78. self.lv.clear()
  79. tables = db.tables()
  80. for t in tables:
  81. lvi = TQListViewItem(self.lv, t)
  82. fields = db.recordInfo(t)
  83. for f in fields:
  84. req = "?"
  85. if f.isRequired() > 0:
  86. req = "Yes"
  87. elif f.isRequired() == 0:
  88. req = "No"
  89. fi = TQListViewItem(lvi, f.name(), TQVariant.typeToName(f.type()), req)
  90. lvi.insertItem(fi)
  91. self.lv.insertItem(lvi)
  92. self.submitBtn.setEnabled(TRUE)
  93. def execQuery(self):
  94. cursor = CustomSqlCursor(self.te.text(), TRUE,
  95. TQSqlDatabase.database("SqlEx", TRUE))
  96. if cursor.isSelect():
  97. self.dt.setSqlCursor(cursor, TRUE, TRUE)
  98. self.dt.refresh()
  99. txt = TQString("Query OK")
  100. if cursor.size() >= 0:
  101. txt.append(", returned rows: %s" % cursor.size())
  102. self.lbl.setText(txt)
  103. else:
  104. if not cursor.isActive():
  105. # an error occured
  106. showError(cursor.lastError(), self)
  107. else:
  108. self.lbl.setText("Query OK, affected rows: %s" %
  109. cursor.numRowsAffected())
  110. def showTable(self, item):
  111. i = item.parent()
  112. if not i:
  113. i = item
  114. cursor = TQSqlCursor(i.text(0), TRUE, TQSqlDatabase.database("SqlEx", TRUE))
  115. self.dt.setSqlCursor(cursor, TRUE, TRUE)
  116. self.dt.setSort(cursor.primaryIndex())
  117. self.dt.refresh(TQDataTable.RefreshAll)
  118. self.lbl.setText("Displaying table %s" % i.text(0))
  119. if __name__ == "__main__":
  120. a = TQApplication(sys.argv)
  121. TQObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
  122. w = MainWindow()
  123. a.setMainWidget(w)
  124. w.show()
  125. a.exec_loop()