Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #-*- coding: utf-8 -*-
- '''
- ------------------------------------------------------------------------
- | Created on 03/10/2016 |
- | |
- | @author: Luiz Carlos P.O. Junior && Luis Felipe Caiaffa dos Santos |
- ------------------------------------------------------------------------
- '''
- '''
- --------------------------
- | Importing modules |
- --------------------------
- '''
- from output import Output # Output module to create default returns in the terminal.
- import imp # Find and Load modules
- class Connection():
- '''
- --------------------------------
- | Database connection class |
- --------------------------------
- '''
- dbname = 'arvore'
- user = 'root'
- password = ''
- host = 'localhost'
- out = Output() # Output object
- def __init__(self):
- pass
- def connect(self):
- # Connecting to database
- try:
- self.out.generete_Confirmation('Initializing the connection to the database', 0)
- self.out.generete_Confirmation(self.dbname, 0)
- self.out.generete_Confirmation(self.host, 0)
- self.out.generete_Confirmation(self.user, 0)
- self.out.generete_Confirmation(self.password, 0)
- import MySQLdb # Mysql connection module
- self.out.generete_Confirmation('Engine: Mysql', 1)
- self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password,db=self.dbname)
- pass
- self.cursor = self.conn.cursor()
- self.out.generete_Confirmation("Successfully connected to the database", 0)
- except:
- self.out.generate_Error("An error occurred while trying to connect to the database!")
- def close(self):
- # Finalizing database connection
- try:
- self.out.generete_Confirmation("Closing the connection to the database", 0)
- self.conn.close()
- self.out.generete_Confirmation("Successfully closed connection", 0)
- return True
- except:
- self.out.generate_Error("An error occurred while trying to terminate the connection to the database!")
- return False
- def get_Cursor(self):
- # Recovering the cursor used in queries
- self.out.generete_Confirmation('Getting cursor', 0)
- return self.cursor
- def get_Connection(self):
- # Retrieving database connection
- self.out.generete_Confirmation('Getting connection', 0)
- return self.conn
- def execute(self, query, is_select):
- # Running the query
- self.out.generete_Confirmation('Query: %s' % query, 1)
- self.out.generete_Confirmation('Running the query', 0)
- if is_select:
- data = self.cursor.execute(query)
- self.out.generete_Confirmation("Successfully executed query\n", 0)
- return self.cursor.fetchall()
- else:
- data = self.cursor.execute(query)
- self.out.generete_Confirmation("Successfully executed query\n", 0)
- return [True, self.cursor.lastrowid]
- def commit(self):
- # Commiting a query
- try:
- self.conn.commit()
- self.out.generete_Confirmation("Successfully executed commit", 0)
- return True
- except:
- self.out.generate_Error("An error occurred while trying to commit the information!")
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement