Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. #-*- coding: utf-8 -*-
  3.  
  4.  
  5. '''
  6. ------------------------------------------------------------------------
  7. | Created on 03/10/2016 |
  8. | |
  9. | @author: Luiz Carlos P.O. Junior && Luis Felipe Caiaffa dos Santos |
  10. ------------------------------------------------------------------------
  11. '''
  12.  
  13.  
  14. '''
  15. --------------------------
  16. | Importing modules |
  17. --------------------------
  18. '''
  19.  
  20.  
  21. from output import Output # Output module to create default returns in the terminal.
  22. import imp # Find and Load modules
  23.  
  24.  
  25. class Connection():
  26.  
  27. '''
  28. --------------------------------
  29. | Database connection class |
  30. --------------------------------
  31. '''
  32.  
  33.  
  34.  
  35.  
  36. dbname = 'arvore'
  37. user = 'root'
  38. password = ''
  39. host = 'localhost'
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. out = Output() # Output object
  47.  
  48.  
  49. def __init__(self):
  50. pass
  51.  
  52.  
  53. def connect(self):
  54. # Connecting to database
  55. try:
  56.  
  57.  
  58. self.out.generete_Confirmation('Initializing the connection to the database', 0)
  59. self.out.generete_Confirmation(self.dbname, 0)
  60. self.out.generete_Confirmation(self.host, 0)
  61. self.out.generete_Confirmation(self.user, 0)
  62. self.out.generete_Confirmation(self.password, 0)
  63. import MySQLdb # Mysql connection module
  64. self.out.generete_Confirmation('Engine: Mysql', 1)
  65. self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password,db=self.dbname)
  66. pass
  67.  
  68.  
  69. self.cursor = self.conn.cursor()
  70. self.out.generete_Confirmation("Successfully connected to the database", 0)
  71. except:
  72. self.out.generate_Error("An error occurred while trying to connect to the database!")
  73.  
  74.  
  75. def close(self):
  76. # Finalizing database connection
  77. try:
  78. self.out.generete_Confirmation("Closing the connection to the database", 0)
  79. self.conn.close()
  80. self.out.generete_Confirmation("Successfully closed connection", 0)
  81. return True
  82. except:
  83. self.out.generate_Error("An error occurred while trying to terminate the connection to the database!")
  84. return False
  85.  
  86.  
  87. def get_Cursor(self):
  88. # Recovering the cursor used in queries
  89. self.out.generete_Confirmation('Getting cursor', 0)
  90. return self.cursor
  91.  
  92.  
  93. def get_Connection(self):
  94. # Retrieving database connection
  95. self.out.generete_Confirmation('Getting connection', 0)
  96. return self.conn
  97.  
  98.  
  99. def execute(self, query, is_select):
  100. # Running the query
  101. self.out.generete_Confirmation('Query: %s' % query, 1)
  102. self.out.generete_Confirmation('Running the query', 0)
  103. if is_select:
  104. data = self.cursor.execute(query)
  105. self.out.generete_Confirmation("Successfully executed query\n", 0)
  106. return self.cursor.fetchall()
  107. else:
  108. data = self.cursor.execute(query)
  109. self.out.generete_Confirmation("Successfully executed query\n", 0)
  110. return [True, self.cursor.lastrowid]
  111.  
  112.  
  113. def commit(self):
  114. # Commiting a query
  115. try:
  116. self.conn.commit()
  117. self.out.generete_Confirmation("Successfully executed commit", 0)
  118. return True
  119. except:
  120. self.out.generate_Error("An error occurred while trying to commit the information!")
  121. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement