1

I was wondering if it was possible to use the datetime module in python to create day-specific tables so that the table's name is the date itself.

date_object = datetime.date.today()
sqlite_create_transfer_table = '''CREATE TABLE IF NOT EXISTS date_object (
                                sender TEXT NOT NULL,
                                recipient TEXT NOT NULL,
                                ID text NOT NULL,
                                Size NOT NULL,
                                Colour NOT NULL,
                                Quantity INTEGER NOT NULL);'''

However this just makes the table titled 'date_object' rather than using the variable. Any help would be greatly appreciated, thanks! <3

1
  • It is generally problematic to want to do this, as it creates multiple tables that you now need to manage instead of one table to manage. Commented Mar 5, 2022 at 18:46

2 Answers 2

1

datetime.date.today() will return a datetime.date object which you must convert to a string, but even then a string like 2022-03-05 is not a valid name for SQLite.
You must enclose it between square brackets or backticks or double quotes.

Try this:

date_object = datetime.date.today()
sqlite_create_transfer_table = f"""CREATE TABLE IF NOT EXISTS [%s](
                                sender TEXT NOT NULL,
                                recipient TEXT NOT NULL,
                                ID text NOT NULL,
                                Size NOT NULL,
                                Colour NOT NULL,
                                Quantity INTEGER NOT NULL);""" % date_object
Sign up to request clarification or add additional context in comments.

Comments

0

You need to concatenate the query something like this:

sqlite_create_transfer_table = '''CREATE TABLE IF NOT EXISTS '+date_object+' (
                            sender TEXT NOT NULL,
                            recipient TEXT NOT NULL,
                            ID text NOT NULL,
                            Size NOT NULL,
                            Colour NOT NULL,
                            Quantity INTEGER NOT NULL);'''

2 Comments

That didn't work, the table is just titled '+date_object+' now
use this sqlite_create_transfer_table = "CREATE TABLE IF NOT EXISTS "+date_object+" ( sender TEXT NOT NULL, recipient TEXT NOT NULL, ID text NOT NULL, Size NOT NULL, Colour NOT NULL, Quantity INTEGER NOT NULL);"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.