I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product.
I am a bit stuck on how to get started. Does anyone have any tips for me?
I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product.
I am a bit stuck on how to get started. Does anyone have any tips for me?
A barcode is simply a graphical representation of a series of characters (alphanumeric)
So if you have a method for users to enter this code (a barcode scanner), then its just an issue of querying the mysql database for the character string.
That is a very ambiguous question. What you want can be done in many ways depending on what you actually want to do.
How are your users going to enter the bar code? Are they going to use a bar code scanner? Are they entering the bar code numbers manually?
Is this going to run on a desktop/laptop computer or is it going to run on a handheld device?
Is the bar code scanner storing the bar codes for later retrieval or is it sending them directly to the computer. Will it send them through a USB cable or wireless?
To start with, treat the barcode input as plain old text.
It has been quite a while since I worked with barcode scanners, but I doubt they have changed that much, the older ones used to just piggyback on the keyboard input, so from a programming perspective, the net result was a stream of characters in the keyboard buffer, either typed or scanned made no difference.
If the device you are targeting differs from that, you will need to write something to deal with that before you get to the database query.
If you have one of the devices to play with, plug it in, start notepad, start scanning some barcodes and see what happens.
Use python-mysql. It is a dbapi-compatible module that lets you 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. It could allow you to do:
product = session.query(Product).filter(Product.barcode == user_input).scalar()
print product.name, product.price