简体   繁体   中英

Java code to insert data into a table by retrieving the result from another query

I have sql query which works well if I have to run it manually in sql command line or IDE. But I have to convert that into java code and the query is not working in java. Sql query -

"INSERT INTO table_1(column1, column2,column3, column4, column5)
SELECT DISTINCT s.computedmetric_name AS metric_name, s.display_name, FALSE 
AS is_generated,FALSE AS is_manually_marked_for_deletion, FALSE AS 
is_data_marked_for_deletion FROM db_computedmetrics_temp AS s WHERE   
s.computedmetric_name IS NOT NULL AND s.display_name IS NOT NULL AND NOT 
EXISTS (SELECT  1 FROM    db_metrics AS t WHERE   t.metric_name = 
s.computedmetric_name)"

I am trying with below java code

String sql="Above mentioned sql query directly into this string";
Statement stmt = sqliteDBConnection.createStatement();
stmt.executeUpdate(sql);

But while running the code I am getting the exception saying that -

Exception in thread "main" org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such column: FALSE)

Please note the earlier sql was written for postgres DB and Java code is connecting to sqllite db. Please help me out to convert the sql to work with java. Thank you!

SQLite doesn't support TRUE and FALSE as keywords

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

http://www.sqlite.org/datatype3.html

So you have to change your query to something like this

INSERT INTO table_1(column1, column2,column3, column4, column5)
SELECT DISTINCT s.computedmetric_name AS metric_name, s.display_name, 0 
AS is_generated,0 AS is_manually_marked_for_deletion, 0 AS 
is_data_marked_for_deletion FROM db_computedmetrics_temp AS s WHERE   
s.computedmetric_name IS NOT NULL AND s.display_name IS NOT NULL AND NOT 
EXISTS (SELECT  1 FROM    db_metrics AS t WHERE   t.metric_name = 
s.computedmetric_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM