使用用户输入在Mysql数据库中查找信息

时间:2022-09-27 16:43:06

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.

我需要使用python设计一个程序,它会询问用户条形码。然后,使用此条形码,它将搜索mysql以查找其相应的产品。

I am a bit stuck on how to get started. Does anyone have any tips for me?

我有点坚持如何开始。有没有人对我有任何提示?

4 个解决方案

#1


Use python-mysql. It is a dbapi-compatible module that lets you talk to the database.

使用python-mysql。它是一个dbapi兼容模块,可让您与数据库通信。

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:

如果您想要更高级别的内容,请考虑使用SQLAlchemy作为图层。它可以让你做:

product = session.query(Product).filter(Product.barcode == user_input).scalar()
print product.name, product.price

#2


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.

因此,如果您有一个方法供用户输入此代码(条形码扫描程序),那么它只是查询mysql数据库中的字符串的问题。

#3


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?

条形码扫描器是否存储条形码以供以后检索,或者是否将它们直接发送到计算机。它会通过USB线或无线发送吗?

#4


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.

如果您要使用其中一个设备,请将其插入,启动记事本,开始扫描一些条形码,看看会发生什么。

#1


Use python-mysql. It is a dbapi-compatible module that lets you talk to the database.

使用python-mysql。它是一个dbapi兼容模块,可让您与数据库通信。

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:

如果您想要更高级别的内容,请考虑使用SQLAlchemy作为图层。它可以让你做:

product = session.query(Product).filter(Product.barcode == user_input).scalar()
print product.name, product.price

#2


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.

因此,如果您有一个方法供用户输入此代码(条形码扫描程序),那么它只是查询mysql数据库中的字符串的问题。

#3


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?

条形码扫描器是否存储条形码以供以后检索,或者是否将它们直接发送到计算机。它会通过USB线或无线发送吗?

#4


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.

如果您要使用其中一个设备,请将其插入,启动记事本,开始扫描一些条形码,看看会发生什么。