SQL语句适用于SQLiteManager,但不适用于我的程序

时间:2018-05-08 15:36:05

标签: java sqlite javafx sqlitemanager fts5

我正在尝试从现有表中获取相关值并将它们放入虚拟表中,然后我可以搜索并获取所需的条目。

除了通过程序将条目实际加载到此虚拟表中之外,我或多或少都有效。我已经在sqliteManager中尽我所能地测试了这些语句,它们似乎没有问题,但是当我尝试通过程序执行它们时,没有任何反应,stmt.execute()返回false。

有问题的代码:

public void getResults() {

    String sqlGetData = "INSERT INTO VirtualProductTable SELECT * FROM 
    H4HProductTable WHERE id = ? AND price <= ?" + 
         " AND length <= ? AND width <= ? AND height <= ?";

    switch (((Categories)this.categorySelect.getValue()).toString()) {
    case "Furniture":
        sqlGetData = sqlGetData.concat(" AND isFurniture = 1");
        break;
    case "Appliances":
        sqlGetData = sqlGetData.concat(" AND isAppliance = 1");
        break;
    case "Building_Materials":
        sqlGetData = sqlGetData.concat(" AND isBuildingMaterial = 1");
        break;
    case "Tools":
        sqlGetData = sqlGetData.concat(" AND isTool = 1");
        break;
    default:
        break;
    }

    // corrects for empty/unimportant fields //
    try {
        Connection conn = DBConnection.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sqlGetData);

        if(this.idSearch.getText().equals(null) || 
        this.idSearch.getText().isEmpty()) {
            stmt.setString(1, "id");
        } else {
            stmt.setString(1, this.idSearch.getText());
        }

        if(this.priceSearch.getText().equals(null) || 
        this.priceSearch.getText().isEmpty()) {
            stmt.setString(2, "price");
        } else {
            stmt.setString(2, this.priceSearch.getText());
        }

        if(this.lengthSearch.getText().equals(null) || 
        this.lengthSearch.getText().isEmpty()) {
            stmt.setString(3, "length");
        } else {
            stmt.setString(3, this.lengthSearch.getText());
        }

        if(this.widthSearch.getText().equals(null) || 
        this.widthSearch.getText().isEmpty()) {
            stmt.setString(4, "width");
        } else {
            stmt.setString(4, this.widthSearch.getText());
        }

        if(this.heightSearch.getText().equals(null) || 
        this.heightSearch.getText().isEmpty()) {
            stmt.setString(5, "height");
        } else {
            stmt.setString(5, this.heightSearch.getText());
        }

        if(!stmt.execute()) {
            System.out.println("insert statement not executed");
        }
        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

然后由searchProduct()函数调用它:

public void searchProduct (ActionEvent event) throws Exception {
    // makes sure we are not just piling entry after entry every consecutive search //

    String drop = "DELETE FROM VirtualProductTable";
    PreparedStatement stmt = this.newSearchModel.connection.prepareStatement(drop);
    stmt.execute();
    stmt.close();


    // 1. Creates a virtual table and inserts all entries of the category we're looking for, that are also within our other criteria 
    // 2. Sort that table into a list and print out the fields we need row by row 
    getResults();
    showFirstSix();
}

现在,我知道我的程序至少与我的数据库连接,因为&#34; DELETE FROM VirtualProductTable&#34;声明工作正常(我已经通过手动输入相关表格中的项目来测试它。)

我可能认为可能导致此问题的唯一另一件事是我的stmt.setString(...)无法正常工作,但我不知道如何正确测试这些并看到了什么实际上正在采取价值观。

我应该说清楚,我没有从eclipse收到任何实际的错误信息,所以我认为我在语法上是合理的。

0 个答案:

没有答案