def list(): clients = [] curseur.execute("SELECT * FROM client") results = curseur.fetchall() for row in results: clients.append(Client(row[0], row[1], row[2], row[3])) return render_template('index.html', clients=clients)
def loadColumns(self): curseur.execute("SHOW COLUMNS FROM " + self.__table) if curseur.rowcount == 0 : return False columns = curseur.fetchall() for column in columns: self.__columns.append(column['Field'])
def save(self) : try : curseur.execute("INSERT INTO 'client' VALUES(" + self.nom + ", " + self.prenom + ", " +self.ville+ "," +self.ca+ "," +self.objectif+")") connexion.commit() except: return False return True
def load(self): if self.id == None or self.id == "": raise Error(400,"No id providing to manage the request") try: curseur.execute("SELECT * FROM " + self.__table +" WHERE id='%s'"%self.id) except MySQLdb.Error, e: try: print ("MySQL Error [%d]: %s") % (e.args[0], e.args[1]) raise Error(400,"Error processing request") except IndexError: print ("MySQL Error: %s") % str(e) raise Error(400, "Error processing request")
def save(self): values = [] for column in self.__columns: if getattr(self, column) == None or column == "updated": continue if type(getattr(self, column)) is datetime: values.append(column + " = '" + MySQLdb.escape_string(getattr(self, column).strftime("%Y-%m-%d %H:%M:%S")) + "'") else: values.append(column + " = '" + MySQLdb.escape_string(getattr(self, column)) + "'") request = "INSERT INTO " + self.__table + " SET " + ",".join(values) + " ON DUPLICATE KEY UPDATE " + ",".join(values) try : if curseur.execute(request) and curseur.lastrowid: setattr(self,"id",curseur.lastrowid) connexion.commit() return True except MySQLdb.Error, e: try: print ("MySQL Error [%d]: %s") % (e.args[0], e.args[1]) raise Error(400,"Error occuring processing the request") except IndexError: print ("MySQL Error: %s") % str(e) raise Error(400,"Error occuring processing the request")
def caTotal(self, villes=[]): if (len(villes) == 0): curseur.execute("SELECT ca FROM client") valeurs = curseur.fetchall() total = 0 for v in valeurs: total = total + v[0] return "Le CA total est de %f €" % (total) else: qry = "SELECT ca FROM client" for ville in villes: curseur.execute( "SELECT sum(ca) FROM client WHERE ville = '%s' GROUP BY ville" % (ville)) valeurs = curseur.fetchall() responses.append('Le CA total de %s est de %f' % (ville, valeurs[0][0])) return responses
def _executeSearch(self,request = None,where = []): if request == None: return False if len(where)>0: request += " WHERE " + " AND ".join(where) print request try: curseur.execute(request) except MySQLdb.Error, e: try: print ("MySQL Error [%d]: %s") % (e.args[0], e.args[1]) except IndexError: print ("MySQL Error: %s") % str(e) raise Error(400,"Error occuring processing the request")
def ca(ville): curseur.execute( "SELECT sum(ca) FROM client WHERE ville = '%s' GROUP BY ville" % (ville)) valeurs = curseur.fetchall() return 'Le CA total de %s est de %f euros' % (ville, valeurs[0][0])
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Dos Santos Julien' from config import connexion, curseur curseur.execute("DROP TABLE client") curseur.execute( "CREATE TABLE 'client' (nom text, prenom text, ville text, ca float, objectif float)" ) curseur.execute( "INSERT INTO 'client' VALUES ('Martin','Pierre','Dax','15000',NULL)") curseur.execute( "INSERT INTO 'client' VALUES ('Dupin','Luc','Dax','20000',NULL)") curseur.execute( "INSERT INTO 'client' VALUES ('Dubois','Jean','Pau','25000',NULL)") curseur.execute( "INSERT INTO 'client' VALUES ('Dupont','Pierre','Bordeaux','12000',NULL);") connexion.commit()