安装后SQlite数据库插入和删除问题



我使用Eclipse在JavaFx中创建了一个SQLite数据库。创建和访问数据库的函数如下所示。我使用导出选项创建了可运行的jar。我尝试了两种方法:方法1:使用Inno创建安装程序。然后在另一台笔记本电脑上安装应用程序进行测试。已成功安装并启动。问题是没有创建数据库。同时,它在笔记本电脑在Eclipse工作很好。方法2:在创建安装程序时,我将基于Eclipse的文件夹中的数据库文件与应用程序JAR一起包含在Inno中。包含数据库文件的exe安装在另一台笔记本电脑上。安装和应用程序启动成功。问题是显示了数据库中已经存在的表项。但是,没有插入新条目和删除现有条目。项目结构如图所示。

public static Connection getConnection() {      
Connection conn = null;
Statement stmt = null;
try {
String dbName  = "patientdata";             
File file = new File (dbName);
//Class.forName("com.mysql.cj.jdbc.Driver");            
//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,"root","");     
if(file.exists())System.out.println("patient file exists:  "+ file.getAbsolutePath());
else System.out.println("patient file not exist: Creaeted new one ");

try{
Class.forName("org.sqlite.JDBC");
//conn = DriverManager.getConnection("jdbc:sqlite:"+dbName,"root","");
conn = DriverManager.getConnection("jdbc:sqlite:"+dbName);
System.out.println("data base connection established:  "+ conn.toString());

stmt = conn.createStatement();
String pat = "CREATE TABLE if not exists newpatient " +
"(patientId INTEGER NOT NULL," +
" patientName    CHAR(50)    NOT NULL, " + 
" patientAge     INTEGER     NOT NULL, " + 
"patientGender   CHAR(10) NOT NULL,"+
"patientAddress  CHAR(100) NOT NULL,"+
"patientMobile   BIGINT(10) NOT NULL)";
System.out.println("newpatient Table Created:  ");
stmt.executeUpdate(pat);
stmt.close();

stmt = conn.createStatement();
String hist = "CREATE TABLE if not exists history " +
"(id INTEGER NOT NULL," +
" date    DATE    NOT NULL, " + 
" start   TIME     NOT NULL, " +                                               
"stop   TIME NOT NULL)";
System.out.println("history Table Created:  ");    
stmt.executeUpdate(hist);
stmt.close();

Dialog<Void> pop = new Dialog<Void>();
pop.setContentText("Data base accessed");
pop.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
Node closeButton = pop.getDialogPane().lookupButton(ButtonType.CLOSE);
closeButton.setVisible(false);
pop.showAndWait();

}catch(SQLException tb){
System.err.println(tb.getClass().getName() + ": " + tb.getMessage());
Dialog<Void> pop = new Dialog<Void>();
pop.setContentText("Data base  not accessed");
pop.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
Node closeButton = pop.getDialogPane().lookupButton(ButtonType.CLOSE);
closeButton.setVisible(false);
pop.showAndWait();
}
}catch(Exception e)
{
//System.out.println("errors to Create Data Base : "+e.getMessage());
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
return conn;
}

private void insertrecord()
{
try{
String query ="INSERT INTO `newpatient`(patientId,patientName,patientAge,patientGender,patientAddress,patientMobile)"+
"VALUES("+ newpatient_id.getText() +",'" +  newpatient_name.getText() + "','"+  newpatient_age.getText() + "',"
+ "'"+  selectedGender  + "','"+  newpatient_address.getText() + "',"+  newpatient_mobile.getText() +")";
System.out.println(gender);     
executeQuery(query);
System.out.println("Saved");
Main.selected_patient_id = Integer.parseInt(newpatient_id.getText());
}
catch(Exception e) {
System.out.println("Execption in Save");
e.printStackTrace();
}
}
private void executeQuery(String query) {

Connection  conn= getConnection();
Statement st;
try {
st = conn.createStatement();
st.executeUpdate(query);         
}catch(Exception e){
e.printStackTrace(); 
}
}
@FXML
private void btnFetchHistory(ActionEvent event) {                                  
try {                       
Existpatcontrol existpat_controller = new Existpatcontrol();               

FXMLLoader loader1 = new FXMLLoader();
loader1.setController(existpat_controller.getTvHistory());
//URL urlext = loader1.getClass().getClassLoader().getResource("Historypat.fxml");
//Parent root1 = FXMLLoader.load(urlext);
loader1.setLocation(getClass().getResource("Historypat.fxml"));
Parent root1 = loader1.load();


Stage stage1 = new Stage();
stage1.setTitle("History");
stage1.setScene(new Scene(root1));
stage1.show();                 

}catch (Exception hs) {

System.out.println("errors"+hs.getMessage());
try {
PrintStream ps = new PrintStream(Main.test_file);
hs.printStackTrace(ps);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("errors"+e.getMessage());
}               
}            
} 

输入图片描述

在这里输入图像描述输入图片描述

问题解决了。在我的脑海中有一个误解,即jdbc:sqlite:在安装应用程序的同一个文件夹中创建数据库文件,但是安装文件夹是写保护的。我传递了一个固定的路径
jdbc:sqlite:c://appdata/database.db来保存应用程序运行时的数据。