1

I'm trying to add a column to my table but I'm getting an error saying my syntax is wrong. I'm stuck, any ideas on correct syntax at line 2 on here?

String myColumnType = "DECIMAL";
st.executeUpdate("ALTER TABLE stocks.stockvalue ADD " + myColumnName + " " + myColumnType);
String updateTableSQL = ("UPDATE stocks.stockvalue SET stockvalue." + myColumnName + " = ? WHERE stockvalue.stockvalue_id = " + count);
PreparedStatement preparedStatement = conn.prepareStatement(updateTableSQL);
preparedStatement.setDouble(2, stockV);
preparedStatement.executeUpdate();
System.out.println("Done");
preparedStatement.close();
conn.close();
halfer
  • 19,824
  • 17
  • 99
  • 186
Zack
  • 330
  • 3
  • 11
  • 2
    **Post the error message.** And shouldn't it be `ALTER TABLE foo ADD COLUMN`? – chrylis -cautiouslyoptimistic- Dec 13 '16 at 03:53
  • Next time check the [documentation](http://dev.mysql.com/doc/refman/5.7/en/alter-table.html). – shmosel Dec 13 '16 at 03:53
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12/12/2016 DECIMAL' at line 1 – Zack Dec 13 '16 at 03:55
  • I made it ALTER TABLE stocks.stockvalue ADD COLUMN and still have the error. – Zack Dec 13 '16 at 03:56
  • this http://stackoverflow.com/questions/16113570/how-to-add-new-column-to-mysql-table?rq=1 can help – Viet Dec 13 '16 at 03:57
  • 2
    @Zack Welcome to StackOverflow. This is a Q&A site, not a forum. Please click the green mark on the left side of the answer that solved your question, that will mark it as the accepted solution. We do that instead of editing the title to add "solved", which is usual in forum format. – iled Dec 13 '16 at 05:18
  • Honestly, why would you make another column? If that is to solve one to many realation, you should make another table instead – Christian Moen Dec 15 '16 at 23:34

1 Answers1

1

The syntax for adding column to existing table is

ALTER TABLE table_name
  ADD column_name column-definition;

For example ,

"ALTER TABLE stockvalue ADD " + myColumnName + " " + myColumnType

I think you missed word TABLE in statement. Try it. Hope this help you..

Programmer
  • 878
  • 2
  • 11
  • 24
  • Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12/12/2016 DECIMAL' at line 1 – Zack Dec 13 '16 at 04:04
  • I figured out it's probably the table name, can I not have the date as the table name? Such as "12/12/16"? – Zack Dec 13 '16 at 04:08
  • Yes I guess. Try changing column name to some dummy name. – Programmer Dec 13 '16 at 04:11
  • Yeah, thanks, I had to change the date from 12/12/16 to 12_12_16. Thank you :) . – Zack Dec 13 '16 at 04:19
  • Hey if your problem got solved then please accept my answer.. thanks – Programmer Dec 13 '16 at 14:09
  • @Zack: please accept this answer, by clicking on the tick icon to the left. This is how we thank people on Stack Overflow, and how we mark questions as resolved. Thanks! – halfer Dec 15 '16 at 23:31