首页 > 解决方案 > SQLITE_ERROR SQL 错误或缺少数据库(没有这样的表:naprawy)

问题描述

我有 sqlite3 DB 和 2 个名为:汽车和 naprawy 的表。当我从汽车表中检索数据时,一切都很好。但是,当我尝试从 naprawy 查询数据时,我收到此错误:[SQLITE_ERROR] SQL 错误或缺少数据库(没有这样的表: naprawy)。从 DB Browser 我可以从 naprawy 查询数据,但在应用程序中不断向我抛出异常。

为了连接到数据库和查询数据,我使用这个类:

package CompanyCars.MainWindow.DataSource;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DataSource {

    public static final String TABLE_CARS = "cars";
    public static final String DB_NAME = "companyCars";
    public static final String CARS_COLUMN_NRREJ = "nrrej";
    public static final String CARS_COLUMN_MARKA = "marka";
    public static final String CARS_COLUMN_MODEL = "model";
    public static final String CARS_COLUMN_KOLOR = "kolor";
    public static final String CARS_COLUMN_MIASTO = "miasto";
    public static final String CARS_COLUMN_UZYTKOWNIK = "uzytkownik";
    public static final String CARS_COLUMN_KARTA_PALIWOWA = "kartapaliwowa";
    public static final String CARS_COLUMN_DATA_PRZEGLADU = "dataprzegladu";
    public static final String CARS_COLUMN_OLEJ_PRZEGIEG = "olejprzebieg";
    public static final String CARS_COLUMN_PRZEBIEG_OBECNIE = "przebiegobecnie";
    public static final String CARS_COLUMN_PALIWO= "paliwo";
    public static final String CARS_COLUMN_BUTLA_WAZNOSC = "butlawaznosc";
    public static final String CARS_COLUMN_PRZEBIEG_PRZEGLAD_GAZU = "przebiegprzegladgazu";
    public static final String CARS_COLUMN_DATA_PIERWSZEJ_REJESTRACJI = "datapierwszejrejestracji";
    public static final String CARS_COLUMN_UBEZPIECZYCIEL = "ubezpieczyciel";
    public static final String CARS_COLUMN_OCDATA = "ocdata";
    public static final String CARS_COLUMN_OCFIRMA = "ocfirma";
    public static final String CARS_COLUMN_ACFIRMA = "acfirma";
    public static final String CARS_COLUMN_AUTOPOMOC = "autopomoc";
    public static final String CARS_COLUMN_VAT_1 = "vat1";
    public static final String CARS_COLUMN_OPONY_LETNIE = "oponyletnie";
    public static final String CARS_COLUMN_OPONY_ZIMOWE = "oponyzimowe";
    public static final String CARS_COLUMN_OPONY_LOKALIZACJA = "oponylokalizacja";
    public static final String CARS_COLUMN_WYPOSAZENIE = "wyposazenie";
    public static final String CARS_COLUMN_INNE = "inne";
    public static final String CARS_COLUMN_KSYWKA = "ksywka";
    public static final String QUERY_ALL_CARS = "select * from cars" ;


    public static final String UPDATE_ITEM = "UPDATE " + TABLE_CARS + " SET ";
    public static final String INSERT_INTO_CAR = "INSERT INTO " + TABLE_CARS + " VALUES (";



    public static final String TABLE_NAPRAWY = "naprawy";
    public static final String NAPRAWY_COLUMN_IDNAPRAWY = "IDnaprawy";
    public static final String NAPRAWY_COLUMN_DATA = "data";
    public static final String NAPRAWY_COLUMN_MECHANIK = "mechanik";
    public static final String NAPRAWY_COLUMN_CO = "co";
    public static final String NAPRAWY_COLUMN_NETTO = "netto";
    public static final String NAPRAWY_COLUMN_BRUTTO = "brutto";
    public static final String QUERY_ALL_REPAIRS = "SELECT * FROM \"" + TABLE_NAPRAWY + "\" WHERE nrrej =\"";
    public static final String NAPRAWY_COLUMN_NRREJ = "nrrej";


    List<Car> carList = new ArrayList<>();

    public static final String CONNECTION_STRING = "jdbc:sqlite:" + DB_NAME;
    private Connection connection;

    private static DataSource instance = new DataSource();


    private DataSource() {
    }

    public static DataSource getInstance(){
        return instance;
    }

    public boolean open(){
        try {
            connection = DriverManager.getConnection(CONNECTION_STRING);
            return true;
        } catch (SQLException e){
            System.out.println("Nie można połączyć się z bazą danych");
            return false;
        }
    }


    public void close(){
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e){
            System.out.println("Wystąpił błąd");
        }
    }


    //That method works fine
    public ObservableList<Car> queryAllCars(){

        try (Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(QUERY_ALL_CARS)){

            while(resultSet.next()){

                Car car = new Car();

                car.setNrRej(resultSet.getString(CARS_COLUMN_NRREJ));
                car.setMarka(resultSet.getString(CARS_COLUMN_MARKA));
                car.setModel(resultSet.getString(CARS_COLUMN_MODEL));
                car.setKolor(resultSet.getString(CARS_COLUMN_KOLOR));
                car.setMiasto(resultSet.getString(CARS_COLUMN_MIASTO));
                car.setUzytkownik(resultSet.getString(CARS_COLUMN_UZYTKOWNIK));
                car.setKartaPaliwowa((resultSet.getInt(CARS_COLUMN_KARTA_PALIWOWA)==1));
                car.setDataPrzegladu(resultSet.getString(CARS_COLUMN_DATA_PRZEGLADU));
                car.setOlejPrzebieg(resultSet.getInt(CARS_COLUMN_OLEJ_PRZEGIEG));
                car.setPrzebiegObecnie(resultSet.getInt(CARS_COLUMN_PRZEBIEG_OBECNIE));
                car.setPaliwo(resultSet.getString(CARS_COLUMN_PALIWO));
                car.setButlaWaznosc(resultSet.getString(CARS_COLUMN_BUTLA_WAZNOSC));
                car.setPrzebiegPrzegladGazu(resultSet.getInt(CARS_COLUMN_PRZEBIEG_PRZEGLAD_GAZU));
                car.setDataPierwszejRejestracji(resultSet.getString(CARS_COLUMN_DATA_PIERWSZEJ_REJESTRACJI));
                car.setUbezpieczyciel(resultSet.getString(CARS_COLUMN_UBEZPIECZYCIEL));
                car.setOCData(resultSet.getString(CARS_COLUMN_OCDATA));
                car.setOCFirma(resultSet.getString(CARS_COLUMN_OCFIRMA));
                car.setACFirma(resultSet.getString(CARS_COLUMN_ACFIRMA));
                car.setAutoPomoc(resultSet.getString(CARS_COLUMN_AUTOPOMOC));
                car.setVAT_1((resultSet.getInt(CARS_COLUMN_VAT_1)==1));
                car.setOponyLetnie(resultSet.getString(CARS_COLUMN_OPONY_LETNIE));
                car.setOponyZimowe(resultSet.getString(CARS_COLUMN_OPONY_ZIMOWE));
                car.setLokalizacjaOpon(resultSet.getString(CARS_COLUMN_OPONY_LOKALIZACJA));
                car.setWyposazenie(resultSet.getString(CARS_COLUMN_WYPOSAZENIE));
                car.setInne(resultSet.getString(CARS_COLUMN_INNE));
                car.setKsywka(resultSet.getString(CARS_COLUMN_KSYWKA));

                carList.add(car);
            }

            return FXCollections.observableArrayList(carList);


        } catch (SQLException e){
            System.out.println("Blad queryAllCars");
            return null;
        }

    }


    //This method keeps throwing exception, but the above method works fine

    public ObservableList<Repair> queryAllRepairs(String nrRej){

        try(Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * FROM \"naprawy\"")){

            List<Repair> repairs= new ArrayList<>();

            while(resultSet.next()){
                Repair repair = new Repair();
                repair.setNrRej(resultSet.getString(NAPRAWY_COLUMN_NRREJ));
                repair.setIDnaprawy(resultSet.getInt(NAPRAWY_COLUMN_IDNAPRAWY));
                repair.setData(resultSet.getString(NAPRAWY_COLUMN_DATA));
                repair.setMechanik(resultSet.getString(NAPRAWY_COLUMN_MECHANIK));
                repair.setCo(resultSet.getString(NAPRAWY_COLUMN_CO));
                repair.setNetto(resultSet.getDouble(NAPRAWY_COLUMN_NETTO));
                repair.setBrutto(resultSet.getDouble(NAPRAWY_COLUMN_BRUTTO));
                repairs.add(repair);
            }
            return FXCollections.observableArrayList(repairs);

        }catch (SQLException e){
            System.out.println("Nie udało się pobrać napraw samachodu: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}

应用程序与数据库正确连接,因为我有其他方法正在更新、插入和删除数据库中的数据并且它们工作正常。我正在使用单例类 DataSource。

标签: javajavafx

解决方案


推荐阅读