Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14185

Is there a better way to connect to the DB? This is an example of DAO [closed]

$
0
0

I would like to know if there is a better way to DB in every DAO, and if there is a possibility to create Parametric queries like in "Java" (setParameter)

import mysql.connectorclass ClienteDAO:    def __init__(self):        self.__connection = mysql.connector.connect(            host='localhost',            port='3306',            database='unirenthub',            user='root',            password='ciccio'        )        self.__cursor = self.__connection.cursor()    def insertCliente(self, cliente):        query= """            INSERT INTO Cliente (email, nome, cognome, tipo_utente, data_nascita, numero_carta, data_scadenza, verificato, password)            VALUES (%s, %s, %s, 'Studente', '2000-03-14', '5555666677778888', '2025-08-12', 1, %s)"""        values = (            cliente.getEmail(), cliente.getNome(), cliente.getCognome(),            cliente.getPassword()        )        self.__cursor.execute(query, values)        self.__connection.commit()

It all works, but it doesn't seem optimal to me. I think python can do better and I'm not making the most of it


Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>