Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- con = sqlite3.connect("bartozon.db")
- sql = con.cursor()
- def login():
- username = input("Inserisci username")
- password = input("Inserisci password")
- #cerca utenti con corrispondenti username e password
- sql.execute("""
- SELECT userId, nome, cognome
- FROM utenti
- WHERE username='{}' AND password='{}'
- """.format(username, password))
- lista_utente = sql.fetchall()
- if len(lista_utente) > 0:
- info_utente = lista_utente[0]
- id_utente = info_utente[0]
- nome_utente = info_utente[1]
- cognome_utente = info_utente[2]
- print("Benvenuto, {} {}".format(nome_utente, cognome_utente))
- return id_utente
- else:
- print("Utente non trovato!")
- def esplora():
- sql.execute("""
- SELECT itemId, nome, prezzo, stock
- FROM mercanzia
- WHERE stock>0
- """)
- lista_mercanzia = sql.fetchall()
- print("ID - NOME - PREZZO - STOCK")
- for item in lista_mercanzia:
- print("{} €{} {}".format(item[1], item[2], item[3]))
- def compra(id_utente):
- id_merce=input("Inserisci l'id di quello che vuoi comprare")
- sql.execute("""
- SELECT prezzo, stock
- FROM mercanzia
- WHERE stock>0 AND itemId={}
- """.format(id_merce))
- lista_mercanzia=sql.fetchall()
- if len(lista_mercanzia) > 0:
- prezzo = lista_mercanzia[0][0]
- stock = lista_mercanzia[0][1]
- #aggiorna la tabella degli ordini, aggiungendo un ordine
- sql.execute("""
- INSERT INTO ordini(userId, somma)
- VALUES ('{}', {})
- """.format(id_utente, prezzo))
- #aggiorna la tabella della mercanzia
- #togliendo 1 dalla quantità dell'oggetto acquistato
- sql.execute("""
- UPDATE mercanzia
- SET stock = '{}'
- """.format(stock-1))
- print("Acquisto andato a buon fine")
- else:
- print("L'id inserito non è corretto")
- def infoAccount(id_utente):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement