I am trying to create a system (with a discord bot, but that's not relevant to this) where it lists infractions of a user, like when it happened, where, why, etc. and I want a "date" datatype that logs the timestamp that it happened.
I tried having the DATE datatype to be "timestamp" (as well as "datetime", but the same error happens)
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))
I end up getting a TypeError: Bad binding argument type supplied - argument #1: type datetime.datetime error
From the syntax of the create table statement (
AUTOINCREMENTwithout an underscore) and theapswtag, I suspect that you are using a SQLite database.If you are looking to insert the current timestamp to a
timestampcolumn, my first suggestion is to do it directly in SQL, instead of using a variable generated in python. In sqlite,CURRENT_TIMESTAPgives you the current date/time as a timestamp:Another option, that would furthermore simplify your code, is to set a default for the timestamp column when creating the table. Then, you can just ignore this column when inserting, and rest assured that the correct value will be assigned:
Note:
dateis not a sensible column name, since it clashes with a datatype name. I renamed itwdatein all the above code.