Using user input to find information in Mysql database
Use python-mysql . It is a dbapi-compatible module that allows you to talk to the database.
import MySQLdb
user_input = raw_input("Please enter barcode and press Enter button: ")
db = MySQLdb.connect(passwd="moonpie",db="thangs")
mycursor = db.cursor()
mycursor.execute("""SELECT name, price FROM Product
WHERE barcode = %s""", (user_input,))
# calls fetchone until None is returned (no more rows)
for row in iter(mycursor.fetchone, None):
print row
If you want something more high-level, consider using SQLAlchemy as a layer. This may allow you to:
product = session.query(Product).filter(Product.barcode == user_input).scalar() print product.name, product.price
source to share
This is a very ambiguous question. What you want can be done in different ways depending on what you really want to do.
How will your users enter the barcode? Are they going to use a barcode scanner? Do they enter barcode numbers manually?
Will this work on a desktop / laptop or will it work on a handheld device?
The barcode scanner stores barcodes for later retrieval or sends them directly to your computer. Will it send them via USB cable or wireless?
source to share
Let's start by looking at entering a barcode as plain old text.
It's been quite a while since I've worked with barcode scanners, but I doubt they've changed so much that the older ones were just copied onto the keyboard, so from a programming standpoint, the net result was a stream of characters in the keyboard buffer. either printed or scanned makes no difference.
If the device you configured is different from this, you will need to write something to deal with this before moving on to query the database.
If you have one of the playback devices, plug it in, fire up notepad, start scanning some barcodes, and see what happens.
source to share