Empezado a trabajar en clase de conexion a MySQL
This commit is contained in:
parent
701236dbc4
commit
0278ea68a0
1 changed files with 48 additions and 0 deletions
48
core/mysql_connection.py
Normal file
48
core/mysql_connection.py
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Contiene las clases necesarias para conectar con la Base de Datos mysql.
|
||||||
|
#
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
import mysql.connector
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseInstance():
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
try:
|
||||||
|
self.connection = mysql.connector.connect(host = self.host,
|
||||||
|
database = self.database,
|
||||||
|
user = self.user,
|
||||||
|
password = self.password)
|
||||||
|
except Error 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, parameters):
|
||||||
|
self.connect()
|
||||||
|
if self.connection.is_connected():
|
||||||
|
cursor = self.connection.cursor()
|
||||||
|
cursor.execute()
|
||||||
|
#SEGUIR AQUIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
|
||||||
Loading…
Add table
Add a link
Reference in a new issue