使用 TIMESTAMP 数据类型将日期时间条目插入 table
Insert datetime entry into table with TIMESTAMP data type
我正在尝试创建一个系统(使用 discord 机器人,但这与此无关),其中列出了用户的违规行为,例如发生的时间、地点、原因等,我想要一个 "date" 记录事件发生时间戳的数据类型。
我尝试将 DATE
数据类型设置为 "timestamp"(以及 "datetime",但发生了同样的错误)
conn1 = apsw.Connection('./dbs/warns.db')
warns = conn1.cursor()
warns.execute(
"""
CREATE TABLE IF NOT EXISTS warns
(id INTEGER PRIMARY KEY AUTOINCREMENT,
date timestamp,
server string,
user string,
author string,
reason string)
"""
)
def add_warn(guild: str, user: str, author: str, reason):
now = datetime.datetime.utcnow()
with conn1:
warns.execute("INSERT INTO warns (date, server, user, author, reason) VALUES (?, ?, ?, ?, ?)", (now, guild, user, author, reason))
我最终收到 TypeError: Bad binding argument type supplied - argument #1: type datetime.datetime
错误
从 create table 语句(AUTOINCREMENT
没有下划线)和 apsw
标签的语法来看,我怀疑你使用的是 SQLite 数据库.
如果您希望将当前时间戳插入 timestamp
列,我的第一个建议是直接在 SQL 中执行,而不是使用在 python 中生成的变量.在 sqlite 中,CURRENT_TIMESTAP
为您提供当前的 date/time 作为时间戳:
warns.execute(
"INSERT INTO warns (wdate, server, user, author, reason) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)",
(guild, user, author, reason)
)
另一个可以进一步简化代码的选项是在创建 table 时为时间戳列设置默认值。那么,插入的时候可以忽略这一列,放心会赋值正确:
warns.execute(
"""
CREATE TABLE IF NOT EXISTS warns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
wdate timestamp DEFAULT CURRENT_TIMESTAMP,
server string,
user string,
author string,
reason string
)
"""
)
def add_warn(guild: str, user: str, author: str, reason):
with conn1:
warns.execute(
"INSERT INTO warns (server, user, author, reason) VALUES (?, ?, ?, ?)",
(now, guild, user, author, reason)
)
注意:date
不是一个合理的列名,因为它与数据类型名称冲突。我在上面的所有代码中将其重命名为 wdate
。
我正在尝试创建一个系统(使用 discord 机器人,但这与此无关),其中列出了用户的违规行为,例如发生的时间、地点、原因等,我想要一个 "date" 记录事件发生时间戳的数据类型。
我尝试将 DATE
数据类型设置为 "timestamp"(以及 "datetime",但发生了同样的错误)
conn1 = apsw.Connection('./dbs/warns.db')
warns = conn1.cursor()
warns.execute(
"""
CREATE TABLE IF NOT EXISTS warns
(id INTEGER PRIMARY KEY AUTOINCREMENT,
date timestamp,
server string,
user string,
author string,
reason string)
"""
)
def add_warn(guild: str, user: str, author: str, reason):
now = datetime.datetime.utcnow()
with conn1:
warns.execute("INSERT INTO warns (date, server, user, author, reason) VALUES (?, ?, ?, ?, ?)", (now, guild, user, author, reason))
我最终收到 TypeError: Bad binding argument type supplied - argument #1: type datetime.datetime
错误
从 create table 语句(AUTOINCREMENT
没有下划线)和 apsw
标签的语法来看,我怀疑你使用的是 SQLite 数据库.
如果您希望将当前时间戳插入 timestamp
列,我的第一个建议是直接在 SQL 中执行,而不是使用在 python 中生成的变量.在 sqlite 中,CURRENT_TIMESTAP
为您提供当前的 date/time 作为时间戳:
warns.execute(
"INSERT INTO warns (wdate, server, user, author, reason) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)",
(guild, user, author, reason)
)
另一个可以进一步简化代码的选项是在创建 table 时为时间戳列设置默认值。那么,插入的时候可以忽略这一列,放心会赋值正确:
warns.execute(
"""
CREATE TABLE IF NOT EXISTS warns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
wdate timestamp DEFAULT CURRENT_TIMESTAMP,
server string,
user string,
author string,
reason string
)
"""
)
def add_warn(guild: str, user: str, author: str, reason):
with conn1:
warns.execute(
"INSERT INTO warns (server, user, author, reason) VALUES (?, ?, ?, ?)",
(now, guild, user, author, reason)
)
注意:date
不是一个合理的列名,因为它与数据类型名称冲突。我在上面的所有代码中将其重命名为 wdate
。