Home Conectar a Base de datos con Python
Post
Cancel

Conectar a Base de datos con Python

Clase Database

Clase encargada de conectar con la base de datos y funciones para realizar diferentes consultas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    
import pymysql
from warnings import filterwarnings
filterwarnings('ignore', category=pymysql.Warning)


class Database:
    def __init__(self):
        self.connection = pymysql.connect(
            host='IP',
            port=3306,
            user='user',
            password='password',
            db='db'
        )

        try:
            self.cursor = self.connection.cursor()

        except Exception as e:
            print("Error al conectar con la BD " + e)
            raise

    def __disconnect__(self):

        self.connection.close()

    def select_data(self):

        sql = 'SELECT * FROM process'

        try:
            self.cursor.execute(sql)
            ticker = self.cursor.fetchall()

            return ticker
        except Exception as e:
            print("Error en función select_data " + e)
            raise

        finally:
            self.__disconnect__()


    def update_status_proces(self, status, idprocess):

        sql = 'UPDATE `process` SET  `status`= %s WHERE `idprocess` = %s'
        
        values = (status, idprocess)
        try:
            self.cursor.execute(sql, values)
            self.connection.commit()

        except pymysql.IntegrityError as e:
            if not e[0] == 1062:
                raise
            print("Oops! Fallo al insertar en la tabla process")
        finally:

            self.__disconnect__()
    

    def update_proces(self, idprocess, process_name, process_pid,  update_date, creation_date, status, pc):

        sql = 'UPDATE `process` SET  `idprocess` = %s, `process_name`= %s, `process_pid`=%s, `update_date`= %s, `creation_date`= %s, `status`= %s, `pc`= %s WHERE `idprocess` = %s'
        
        values = (idprocess, process_name, process_pid, update_date, creation_date, status, pc, idprocess)
        try:
            self.cursor.execute(sql, values)
            self.connection.commit()

        except pymysql.IntegrityError as e:
            if not e[0] == 1062:
                raise
            print("Oops! Fallo al insertar en la tabla process")
        finally:

            self.__disconnect__()
    
    

Actualizar campos en base de datos con Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    
    from datetime import datetime
    import time
    import DB
    
    
    def update_process(creation_date):

    try:
        db = DB.Database()

        idprocess = str(socket.gethostname()) + "_process"
        process_pid = os.getpid()
        process_name = "processControl.py"
        update_date = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        status = True
        pc = socket.gethostname()

        db.update_proces(idprocess, process_name, process_pid,
                          update_date, creation_date, status, pc)

        print('Actualizado: ', process_name, update_date, pc)

    except Exception as e:
        print('##### Error en función update_process ####', e)
        raise

This post is licensed under CC BY 4.0 by the author.