gpt4 book ai didi

Python:将SQLITE列的缓冲区类型转换为字符串

转载 作者:行者123 更新时间:2023-11-28 22:59:04 25 4
gpt4 key购买 nike

我是 Python 2.6 的新手。我一直在尝试在我的 Python 程序中获取 yyyy-mm-dd hh:m:ss 格式的日期 datetime 值。在 Python 中检查列类型时,我收到错误:'buffer' object has no attribute 'decode'。我想使用 strptime() 函数来拆分日期数据并使用它,但我找不到如何将缓冲区转换为字符串。以下是我的代码示例(也可用 here ):

conn = sqlite3.connect("mrp.db.db", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
qryT = """
SELECT dateDefinitionTest FROM t
WHERE IDproject = 4 AND IDstatus = 5
ORDER BY priority, setDate DESC
"""
rec = (4,4)
cursor.execute(qryT,rec)
resultsetTasks = cursor.fetchall()
cursor.close() # closing the resultset
for item in resultsetTasks:
taskDetails = {}
_f = item[10].decode("utf-8")

我得到的异常是:

'buffer' object has no attribute 'decode'

最佳答案

我不确定您的问题可能是什么。以下是您要实现的目标的工作示例,希望对您有所帮助:

#!/usr/bin/env python

import datetime
import sqlite3

conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
cursor.execute("CREATE TABLE t (dateDefinitionTest DATETIME)")
cursor.execute("INSERT INTO t VALUES (?)", (datetime.datetime.now(),))
query = "SELECT dateDefinitionTest FROM t"
cursor.execute(query)
for row in cursor.fetchall():
dt = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S.%f")
print(repr(dt))
print(dt.strftime("%Y-%m-%d %H:%M:%S.%f"))
cursor.close()

哪些输出:

datetime.datetime(2012, 11, 11, 16, 40, 26, 788966)
2012-11-11 16:40:26.788966

关于Python:将SQLITE列的缓冲区类型转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com