java.sql.SQLException : no such column: after accepting sql query

标签 java sql sqlite

我正在开发一个管理项目库存的系统,并试图获取接近到期日期的库存元素列表

这就是表的创建方式:

Query_Statement.executeUpdate("CREATE TABLE STOCK_PURCHASE ( SP_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "USER_ID INTEGER,STOCK_ID INTEGER,SUPPLIER_ID INTEGER,SP_ENTRY_DATE TEXT,"
                    + "SP_PURCHASE_PRICE REAL, SP_SELLBY_DATE TEXT, SP_QUANTITY REAL,"
                    + "FOREIGN KEY (USER_ID) REFERENCES USER (USER_ID),"
                    + "FOREIGN KEY (STOCK_ID) REFERENCES STOCK (STOCK_ID),"
                    + "FOREIGN KEY (SUPPLIER_ID) REFERENCES SUPPLIER (SUPPLIER_ID))");

这是我使用的函数:

public ArrayList<String> GetExpiredStock(){
    Connection dbConnection = null;
    ResultSet list = null;
    ArrayList<String> ls = new ArrayList<String>();

    LocalDate currentDate = LocalDate.now();
    //System.out.println(today);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

    try {
        dbConnection = DriverManager.getConnection("jdbc:sqlite:"+dbName+".db");

        Statement Query_Statement = dbConnection.createStatement();
        Query_Statement.setQueryTimeout(10);

        list = Query_Statement.executeQuery("SELECT SP_ENTRY_DATE, STOCK_ID FROM STOCK_PURCHASE"); //this works

        while (list.next()) {
            try {
            LocalDate expDate = LocalDate.parse(list.getString("SP_SELLBY_DATE"), formatter);
            LocalDate monthAway = expDate.minusMonths(1);
            System.out.println(currentDate);
            if(currentDate.isAfter(monthAway)) {
                int id = list.getInt("STOCK_ID");
                ResultSet ids = Query_Statement.executeQuery("SELECT STOCK_NAME FROM STOCK WHERE STOCK_ID=" + id);

                ls.add(ids.getString("STOCK_NAME") + "\t\t" + 
                        list.getString("SP_SELLBY_DATE") + getStockQuant(list.getInt("STOCK_ID"), 
                                currentDate));

            }
            }catch(SQLException e) {
                System.err.println(e);
                continue;


            }
        }

    } catch (SQLException e) {
        System.err.println(e.getMessage());
    } finally {
        try {
            if (dbConnection != null)
                dbConnection.close();
        } catch (SQLException e) {
            System.err.println(e);
        }
    }

    return ls;
}

我希望它能得到到期日期。但它一直说:

java.sql.SQLException: no such column: 'SP_SELLBY_DATE'

编辑:

我将代码更改为如下所示:

    public ArrayList<String> GetExpiredStock(){
    Connection dbConnection = null;
    ResultSet list = null;
    ArrayList<String> ls = new ArrayList<String>();

    LocalDate currentDate = LocalDate.now();
    //System.out.println(today);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

    try {
        dbConnection = DriverManager.getConnection("jdbc:sqlite:"+dbName+".db");

        Statement Query_Statement = dbConnection.createStatement();
        Query_Statement.setQueryTimeout(10);

        list = Query_Statement.executeQuery("SELECT SP_SELLBY_DATE, STOCK_ID FROM STOCK_PURCHASE"); //this works

        while (list.next()) {
            try {
            String da = list.getString("SP_SELLBY_DATE");
            int id = list.getInt("STOCK_ID");
            System.out.println("Executed on id = " + id);
            LocalDate expDate = LocalDate.parse(da, formatter);
            LocalDate monthAway = expDate.minusMonths(1);
            System.out.println(currentDate);
            if(currentDate.isAfter(monthAway)) {

                ResultSet ids = Query_Statement.executeQuery("SELECT STOCK_NAME FROM STOCK WHERE STOCK_ID=" + id);

                ls.add(ids.getString("STOCK_NAME") + "\t\t" + 
                        da + "\t\t"+  getStockQuant(id, 
                                currentDate));

            }
            }catch(SQLException e) {
                System.err.println(e);
                continue;


            }
        }

    } catch (SQLException e) {
        System.err.println(e.getMessage());
    } finally {
        try {
            if (dbConnection != null)
                dbConnection.close();
        } catch (SQLException e) {
            System.err.println(e);
        }
    }

    return ls;

但第一次迭代后仍然失败

最佳答案

您的 SQL 查询没有所需的列,请添加它:

SELECT SP_ENTRY_DATE, STOCK_ID, SP_SELLBY_DATE FROM STOCK_PURCHASE

关于java.sql.SQLException : no such column: after accepting sql query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56368539/

相关文章:

Java Play 2.4 和 IDEA - View 无法解析

android - 当我尝试在SQLite中运行提交数据的应用程序时,出现错误1语法错误

java - 包修饰符有什么意义?

具有多个参数的 Java 方法 - 性能

php - 在单个查询中连接两个表。需要查询来执行我的 php 代码

SQLite3 - 为什么引用数据不会删除?

ios - 从 sqlite3_prepare_v2 失败。错误是 : out of memory

Android:从 Assets 文件夹复制数据库,但只得到一个空文件

java - 是否可以在 Java 的不同位置创建完全相同的对象?

mysql - 如何搜索具有常量值的最大 id