简体   繁体   English

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

[英]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.我有 sql 查询,如果我必须在 sql 命令行或 IDE 中手动运行它,它运行良好。 But I have to convert that into java code and the query is not working in java.但是我必须将其转换为 java 代码,并且查询在 java 中不起作用。 Sql query - 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)"

I am trying with below java code我正在尝试使用以下 java 代码

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.请注意,较早的 sql 是为 postgres DB 编写的,而 Java 代码正在连接到 sqllite db。 Please help me out to convert the sql to work with java.请帮助我将 sql 转换为与 java 一起使用。 Thank you!谢谢!

SQLite doesn't support TRUE and FALSE as keywords SQLite 不支持TRUEFALSE作为关键字

SQLite does not have a separate Boolean storage class. SQLite 没有单独的布尔存储类。 Instead, Boolean values are stored as integers 0 (false) and 1 (true).相反,布尔值存储为整数 0 (false) 和 1 (true)。

http://www.sqlite.org/datatype3.html 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将行从选择查询动态插入到Java中的另一个表? - How to insert rows dynamically from a select query to another table in java? 如何从结果查询中插入查询以在Java NetBeans上插入查询? - how to insert query from the result query to insert query on java netbeans? 从Java中的mysql数据库表中检索数据 - retrieving data from mysql database table in Java 从PHP POST到另一个(Java)Web服务并检索(图像)结果 - POST from PHP to another (Java) web service and retrieving the (image) result 将结果SQL查询数据提取到Table JAVA - Fetch result SQL query data to Table JAVA MYSQL插入查询未将数据更新到JAVA中的表 - MYSQL Insert query not updating data to the table in JAVA 根据从另一个表中选择的结果插入表中 - Insert in a table based on result of a select from another table 从 firestore 检索当前用户数据并在另一个查询中使用它 - retrieving the current user data from firestore and use it in another query 使用Java查询从数据库中选择值并将其用作变量并将其插入到另一个表中 - Selecting Values from a Database with a Query in Java and Use them as a Variable and Insert it in another Table 通过NetBeans将另一个表数据插入SQL表数据 - Insert into SQL table data from another table data through NetBeans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM