【发布时间】:2018-12-04 09:52:58
【问题描述】:
对于一个项目,我需要向使用 8 个值的字符串的 R-Pi 发送交易。例如:
boardone='00000001'
这个 boardone 告诉另一个程序打开或关闭继电器。我已经成功地将整个“字符串”值存储在数据库中,但是使用 fetch 时不起作用。这是我的代码:
from datetime import datetime
import MySQLdb as mariadb
connection = mariadb.connect(host="localhost", user="root",
passwd="secretpass", db="secret")
cursor = connection.cursor ()
#We want to recieve the newest transaction from the database
#One other way would be sorting by transaction ID
cursor.execute ("select Column2, Column3 from connectiontest where start
(select MAX(start) from connectiontest)")
received= cursor.fetchone()
cursor.close ()
connection.close ()
Column2 和 Column3 并存储在数据库中(在最大时间戳)当前为:
(00000001,00000001)
其中 Column2 和 Column3 是无符号的、填零的整数。
收到打印时:
received
Out[410]: (1, 1)
纠正这个问题的正确语法是什么?是 fetch 命令的问题还是其他问题?
【问题讨论】:
-
Column2和Column3应该是varchar以便存储前导零 -
zerofill 实际上更像是一种显示特性,通常只在直接显示的查询结果中看到。由于整数类型本身不具有任何类型的填充属性,因此在大多数环境中,查询结果通常会忽略这一点。 如果您在不带引号的情况下为 boardone 分配该值,与我预期的情况类似。
-
@Hackerman 非常感谢,看起来这就是解决方案。我记得早些时候试图让它们成为 varchar,但出了点问题,但现在看起来不错。非常感谢。
-
@Uueerdo 好的,知道这一点非常重要,因为我将继续使用这种格式的数据。谢谢!!
-
很高兴帮助@SwankyTigers