Helloworld.gd 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. extends Node
  2. var database := PostgreSQLClient.new()
  3. const USER = "samuel"
  4. const PASSWORD = "123123"
  5. const HOST = "localhost"
  6. const PORT = 5432 # Default postgres port
  7. const DATABASE = "samuel" # Database name
  8. func _init() -> void:
  9. var _error := database.connect("connection_established", self, "_executer")
  10. _error = database.connect("authentication_error", self, "_authentication_error")
  11. _error = database.connect("connection_closed", self, "_close")
  12. #Connection to the database
  13. _error = database.connect_to_host("postgresql://%s:%s@%s:%d/%s" % [USER, PASSWORD, HOST, PORT, DATABASE])
  14. print('trying to connect')
  15. print(_error)
  16. func _physics_process(_delta: float) -> void:
  17. database.poll()
  18. func _authentication_error(error_object: Dictionary) -> void:
  19. prints("Error connection to database:", error_object["message"])
  20. func _executer() -> void:
  21. print(database.parameter_status)
  22. var datas := database.execute("""
  23. select * from niggers;
  24. """)
  25. # var datas := database.execute("""
  26. # BEGIN;
  27. # /*Helloworld*/
  28. # SELECT concat('Hello', 'World');
  29. # COMMIT;
  30. # """)
  31. #The datas variable contains an array of PostgreSQLQueryResult object.
  32. for data in datas:
  33. #Specifies the number of fields in a row (can be zero).
  34. print(data.number_of_fields_in_a_row)
  35. # This is usually a single word that identifies which SQL command was completed.
  36. # note: the "BEGIN" and "COMMIT" commands return empty values
  37. print(data.command_tag)
  38. print(data.row_description)
  39. print(data.data_row)
  40. prints("Notice:", data.notice)
  41. if not database.error_object.empty():
  42. prints("Error:", database.error_object)
  43. database.close()
  44. func _close(clean_closure := true) -> void:
  45. prints("DB CLOSE,", "Clean closure:", clean_closure)
  46. func _exit_tree() -> void:
  47. database.close()