通过从另一个查询中检索结果将数据插入表中的Java代码

时间:2018-02-07 20:11:35

标签: java mysql sqlite

我有sql查询,如果我必须在sql命令行或IDE中手动运行它,效果很好。但我必须将其转换为java代码,并且查询在java中不起作用。 SQL查询 -

"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)"

我正在尝试使用以下java代码

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

但是在运行代码时,我得到的例外是 -

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

请注意,早期的sql是为postgres DB编写的,Java代码正在连接到sqllite db。 请帮我转换sql以使用java。 谢谢!

1 个答案:

答案 0 :(得分:1)

SQLite不支持TRUEFALSE作为关键字

  

SQLite没有单独的布尔存储类。相反,布尔值存储为整数0(假)和1(真)。

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

所以你必须将你的查询改为这样的

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)