【发布时间】:2018-09-12 14:39:59
【问题描述】:
我想使用带有树莓派的热敏打印机。我想从 mysql 数据库接收打印机供应商 ID 和产品 ID。我的专栏是varchar 类型。
我的代码是
import MySQLdb
from escpos.printer import Usb
db= MySQLdb.connect(host=HOST, port=PORT,user=USER, passwd=PASSWORD, db=database)
cursor = db.cursor()
sql = ("select * from printerdetails")
cursor.execute(sql)
result = cursor.fetchall()
db.close()
for row in result:
printer_vendor_id = row[2]
printer_product_id = row[3]
input_end_point = row[4]
output_end_point = row[5]
print printer_vendor_id,printer_product_id,input_end_point,output_end_point
Printer = Usb(printer_vendor_id,printer_product_id,0,input_end_point,output_end_point)
Printer.text("Hello World")
Printer.cut()
但它不起作用。 id 是字符串。打印命令显示 0x154f 0x0517 0x82 0x02.in my case
Printer = Usb(0x154f,0x0517,0,0x82,0x02)
工作正常。我怎样才能将相同的 id 存储到数据库中并使用它们来配置打印机
【问题讨论】:
-
您正在从名为
printerdetails的表中获取数据并说“它不起作用”。但是你没有向我们展示你的数据。 id 是字符串 不够详细。print row的结果是什么样的?为什么要从表的最后一行创建Usb的实例? -
我编辑了我的代码。希望现在很清楚。不同的打印机会有不同的id。所以我只想从界面更改数据库。有没有其他方法可以自动检测 id 的?我想一次在同一个软件中使用不同的打印机。 @BoarGules
-
打印命令显示 0x154f 0x0517 0x82 0x02 不完全正确。如果你
print一个列表或一个元组,你会看到括号、逗号,也许还有引号。您的问题是翻译row中的内容,但为了帮助您,我需要确切地知道其中的内容。只需将其剪切并粘贴到屏幕上,就像print语句显示的那样,然后在您的问题中将其格式化为代码。 -
如果我写打印结果然后它显示 (('0x154f', 1303L, '0x82', '0x02'),) 如果我写打印printer_vendor_id,printer_product_id,input_end_point,output_end_point 然后它显示0x154f 1303 0x82 0x02。在数据库中,printerdetails 表字段被声明为 varchar。 @BoarGules
-
如果我写打印结果然后它显示 (('0x154f', '0x0517', '0x82', '0x02'),) 如果我写打印printer_vendor_id,printer_product_id,input_end_point,output_end_point 然后它显示 0x154f 0x0517 0x82 0x02。在数据库中,printerdetails 表字段被声明为 varchar。 @BoarGules
标签: python python-2.7 raspberry-pi raspberry-pi3 escpos