views:

437

answers:

4

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?

+1  A: 

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.

BradC
A: 

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?

adolfojp
A: 

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.

seanb
+2  A: 

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
nosklo
Could you explain a bit the for statement from the first code example? Thank you.
Cristian Ciupitu
@Cristian: you got it.
nosklo
@nosklo: I got it now. I thought that fetchone returns the first record or None and that's it. Anyway, I've looked at the DB API and I'm curious: doesn't MySQLdb support iteration over cursors, e.g. "for row in mycursor"?
Cristian Ciupitu
@Cristian: I don't use MySQLdb and I don't have it installed. The MySQLdb docs don't say anything. Maybe you should try and see. The iter(cursor.fetchone, None) will work on any DBAPI2.0 compatible driver so I prefer to use it anyway.
nosklo
@nosklo: I see. Thanks for the idiom.
Cristian Ciupitu