How do you create a new table from a variable?

Of course, the code below inserts the name of the variable, not its content!
How do I do it? Thanks for your help.

var datedujour As String = Format(DateTime.Now.Hour,"00")
db.ExecuteSQL("CREATE TABLE datedujour(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, surname TEXT)")

It may be wise to use “if not exists” to check if such a table already exists, before trying to create it…

var datedujour As String = Format(DateTime.Now.Hour,"00")
Var sql as String = "CREATE TABLE " + datedujour + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, surname TEXT)"
db.ExecuteSQL(sql)

Thanks, but I’ve made a mistake!


I would advise against creating one table for each day in real time …
better create a fixed table “events” with a field named “day_of_month” where you store the day
then it’s a question of filtering the display to select only the day you want.
it’s more “integrity friendly”
just my 2cts.

2 Likes
var datedujour As String = "table_"+Format(DateTime.Now.Hour,"00") // must start with letters
Var sql as String = "CREATE TABLE " + datedujour + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, surname TEXT)"
db.ExecuteSQL(sql)

Tables probably cannot be named as a numeric string. And you should heed @Jean-Yves_Pochez ’ advice. And store the day as an integer.

1 Like

As the user seem not being an expert, I guess you are correct. But things really depends on use case and design. I think that may have some real proper use case like rotating logs being created, and after backup, dropped… But as I said, I guess it is not the current case.

I’ve just realized that you can’t put a number in the name of an SQLite table! If this is true, I find it completely absurd!
Yes, I’m going to go back to a single table with a column named with the day.
Thank you all for your help…

As I showed above, IN you can, it can’t just START WITH.

Same rule for variable names in any language.

It’s the basis too! I’ve really found myself stupid! now it’s working…
Many thanks to you.