Finalizado la primera version del wrapper de Mysql. Empezado a trabajar en el cuerpo del explorer
This commit is contained in:
parent
0278ea68a0
commit
b1b7de13f8
6 changed files with 125 additions and 15 deletions
56
core/mysql_wrapper.py
Normal file
56
core/mysql_wrapper.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import mysql.connector
|
||||
|
||||
anuncios_db_parameters = {'host': '46.183.115.154',
|
||||
'database': 'anuncios',
|
||||
'user': 'pablo',
|
||||
'password': 'noesfacilvivirsinpin'}
|
||||
|
||||
class DatabaseWrapper():
|
||||
|
||||
def __init__(self, connection_parameters):
|
||||
self.host = connection_parameters['host']
|
||||
self.database = connection_parameters['database']
|
||||
self.user = connection_parameters['user']
|
||||
self.password = connection_parameters['password']
|
||||
self.connection = None
|
||||
|
||||
self.ping()
|
||||
|
||||
def connect(self):
|
||||
try:
|
||||
self.connection = mysql.connector.connect(host = self.host,
|
||||
database = self.database,
|
||||
user = self.user,
|
||||
password = self.password)
|
||||
except Exception as e:
|
||||
print("Could not connect to the database.")
|
||||
print(e)
|
||||
|
||||
def disconnect(self):
|
||||
if self.connection.is_connected():
|
||||
self.connection.disconnect()
|
||||
else:
|
||||
print("Connection was not active.")
|
||||
|
||||
def ping(self):
|
||||
self.connect()
|
||||
self.disconnect()
|
||||
|
||||
def query(self, query_statement, query_parameters = None, dictionary = False):
|
||||
self.connect()
|
||||
if self.connection.is_connected():
|
||||
execution_cursor = self.connection.cursor(dictionary = dictionary)
|
||||
execution_cursor.execute(query_statement, query_parameters)
|
||||
self.disconnect()
|
||||
return execution_cursor
|
||||
|
||||
|
||||
def query_dict(self, query_statement, query_parameters = None):
|
||||
return self.query(query_statement, query_parameters, dictionary = True)
|
||||
|
||||
def get_anunciosdb():
|
||||
return DatabaseWrapper(anuncios_db_parameters)
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue