如何在SQL查询中从VIEWS中读取表

问题描述 投票:-1回答:1

我想从数据库的视图中读取此表SCHACHT_V的三列,即“名称,xcoordinate,ycoordinate”,如下图所示来自数据库视图的表

我使用的代码如下

/**
 *
 * @author liu
 */
public class SelectApp {

    /**
     * @param args the command line arguments
     *
     */

        private Connection connect() throws ClassNotFoundException, SQLException {
            // SQLite connection string
            Class.forName("org.sqlite.JDBC");
            File file = new File("F:\\LHU\\ISU\\Masterarbeit\\Daten Ricklingen\\Model-Extr_BLD3V1,2_EXT.idbr");
            SQLiteConfig config = new SQLiteConfig();
            config.setEncoding(SQLiteConfig.Encoding.UTF8);
            Connection conn = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath(), config.toProperties());

            return conn;
        }

        /**
         * select all rows in the warehouses table
         */
        public void selectAll() {

            String sql = "SELECT name, xcoordinate, ycoordinate FROM SCHACHT_V";

        try (Connection conn = this.connect();
             Statement stmt  = conn.createStatement();
             ResultSet rs   = stmt.executeQuery(sql)){

                // loop through the result set

                while(rs.next()) {

                        System.out.println(rs.getString("name") + "\t" + rs.getDouble("xcoordinate") + "\t" + rs.getDouble("ycoordinate") + "\t");

                }
            }catch (SQLException e) {
                System.out.println(e.getMessage());

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SelectApp.class.getName()).log(Level.SEVERE, null, ex);
            }

        }



        /**
         * @param args the command line arguments
         */

        public static void main(String[] args) {

            SelectApp app = new SelectApp();
            app.selectAll();

        }

}

但是它总是显示错误:

“ [SQLITE_ERROR] SQL错误或缺少数据库(无此功能:X)”。

我被这个问题困扰了这么长时间。有人可以帮我吗?

java sqlite view
1个回答
0
投票
我尝试了您的代码,供我查看。从您共享的屏幕快照中,我可以看到您在Java代码中传递的列名是错误的。

请尝试使用

XKOORDINATE(如屏幕截图中所示)而不是xcoordinate和YKOORDINATE(如屏幕截图中所示)而不是ycoordinate。

列名错误的代码

String sql = "SELECT name, xcoordinate, ycoordinate FROM SCHACHT_V"; //1 while(rs.next()) { //2 System.out.println(rs.getString("name") + "\t" + rs.getDouble("xcoordinate") + "\t" + rs.getDouble("ycoordinate") + "\t"); }

© www.soinside.com 2019 - 2024. All rights reserved.