I have a Mathematica batch script which makes some MySQL queries using DatabaseLink to store parameters of the run in a database. Recently, I had a run where these queries failed, and there was no output to indicate why. In the past, when I was using the SQLite interface, I had much success debugging (or at least recovering from errors) by printing out every SQL query made to the log file, but with functions like SQLUpdate and SQLInsert I don't get access to the raw queries. Is there a way I can get DatabaseLink to print them out for me, or at least get access to the raw query so I can print it myself?
1 Answer
If you have full control over the MySQL database I think it lets you log every SQL statement from every client (query-log), which probably is the most simple way to get that information. You could also try to look at or even manipulate the sources, it looks like the relevant code is delivered as clear text in the following file in the Mathematica directory: SystemFiles/Links/DatabaseLink/Kernel/SQL.m. It isn't an easy read though and some of the relevant stuff might be buried in one of the java classes, but there seem to be sources also for those, if you really want to dig deep...
-
1$\begingroup$ +1. I am not totally sure, but I am afraid that DatabaseLink implementation (Java part) might use things like
PreparedStatementand the like, in which case, there isn't a point in the code where you can intercept the full query. This does not invalidate your query-log suggestion though. $\endgroup$Leonid Shifrin– Leonid Shifrin2012-04-18 21:12:56 +00:00Commented Apr 18, 2012 at 21:12 -
$\begingroup$ @Leonid That's a very good point that I hadn't considered. Fortunately it appears that, as a practical matter,
PreparedStatementisn't used in the current version of DatabaseLink, except for postprocessing queries passed in from the Mathematica side. Thus this suggestion (and my implementation given in my answer) should be valid. $\endgroup$Oleksandr R.– Oleksandr R.2012-04-18 21:25:55 +00:00Commented Apr 18, 2012 at 21:25 -
$\begingroup$ @Oleksandr R. Sorry to disappoint you, but it is. Have a look at the class
SQLStatementProcessor.java(which you mentioned), and you will seePreparedStatementused and populated there. This is logical, because not using it in general is considered a bad style in Java (JDBC), and often leads to overhead and errors. $\endgroup$Leonid Shifrin– Leonid Shifrin2012-04-18 21:32:00 +00:00Commented Apr 18, 2012 at 21:32 -
$\begingroup$ @Oleksandr R. Yes, but post-processing is non-trivial, becuase queries sent to
PreparedStatementare not complete - they have question marks in places where actual parameters are inserted (and those inserted parameters are often "dressed" in additional quotation marks or otherwise processed). So, in summary, you don't get a full query this way, which you could then execute stand-alone, e.g. through the DB administration UI or command-line. I've worked with JDBC quite a bit in the past, was bitten by this before :) $\endgroup$Leonid Shifrin– Leonid Shifrin2012-04-18 21:35:34 +00:00Commented Apr 18, 2012 at 21:35 -
3$\begingroup$ @Oleksandr R. You may be better off not being too familiar with this. I did (and still do) use Java professionally and it is very useful at times (mostly because so many Java libraries are written literally for anything), but I am not fond of it as a language, and IMO it kills the brain if you start using it exclusively and for too long (no intention to start a flame war with anyone reading this, just a personal opinion). $\endgroup$Leonid Shifrin– Leonid Shifrin2012-04-18 21:42:18 +00:00Commented Apr 18, 2012 at 21:42
SQLExecutein this particular case. $\endgroup$SQLExecute, as you suggested. $\endgroup$