SQLITE_ERROR SQL错误或缺少数据库(表食品没有列名为price)

时间:2017-02-26 21:26:49

标签: java sql sqlite jdbc

我正在尝试制作预算应用程序,用户可以在其中输入食物和各种实用程序等项目。我正在使用Intellij Ultimate。

我根本不理解这个错误。我的意思是,我有点但我不明白为什么说:

System.out.println()

当我尝试从食物表中插入或选择时。我清楚地创造了价格'列。

这是我的代码:

SQLITE_ERROR SQL error or missing database (table food has no column named price)

当我运行它时,我的输出是:

    /**
 * Database class
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Database {
    public static void connect() {
        Connection conn = null;
        try {
            // db parameters
            String url = "jdbc:sqlite:budget.db";
            // create a connection to the database
            conn = DriverManager.getConnection(url);

            System.out.println("Connection to SQLite has been established.");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    public static void insertFood(String name, double quantity, double price) {
        String sql = "INSERT INTO food(name, quantity, price) VALUES(?,?,?)";
        try {
            String url = "jdbc:sqlite:budget.db";
            Connection connection = DriverManager.getConnection(url);
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setDouble(2, quantity);
            pstmt.setDouble(3, price);
            pstmt.executeUpdate();
            System.out.println("Items have been inserted into food table");
        } catch(SQLException e) {
            System.out.println(e.getMessage());
        }
    }
    public static void selectAllFood() {
        String sql = "SELECT name, quantity, price FROM food";

        try {
            String url = "jdbc:sqlite:budget.db";
            Connection connection = DriverManager.getConnection(url);
            Statement stmt = connection.createStatement();
            ResultSet resultSet = stmt.executeQuery(sql);

            while(resultSet.next()) {
                System.out.println(resultSet.getString("name") + "\t" +
                        resultSet.getDouble("quantity") + "\t" +
                        resultSet.getDouble("price"));
            }
        } catch(SQLException e) {
            System.out.println(e.getMessage());
        }
    }
    public static void createNewDatabase(String fileName) {
        String url = "jdbc:sqlite:C:/Users/Jim/IdeaProjects/BudgetPro/" + fileName;

        try (Connection conn = DriverManager.getConnection(url)) {
            if (conn != null) {
                DatabaseMetaData meta = conn.getMetaData();
                System.out.println("The driver name is " + meta.getDriverName());
                System.out.println("A new database has been created.");
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
    public static void createFoodTable() {
        // SQLite connection string
        String url = "jdbc:sqlite:budget.db";

        // SQL statement for creating a new table
        String sql = "CREATE TABLE IF NOT EXISTS food (\n"
                + " id integer PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
                + " name text NOT NULL ,\n"
                + " quantity real,\n"
                + " price real\n"
                + ");";

        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement()) {
            // create a new table
            stmt.execute(sql);
            System.out.println("Food table has been established.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
    public static void createUtilitiesTable() {
        String url = "jdbc:sqlite:budget.db";
        String sql = "CREATE TABLE IF NOT EXISTS utilities (\n"
                + " id integer PRIMARY KEY AUTOINCREMENT NOT NULL ,\n"
                + " name text NOT NULL,\n"
                + " price real\n"
                + ");";

        try (Connection connection = DriverManager.getConnection(url);
             Statement stmt = connection.createStatement()) {
            stmt.execute(sql);
            System.out.println("Utilities table has been established.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}

/**
 * Main class
 */
public class Main {
    public static void main(String[] args) {
        Database.connect();
        Database.createFoodTable();
        Database.createUtilitiesTable();

        Database.insertFood("Pompano", 2, 7.99);
    }
}

1 个答案:

答案 0 :(得分:0)

您可能在没有价格列之前创建了此表。您应该删除该表或将其重命名为food2

如果重命名它可以解决您的问题,请删除表food并重新创建它。

祝你好运......