Using user input to find information in Mysql database

I need to develop a program using python that will ask the user for a barcode. Then, using that barcode, it will search for mysql to find the corresponding product.

I got a bit stuck on how to get started. Anyone have any tips for me?

0


source to share


4 answers


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

      

+4


source


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 (barcode scanner), then it is just a matter of querying the mysql database for a character string.

+1


source


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?

0


source


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.

0


source







All Articles