I want to use Mathematica to do some SQL tasks like insert. However, I have difficulty in inserting date into SQL. Here is an example.
conn = OpenSQLConnection[
JDBC["MySQL(Connector/J)", "localhost:3306/dbTest"],
"Name" -> "dbTest", "Username" -> "mathematica",
"Password" -> "password"]
SQLInsert[conn, SQLTable["match"], {"id", "time", "home_id"}, {1, Now, 100}]
I got the following error:
JDBC::error: 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 'match (id,season,league_id,time,home_id,away_id,fthg,ftag,hthg,htag) VALUES (1,2' at line 1
I think it's probably because of the date object. I also converted the DateObject
into DateString
, the same happens.
SQLInsert[conn, SQLTable["match"], {"id", "time", "home_id"},
{1, DateString[Now, {"ISODate", " ", "Time", ".", "Millisecond", "000"}], 100}]
JDBC::error: 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 'match (id,season,league_id,time,home_id,away_id,fthg,ftag,hthg,htag) VALUES (1,2' at line 1
Any ideas?
DateString
. It returns something like"2019-02-01 10:00:35.952000"
Are all of those characters legal in your table? $\endgroup$DateString
. As it turns out, theDateObject
was not the only problem. See my own answer below. $\endgroup$DateObject
s should be possible, as long as the column type allows for it. I've definitely done that in the past. It might depend on the database and the connector, though. $\endgroup$