code_junkie

Using user input to find information in a Mysql database

Nov 13th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. import MySQLdb
  2.  
  3. user_input = raw_input("Please enter barcode and press Enter button: ")
  4.  
  5. db = MySQLdb.connect(passwd="moonpie",db="thangs")
  6. mycursor = db.cursor()
  7. mycursor.execute("""SELECT name, price FROM Product
  8. WHERE barcode = %s""", (user_input,))
  9.  
  10. # calls fetchone until None is returned (no more rows)
  11. for row in iter(mycursor.fetchone, None):
  12. print row
  13.  
  14. product = session.query(Product).filter(Product.barcode == user_input).scalar()
  15. print product.name, product.price
Add Comment
Please, Sign In to add comment