I am trying to create 2 MySQL statements that will insert a new row in one column and reference that same row in another, like this:
In case you need to know, I am using Java/com.mysql.jdbc.Driver
insertNewMessage = connection
.prepareStatement(new StringBuilder(128)
.append("INSERT INTO messages (Message,SHA256) VALUES (?,?);")
.append("INSERT INTO messagelog (MESSAGE_ID,SERVER_ID,CLIENT_ID) VALUES ((SELECT LAST_INSERT_ID()),?,?)")
.toString());
So it make it clearer, my SQL statement is:
INSERT INTO messages (Message,SHA256) VALUES (?,?);
INSERT INTO messagelog (MESSAGE_ID,SERVER_ID,CLIENT_ID) VALUES ((SELECT LAST_INSERT_ID()),?,?)
I want to take the new ROWID value created in the messages table and insert it into the messagelog table.
I am finding LAST_INSERT_ID(), mysql_insert_id() both not working. I get the following exception thrown:
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 'INSERT INTO messagelog (MESSAGE_ID,SERVER_ID,CLIENT_ID) VALUES ((SELECT LAST_INS' at line 1
In the documentation for MYSQL, it sounds like Last_Insert_ID should work, but I am guessing there are caveats. I don't know what those caveats are. (Yes A.I is set on the ROWIDs)