Advertisement
Guest User

Untitled

a guest
Jun 8th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import mysql.connector #these lines import the necessary modules for this program to work.
  2. from mysql.connector import errorcode
  3. from tabulate import tabulate
  4.  
  5.  
  6. try:
  7. db = mysql.connector.connect(user='ipass',password='ipass123',host='192.168.10.1',database='IPASSDB')
  8. #The line above sets the database, using the mysql connector it tries to connect to a distant database.
  9. c = db.cursor()
  10. while True:
  11. #The line beneath we are printing what the options are for the user.
  12. optie = input("Welke actie wilt u uitvoeren (1t/m3) \n 1. Alle product informatie opvragen. \n "
  13. "2. Product aanpassen. \n 3. Product toevoegen")
  14. if optie == "1": #This if statement selects everything in the table producten, the loop puts each row in a tuple.
  15. c.execute("SELECT * FROM producten")
  16. rows = c.fetchall()
  17. product_tuples = []
  18. for row in rows:
  19. #print(row)
  20. product_tuples.append(row)
  21. #print(product_tuples)
  22. product_list = list(product_tuples) #with this line we make the tuple into a list
  23. #Down here we print the table we've made from the list. Because tabulate won't accept tuple.
  24. print(tabulate(product_list, headers=["typenummer","voorraad","prijs in euro","gewicht in gram",
  25. "opslag in MB","afmeting lxbxh","fabrikant_id"],
  26. tablefmt="grid"))
  27. elif optie == "2":
  28. pass
  29. elif optie == "3":
  30. #after option 3 is chosen, we ask for the details about the product the user wants to add.
  31. type_in = input("Wat is het typenummer dat u wilt toevoegen?")
  32. voorraad_in = input("Wat is de begin voorraad voor dit product?")
  33. gewicht_in = input("Wat is het gewicht van het product? (in gram)")
  34. prijs_iv = input("Wat is de prijs van dit product? (in euros)")
  35. afm_in = input("Wat zijn de afmetingen van dit product? (in cm, lxbxh)")
  36. poort_in = input("Welke poorten heeft dit product? (1 voor Gigabit, 2 voor FastEthernet)")
  37. fab_in = input("Wat is de fabrikant_id?")
  38. opslag_in = input ("Wat is de opslag van dit product? (in MB)")
  39. #add_product is a mysql line we will use.
  40. add_product = ("INSERT INTO producten"
  41. "(typenummer, voorraad, prijs, gewicht, opslag, afmeting, fab_id) "
  42. "VALUES(%s, %s, %s, %s, %s, %s, %s)")
  43. add_product_data = (type_in, voorraad_in, prijs_iv, gewicht_in, opslag_in, afm_in, fab_in)
  44. c.execute(add_product, add_product_data) #Combines the made variables from the questions with the line.
  45. db.commit()
  46. print("Information committed")
  47. continue
  48.  
  49. else:
  50. print("Dit is geen geldige optie, keuze maken uit 1,2 of 3.")
  51.  
  52.  
  53.  
  54. #if the connection to the database failed, we several print statements the user could use
  55. #for troubleshooting.
  56. except mysql.connector.Error as err:
  57. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  58. print("Verkeerde user of wachtwoord")
  59. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  60. print("Verkeerde database opgegeven, of deze bestaat niet")
  61. else:
  62. print(err)
  63. else:
  64. print('sluit')
  65.  
  66. db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement