Advertisement
Guest User

Untitled

a guest
May 11th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. import sqlite3
  2. con = sqlite3.connect("bartozon.db")
  3. sql = con.cursor()
  4.  
  5. def login():
  6.     username = input("Inserisci username")
  7.     password = input("Inserisci password")
  8.  
  9.     #cerca utenti con corrispondenti username e password
  10.     sql.execute("""
  11.                SELECT userId, nome, cognome
  12.                FROM utenti
  13.                WHERE username='{}' AND password='{}'
  14.                """.format(username, password))
  15.  
  16.     lista_utente = sql.fetchall()
  17.    
  18.     if len(lista_utente) > 0:
  19.         info_utente = lista_utente[0]
  20.         id_utente = info_utente[0]
  21.         nome_utente = info_utente[1]
  22.         cognome_utente = info_utente[2]
  23.         print("Benvenuto, {} {}".format(nome_utente, cognome_utente))
  24.  
  25.         return id_utente
  26.        
  27.     else:
  28.         print("Utente non trovato!")
  29.  
  30. def esplora():
  31.     sql.execute("""
  32.                SELECT itemId, nome, prezzo, stock
  33.                FROM mercanzia
  34.                WHERE stock>0
  35.                """)
  36.  
  37.     lista_mercanzia = sql.fetchall()
  38.  
  39.     print("ID - NOME - PREZZO - STOCK")
  40.     for item in lista_mercanzia:
  41.         print("{} €{} {}".format(item[1], item[2], item[3]))
  42.  
  43. def compra(id_utente):
  44.     id_merce=input("Inserisci l'id di quello che vuoi comprare")
  45.     sql.execute("""
  46.                SELECT prezzo, stock
  47.                FROM mercanzia
  48.                WHERE stock>0 AND itemId={}
  49.                """.format(id_merce))
  50.  
  51.     lista_mercanzia=sql.fetchall()
  52.     if len(lista_mercanzia) > 0:
  53.         prezzo = lista_mercanzia[0][0]
  54.         stock = lista_mercanzia[0][1]
  55.        
  56.         #aggiorna la tabella degli ordini, aggiungendo un ordine
  57.         sql.execute("""
  58.                    INSERT INTO ordini(userId, somma)
  59.                    VALUES ('{}', {})
  60.                    """.format(id_utente, prezzo))
  61.        
  62.         #aggiorna la tabella della mercanzia
  63.         #togliendo 1 dalla quantità dell'oggetto acquistato
  64.         sql.execute("""
  65.                    UPDATE mercanzia
  66.                    SET stock = '{}'
  67.                    """.format(stock-1))
  68.         print("Acquisto andato a buon fine")
  69.     else:
  70.         print("L'id inserito non è corretto")
  71.  
  72. def infoAccount(id_utente):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement