【问题标题】:Python pymssql Select Sequence Number Returning Strange ValuePython pymssql 选择序列号返回奇怪的值
【发布时间】:2021-12-05 18:03:48
【问题描述】:

我正忙于使用 Python 的 pymssql 查询 SQL Server 数据库,并从以下查询中得到一个奇怪的结果

cursor = conn.cursor()
cursor.execute("select current_value from sys.sequences where name = 'testSequence'")
currentValue = cursor.fetchone()[0]
cursor.close()
print(currentValue)

打印中的值显示为b'.\x00\x00\x00\x00\x00\x00\x00'

然后当我尝试将字节转换为整数时

currentValue = int.from_bytes(cursor.fetchone()[0], byteorder="big")

我得到了一个非常大的数字3530822107858468864

在 SQL Server Studio 中执行完全相同的查询,我可以看到实际的 current_value 只是 40。是我做错了转换还是有什么我没有看到?

注意

只是额外提及我正在使用 Python3(以防有人看不到标签)

【问题讨论】:

  • 哪个数据类型有 current_value
  • 它的数据类型是long
  • 不,不是,我弄错是bigint

标签: sql python-3.x byte pymssql


【解决方案1】:

Bigint转换为float64,你可以使用CAST和VARCHAR并绕过它

cursor = conn.cursor()
cursor.execute("select CAST(current_value AS VARCHAR(20)) AS current_value from sys.sequences where name = 'testSequence'")
currentValue = cursor.fetchone()[0]
cursor.close()
print(currentValue)

【讨论】:

  • 只是好奇,但你为什么将它转换为 varchar 20?是否需要特别长 20 个字符?
  • bigint 的最大数量为 9223372036854775807 + 符号
猜你喜欢
  • 2016-01-27
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多