Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

testADOEvents.py 2.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import time
  3. import pythoncom
  4. from win32com.client import Dispatch, DispatchWithEvents, constants
  5. finished = 0 # Flag for the wait loop from (3) to test
  6. class ADOEvents: # event handler class
  7. def OnWillConnect(self, str, user, pw, opt, sts, cn):
  8. # Must have this event, as if it is not handled, ADO assumes the
  9. # operation is cancelled, and raises an error (Operation cancelled
  10. # by the user)
  11. pass
  12. def OnConnectComplete(self, error, status, connection):
  13. # Assume no errors, until we have the basic stuff
  14. # working. Now, "connection" should be an open
  15. # connection to my data source
  16. # Do the "something" from (2). For now, just
  17. # print the connection data source
  18. print("connection is", connection)
  19. print("Connected to", connection.Properties("Data Source"))
  20. # OK, our work is done. Let the main loop know
  21. global finished
  22. finished = 1
  23. def OnCommitTransComplete(self, pError, adStatus, pConnection):
  24. pass
  25. def OnInfoMessage(self, pError, adStatus, pConnection):
  26. pass
  27. def OnDisconnect(self, adStatus, pConnection):
  28. pass
  29. def OnBeginTransComplete(self, TransactionLevel, pError, adStatus, pConnection):
  30. pass
  31. def OnRollbackTransComplete(self, pError, adStatus, pConnection):
  32. pass
  33. def OnExecuteComplete(
  34. self, RecordsAffected, pError, adStatus, pCommand, pRecordset, pConnection
  35. ):
  36. pass
  37. def OnWillExecute(
  38. self,
  39. Source,
  40. CursorType,
  41. LockType,
  42. Options,
  43. adStatus,
  44. pCommand,
  45. pRecordset,
  46. pConnection,
  47. ):
  48. pass
  49. def TestConnection(dbname):
  50. # Create the ADO connection object, and link the event
  51. # handlers into it
  52. c = DispatchWithEvents("ADODB.Connection", ADOEvents)
  53. # Initiate the asynchronous open
  54. dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=%s" % dbname
  55. user = "system"
  56. pw = "manager"
  57. c.Open(dsn, user, pw, constants.adAsyncConnect)
  58. # Sit in a loop, until our event handler (above) sets the
  59. # "finished" flag or we time out.
  60. end_time = time.clock() + 10
  61. while time.clock() < end_time:
  62. # Pump messages so that COM gets a look in
  63. pythoncom.PumpWaitingMessages()
  64. if not finished:
  65. print("XXX - Failed to connect!")
  66. def Test():
  67. from . import testAccess
  68. try:
  69. testAccess.GenerateSupport()
  70. except pythoncom.com_error:
  71. print("*** Can not import the MSAccess type libraries - tests skipped")
  72. return
  73. dbname = testAccess.CreateTestAccessDatabase()
  74. try:
  75. TestConnection(dbname)
  76. finally:
  77. os.unlink(dbname)
  78. if __name__ == "__main__":
  79. Test()