I have a function that INSERT INTO a table some data. I do not know how much columns the table has (let it be N).
My func gets a list of N-1 params and then
INSERT INTO ... VALUES( *N-1 params* )
The last N-th column is a standart ID column, which i want to set DEFAULT (ID column has the default value = “max” and is auto incremented)
The main problem: due to the fact that i do not know names of the columns i am working with, i can’t manually enter data into the database.
How to insert a row of data, if I don’t know the number of columns, but I know that I have 1 less than it should be and the last column is auto incremented / has the default value?
Advertisement
Answer
If you know that the order of the N-1 params is exactly the same as the order of the columns in the definition of the table (the CREATE
stamenet), then there is no need to enumerate the names of the columns in the INSERT
statement.
Add, 1 more NULL
value to the list for the ID
column (at the end of the list if, as you say it is defined last):
INSERT INTO tablename VALUES(param1, param2, ..., paramN-1, NULL)
By passing NULL for the ID
, it will be incremented since it is defined as AUTOINCREMENT
.
See a simplified demo.