[英]IntelliJ extract data from sqlite database file?
我剛開始使用sqlite,但遇到了一種奇怪的現象(也許只是對我來說)。 當我用Java連接testDB.db文件並進行一個或某些查詢時,數據和表本身消失了。 控制台說SQL錯誤或數據庫丟失,當我在cmd中檢查數據庫文件時,情況確實如此; 文件中沒有數據。 有人可以幫我解決這個基本問題嗎? (我想這僅僅是因為我對此主題缺乏了解,但是我願意接受新的信息)
public class jdbcTest{
public static void main(String[] strg) {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Username\\Documents\\sqlite\\testDB");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30);
//statement.executeUpdate("drop table if exists person");
ResultSet rs = statement.executeQuery("select * from company");
while (rs.next()){
System.out.print("id = "+rs.getInt("id")+" ");
System.out.println("name = "+rs.getString("name"));
}
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
try {
if (connection!=null){
connection.close();
}
}
catch (SQLException e){
System.err.println(e);
}
}
}
}
當打開不存在的數據庫文件時,SQLite會愉快地創建一個新的空文件。
您為getConnection
提供的文件名實際上並不指向您保存的數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.