Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql
- connection = pymysql.connect(host='localhost',
- user='root',
- password='raspberry',
- db='phpmyadmin',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- cursor = connection.cursor()
- #Insert
- #cursor.execute("""INSERT INTO Productos (product,price,stock) VALUES ('Limon', %s, %s)""",(2,30))
- #cursor.execute(query)
- # You need this if you want your changes 'commited' to the database.
- print("Bienvenido a la fruteria Redessr, estas son tus opciones. \n1- Select. \n2. Insert (product,price,stock)\n3. Delete. \n4-.Modify.")
- opcion = raw_input()
- while (opcion != "q"):
- if(opcion == "1"):
- print("Select, ingrese el parametro a seleccionar")
- value = raw_input()
- try:
- cursor.execute(("SELECT {0} FROM Productos").format(value))
- if cursor.rowcount == 0:
- print ('No results matched your query.')
- else:
- print (cursor.fetchall())
- except:
- print("error")
- if(opcion == "2"):
- print("Inserte el nombre del producto")
- name = raw_input()
- print("Inserte el precio")
- precio = input()
- print("Inserte la cantidad")
- stock = input()
- cursor.execute("""INSERT INTO Productos (product,price,stock) VALUES (%s, %s, %s)""",(name,precio,stock))
- print("Se agrego el dato")
- connection.commit()
- if(opcion == "3"):
- print("Ingrese el producto a eliminar")
- product = raw_input()
- cursor.execute("""DELETE FROM Productos where product = %s""",(product))
- connection.commit()
- if(opcion == "4"):
- print("Ingrese el nombre del producto a modificar")
- name = raw_input()
- print("Ingrese la columna a cambiar")
- col = raw_input()
- print("Ingrese el nuevo dato")
- cambio = input()
- if(col == "price"):
- cursor.execute("""UPDATE Productos SET price = %s where product = %s""",(cambio,name))
- else:
- cursor.execute("""UPDATE Productos SET stock = %s where product = %s""",(cambio,name))
- connection.commit()
- print(opcion)
- opcion = raw_input()
- connection.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement