【问题标题】:How to store and query hex values in mysqldb如何在mysqldb中存储和查询十六进制值
【发布时间】: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


【解决方案1】:

您的问题是您对Usb 的调用需要整数,如果您这样调用它会起作用

Printer = Usb(0x154f,0x0517,0,0x82,0x02)

但是您的数据库调用正在返回存储为字符串的十六进制值的元组。因此,您需要将这些字符串转换为整数,如下所示:

for row in result:
    printer_vendor_id = int(row[2],16)
    printer_product_id = int(row[3],16)
    input_end_point = int(row[4],16)
    output_end_point = int(row[5],16)

如果你这样做了

print printer_vendor_id,printer_product_id,input_end_point,output_end_point

你会得到

(5455, 1303, 130, 2)

这可能看起来不对,但实际上并非如此,您可以通过要求以十六进制格式显示整数来检查:

print ','.join('0x{0:04x}'.format(i) for i in (printer_vendor_id,printer_product_id,input_end_point,output_end_point))

0x154f,0x0517,0x0082,0x0002

我应该指出,这只有效,因为您的数据库表只包含一行。 for row in result 循环遍历表中的所有行,但恰好只有一个,这没关系。如果还有更多,您的代码将始终获取表的最后一行,因为它不检查行的标识符,因此会重复为相同的变量赋值,直到数据用完。

解决此问题的方法是将where 子句放在您的SQL select 语句中。类似的东西

"select * from printerdetails where id = '{0}'".format(printer_id)

现在,因为我不知道您的数据库表是什么样的,所以列名id 几乎肯定是错误的。而且很可能是数据类型:它很可能不是字符串。

【讨论】:

  • 谢谢。我的问题解决了。你的回答很棒。但是一件事 printer_vendor_id = ','.join('0x{0:04x}'.format(int(row[2],16)))。 print printer_vendor_id 显示 0,X,1,5,4,f
  • @AkashNil 如果您只想格式化row[2],请执行以下操作:printer_vendor_id = '0x{0:04x}'.format(int(row[2],16))。答案中对join() 的调用是一次格式化4 个字段并用逗号将它们串在一起。如果您只想格式化一个字段,请不要使用它。然后发生的事情是join() 获取您期望的值'0x154f',将其分开并用逗号将其串在一起,得到'0,x,1,5,4,f'(带有小写的x)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2011-10-04
  • 2012-09-21
  • 2010-12-15
  • 2012-10-07
相关资源
最近更新 更多