如何在 mysqldb 中存储和查询十六进制值
How to store and query hex values in mysqldb
我想使用 raspberry pi 的热敏打印机。我想从 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 我的案例
Printer = Usb(0x154f,0x0517,0,0x82,0x02)
有效 fine.How 我可以将相同的 ID 存储到数据库并使用它们来配置打印机吗
你的问题是你对 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
我应该指出,这只有效,因为您的数据库 table 只包含一行。 for row in result
遍历 table 中的所有行,但恰好只有一行,这没关系。如果有更多,您的代码将始终获取 table 的最后一行,因为它不检查该行的标识符,因此会重复为相同的变量赋值,直到用完数据。
解决这个问题的方法是在 SQL select
语句中添加一个 where
子句。像
"select * from printerdetails where id = '{0}'".format(printer_id)
现在,因为我不知道您的数据库 table 是什么样子,列名 id
几乎可以肯定是错误的。很可能数据类型也是:它很可能不是字符串。
我想使用 raspberry pi 的热敏打印机。我想从 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 我的案例
Printer = Usb(0x154f,0x0517,0,0x82,0x02)
有效 fine.How 我可以将相同的 ID 存储到数据库并使用它们来配置打印机吗
你的问题是你对 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
我应该指出,这只有效,因为您的数据库 table 只包含一行。 for row in result
遍历 table 中的所有行,但恰好只有一行,这没关系。如果有更多,您的代码将始终获取 table 的最后一行,因为它不检查该行的标识符,因此会重复为相同的变量赋值,直到用完数据。
解决这个问题的方法是在 SQL select
语句中添加一个 where
子句。像
"select * from printerdetails where id = '{0}'".format(printer_id)
现在,因为我不知道您的数据库 table 是什么样子,列名 id
几乎可以肯定是错误的。很可能数据类型也是:它很可能不是字符串。