SQL: how to make a query query independent of the database

I need to concatenate two fields in an SQL statement, and I use the same statements for MySQL and SQLite in a Java application using JDBC.

It turns out that both MySQL and SQLite have different and incompatible concat statements:

  • MySQL uses "CONCAT (smth, smth)"

  • SQLite uses "smth || smth".

So far I have managed to use the same sentence for both databases, so now I need either a way to find the concat statement independent of MySQL and SQLite; or detect dynamically in my Java application if the statement is executed in MySQL or SQLite.

I can also add a parameter to my Java method, the one that contains the SQL query, to find out if I'm using MySQL or SQLite, but I don't really like this last solution.

Do you have any ideas on how to create an independent concat statement? or any other idea whatsoever?

Thanks.

+4
source share
1 answer

You may be able to do this using the PIPES_AS_CONCAT mode on mysql side to do || behave the same as in sqlite. See also in this article for more information.

+2
source

Source: https://habr.com/ru/post/1498151/


All Articles