Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import pymysql
  2.  
  3. connection = pymysql.connect(host='localhost',
  4. user='root',
  5. password='raspberry',
  6. db='phpmyadmin',
  7. charset='utf8mb4',
  8. cursorclass=pymysql.cursors.DictCursor)
  9.  
  10. cursor = connection.cursor()
  11.  
  12.  
  13.  
  14.  
  15. #Insert
  16. #cursor.execute("""INSERT INTO Productos (product,price,stock) VALUES ('Limon', %s, %s)""",(2,30))
  17. #cursor.execute(query)
  18. # You need this if you want your changes 'commited' to the database.
  19.  
  20. print("Bienvenido a la fruteria Redessr, estas son tus opciones. \n1- Select. \n2. Insert (product,price,stock)\n3. Delete. \n4-.Modify.")
  21.  
  22. opcion = raw_input()
  23. while (opcion != "q"):
  24. if(opcion == "1"):
  25. print("Select, ingrese el parametro a seleccionar")
  26. value = raw_input()
  27. try:
  28. cursor.execute(("SELECT {0} FROM Productos").format(value))
  29. if cursor.rowcount == 0:
  30. print ('No results matched your query.')
  31. else:
  32. print (cursor.fetchall())
  33. except:
  34. print("error")
  35.  
  36. if(opcion == "2"):
  37. print("Inserte el nombre del producto")
  38. name = raw_input()
  39. print("Inserte el precio")
  40. precio = input()
  41. print("Inserte la cantidad")
  42. stock = input()
  43. cursor.execute("""INSERT INTO Productos (product,price,stock) VALUES (%s, %s, %s)""",(name,precio,stock))
  44. print("Se agrego el dato")
  45. connection.commit()
  46. if(opcion == "3"):
  47. print("Ingrese el producto a eliminar")
  48. product = raw_input()
  49. cursor.execute("""DELETE FROM Productos where product = %s""",(product))
  50. connection.commit()
  51. if(opcion == "4"):
  52. print("Ingrese el nombre del producto a modificar")
  53. name = raw_input()
  54. print("Ingrese la columna a cambiar")
  55. col = raw_input()
  56. print("Ingrese el nuevo dato")
  57. cambio = input()
  58. if(col == "price"):
  59. cursor.execute("""UPDATE Productos SET price = %s where product = %s""",(cambio,name))
  60. else:
  61. cursor.execute("""UPDATE Productos SET stock = %s where product = %s""",(cambio,name))
  62. connection.commit()
  63. print(opcion)
  64. opcion = raw_input()
  65. connection.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement