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.

dbconnect.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. import sys
  3. from python_tqt.qt import *
  4. from python_tqt.qtsql import *
  5. from frmconnect import frmConnect
  6. from dbpar import *
  7. TRUE = 1
  8. FALSE = 0
  9. def createConnection():
  10. driver = DB_DRIVER
  11. # all qt examples use TQSqlDatabase::addDatabase, but
  12. # this never returns NULL in my experience
  13. drivers = map(str, TQSqlDatabase.drivers())
  14. if driver in drivers:
  15. dlg = dbConnect(driver)
  16. #TODO: make connection parameters accessible
  17. return dlg.exec_loop()
  18. else:
  19. TQMessageBox.warning(None, "Database Error",
  20. "<%s> database driver not found!\n\n"
  21. "Please make sure, that this database adaptor\n"
  22. "is available in your TQt installation.\n" %
  23. (driver), TQMessageBox.Abort | TQMessageBox.Escape)
  24. return FALSE
  25. class dbConnect(frmConnect):
  26. def __init__(self, driver, parent = None):
  27. frmConnect.__init__(self, parent)
  28. self.hostnames = DB_HOSTNAMES
  29. self.hostname = DB_HOSTNAMES[0]
  30. self.databases = DB_DATABASES
  31. self.database = DB_DATABASES[0]
  32. self.username = DB_USERNAME
  33. self.password = DB_PASSWORD
  34. self.dbdriver = driver
  35. self.txtName.setText(self.username)
  36. self.txtPasswd.setText(self.password)
  37. map(self.cmbServer.insertItem, self.hostnames)
  38. map(self.cmbDatabase.insertItem, self.databases)
  39. self.connect(self.buttonHelp, SIGNAL("clicked()"),
  40. self.buttonHelp_clicked)
  41. def accept(self):
  42. self.hostname = self.cmbServer.currentText()
  43. self.database = self.cmbDatabase.currentText()
  44. self.username = self.txtName.text()
  45. self.password = self.txtPasswd.text()
  46. db = TQSqlDatabase.addDatabase(self.dbdriver)
  47. if db:
  48. db.setHostName(self.hostname)
  49. db.setDatabaseName(self.database)
  50. db.setUserName(self.username)
  51. db.setPassword(self.password)
  52. if db.open():
  53. frmConnect.accept(self)
  54. else:
  55. TQMessageBox.warning(self, "Database Error",
  56. "Cannot open %s database on %s!\n\n%s\n%s\n" %
  57. (self.database, self.hostname,
  58. db.lastError().driverText(),
  59. db.lastError().databaseText()), " Ooops ")
  60. def buttonHelp_clicked(self):
  61. TQMessageBox.information(self, "About Connecting",
  62. "Here you specify userid, password, host and database\n"
  63. "for the PyTQt sql examples. If you encounter any problems,\n"
  64. "please read the README file in this folder before posting.\n\n"
  65. "Thanks,\nHans-Peter Jansen <hpj@urpla.net>\n")
  66. if __name__ == "__main__":
  67. app = TQApplication(sys.argv)
  68. if createConnection():
  69. print "ok"
  70. else:
  71. print "cancel"