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.