SQLite查询未在Java中运行
我使用DB Browser for SQLite创建了一个数据库。现在我试图运行查询从数据库检索数据,但遇到了一个问题。在DB Browser中运行查询时,查询运行平稳。当我尝试在Java中运行相同的查询时,会收到一条错误消息。我对查询字符串进行了三次检查。我试图确保我没有错误地使用SQLite查询未在Java中运行,java,sqlite,Java,Sqlite,我使用DB Browser for SQLite创建了一个数据库。现在我试图运行查询从数据库检索数据,但遇到了一个问题。在DB Browser中运行查询时,查询运行平稳。当我尝试在Java中运行相同的查询时,会收到一条错误消息。我对查询字符串进行了三次检查。我试图确保我没有错误地使用SQLite关键字。为什么在运行Java查询时会出现错误 SELECT subject.id, main.name, main.link, subject.name, main_subject.weight
SQLite
关键字。为什么在运行Java查询时会出现错误
SELECT
subject.id, main.name, main.link, subject.name, main_subject.weight
FROM
main_subject
LEFT JOIN
main ON main_subject.mainId = main.id
LEFT JOIN
subject ON main_subject.subjectId = subject.id
查询在数据库浏览器中正确运行。
MCVE:
Main
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author sedrick
*/
public class Main
{
public static void main(String[] args)
{
try (DatabaseHandler databaseHandler = new DatabaseHandler()) {
List<BySubjectItemTwo> bySubjectItemTwos = databaseHandler.getBySubjectItems();
bySubjectItemTwos.forEach(System.out::println);
//databaseHandler.getByTitleItems().forEach(System.out::println);
}
catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
错误
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author sedrick
*/
public class DatabaseHandler implements AutoCloseable
{
String dbString = "jdbc:sqlite:qDatabase.sqlite3";
private Connection conn;
public DatabaseHandler()
{
try {
conn = DriverManager.getConnection(dbString);
System.out.println("Connected to qDatabase!");
}
catch (SQLException ex) {
Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void close() throws Exception
{
conn.close();
}
public List<BySubjectItemTwo> getBySubjectItems()
{
List<BySubjectItemTwo> returnList = new ArrayList();
String sqlString = "SELECT subject.id, main.name, main.link, subject.name, main_subject.weight FROM main_subject LEFT JOIN main ON main_subject.mainId = main.id LEFT JOIN subject ON main_subject.subjectId = subject.id";
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlString)) {
while (rs.next()) {
System.out.println(rs.getInt("subject.id") + " - " + rs.getString("main.name") + " - " + rs.getString("main.link") + " - " + rs.getString("subject.name") + " - " + rs.getInt("main_subject.weight"));
//returnList.add(new BySubjectItemTwo(rs.getInt("subject.id"), rs.getString("main.name"), rs.getString("main.link"), rs.getString("subject.name"), rs.getInt("main_subject.weight")));
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
return returnList;
}
}
----------< sed.work:CreateDatabaseByTitleAndSubjectDatabase >----------
Building CreateDatabaseByTitleAndSubjectDatabase 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ CreateDatabaseByTitleAndSubjectDatabase ---
Connected to qDatabase!
[SQLITE_ERROR] SQL error or missing database (ambiguous column name: subject.id)
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.484 s
Finished at: 2020-10-16T12:42:35-05:00
------------------------------------------------------------------------
这:
您的查询有问题。从您的声明中删除
、main、subject
。表
main
和subject
在LEFT
联接后正确联接。当您在
FROM
子句中也使用它们时,这将创建额外的引用并交叉连接到相同的表。
此外,尽管SQLite允许查询返回多个同名列,但最好使用别名列,以便在结果集中所有列名都不同。
因此,为main.name
和subject.name
指定别名,如下所示:
SELECT subject.id, main.name AS main_name, ..., subject.name AS subject_name, ...
检索列值时,不要使用表前缀:
System.out.println(rs.getInt("id") + " - " + rs.getString("main_name") + " - " + rs.getString("link") + " - " + rs.getString("subject_name") + " - " + rs.getInt("weight"));
你好,福帕斯。抱歉,我更新了字符串。我尝试了不同的内容,得到了不正确的字符串。@Sedrick如果在SQLite的DB Browser中按编辑前的方式运行查询:FROM main\u subject,main,subject
,您将得到错误:不明确的列名:subject.id
,就像您收到的错误消息一样。因此,产生此错误的实际代码不是您当前问题中的代码。我还直接键入了字符串。我还清理并构建了我的项目。@Sedrick,正如我所说,此查询不会引发此错误,但前一个查询会引发此错误。Hello forpas,String sqlString=“选择subject.id、main.name、main.link、subject.name、main_subject.weight FROM main_subject LEFT JOIN main_subject.mainId=main.id LEFT JOIN subject ON main_subject.subject=subject.id”;
是我当前使用的字符串。它给出了错误信息。
FROM main_subject, main, subject
SELECT subject.id, main.name AS main_name, ..., subject.name AS subject_name, ...
System.out.println(rs.getInt("id") + " - " + rs.getString("main_name") + " - " + rs.getString("link") + " - " + rs.getString("subject_name") + " - " + rs.getInt("weight"));