DJ Tan DJ Tan - 10 months ago 46
Java Question

copy column values if column name is a date

I am trying to copy all values from one table to another table. I am using SQlite. I have multiple columns with the following column names - name,accounts,email, 12/30/2016,01/13/2017.... All columns are named as dates except for the first 3 columns. I am trying to copy using this statement:

PreparedStatement prepCopy = con
.prepareStatement("INSERT INTO table1(12/30/2016) SELECT 12/30/2016 FROM table2");

But I am getting this error:

near "12": syntax error:

I tried doing this for the other columns like name, accounts, and email and it works. This error only occurs on the date column names. I also tried putting '' before and after the date but then it populates all rows with the date itself. I think I am missing a small detail, but I can't seem to figure it out.

Any suggestion is appreciated. :)

Answer Source

Surround the dates - in those cases in which they are column names - with double quotes or backticks.

INSERT INTO table1("12/30/2016") SELECT "12/30/2016" FROM table2

See also: