2013-02-21 60 views
5

我收到以下錯誤:的Java:缺少數據庫錯誤

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: apartments) 

在現實中,該表確實存在。下面是我的代碼:

try { 
    // load the sqlite-JDBC driver using the current class loader 
    Class.forName("org.sqlite.JDBC"); 
    Connection connection = null; 
    // create a database connection 
    connection = DriverManager.getConnection("jdbc:sqlite:/Path/To/apartments.db"); 
    System.out.println(connection.getMetaData()); 
    Statement statement = connection.createStatement(); 
    statement.setQueryTimeout(30); 

    ResultSet rs = statement.executeQuery("select * from apartments"); 
    while(rs.next()) { 
     // read the result set 
     System.out.println("TEXT = " + rs.getString("display_text")); 
     System.out.println("X1 = " + rs.getInt("x1")); 
    } 
} catch (Exception ex) { 
    Logger.getLogger(MouseEventDemo.class.getName()).log(Level.SEVERE, null, ex); 
} 
+2

它說,* SQL錯誤或丟失的數據庫*,我想這不是問題表如何 – Volatil3 2013-02-21 09:32:57

+0

檢查它不會是一個路徑問題? – Volatil3 2013-02-21 09:34:57

+0

我的事情是你的數據庫名稱與表名混淆。你的數據庫名稱是公寓和餐桌的名字也是一樣的? – 2013-02-21 09:35:30

回答

7

可能這可以幫助你得到正確的道路,你的數據庫

如何指定數據庫文件

以下是選擇文件的示例C:\ w ORK \ mydatabase.db(在Windows)

Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db"); 

一個UNIX(在Linux,Mac OS X,等等)文件/home/leo/work/mydatabase.db

Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db"); 

如何使用內存數據庫 SQLite支持內存數據庫管理,它不創建任何數據庫文件。要在Java代碼中使用內存數據庫,獲得數據庫連接,如下所示:

Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:"); 

這也可以幫助

String path=this.getClass().getResource("apartments.db").getPath(); 
      connection = DriverManager.getConnection("jdbc:sqlite:"+path); 

假定apartments.db放在項目的根目錄

+0

我試過這個:** getConnection(「jdbc:sqlite:」+ getClass()。getResource(「apartments.db」)); **現在它給出錯誤: *到'file:/path/to/apartments.db'的路徑:'/ paht/to/file:'不存在*我不明白爲什麼它最後會添加** file **字符串。 – Volatil3 2013-02-21 10:24:34

+0

Connection connection = DriverManager.getConnection(「jdbc:sqlite:/home/leo/work/mydatabase.db」);不適合你?然後嘗試將數據庫複製並粘貼到項目根目錄中,並使用getConnection(「jdbc:sqlite:」+ getClass()。getResource(「apartments.db」)); – 2013-02-21 10:44:03

+0

* build/class *中的looling。看到這一點:*值java.sql.SQLException:路徑 '文件:/xxxx/xx/demo/build/classes/events/apartments.db': '/ XXXX/XX /演示/文件:' 不存在* – Volatil3 2013-02-21 10:50:45

1

改變擴展.db.sqlite

connection = DriverManager.getConnection("jdbc:sqlite:Path\\To\\apartments.sqlite"); 
+0

我已經明確地將.db作爲文件名。改變並沒有區別。 – Volatil3 2013-02-21 10:58:19

+0

謝謝!最後解決了我一直試圖解決的問題 – CrazedCoder 2017-10-20 14:39:02

0

在Android平臺上運行Robolectric的單元測試時,我遇到了同樣的問題。這個問題與我在我的DatabaseHelper類中使用單例模式有關,這些單例模式被我的所有單元測試重用,因爲它是一個靜態對象,由所有單元測試的基類創建。

在我的情況下,修復的方法是在每次測試後將靜態變量緩存在DatabaseHelper內部的單例實例中。所以基類看起來像這樣對我說:

import com.foo.app.HomeActivity; 
import com.foo.contentProvider.DatabaseHelper; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.runner.RunWith; 
import org.robolectric.Robolectric; 
import org.robolectric.RobolectricTestRunner; 
import org.robolectric.annotation.Config; 

import static org.junit.Assert.assertNotNull; 

@Config(shadows = {ShadowCaseSensitiveSQLiteCursor.class}) 
@RunWith(RobolectricTestRunner.class) // <== REQUIRED for Robolectric! 
public class RobolectricTestBase { 

    protected HomeActivity activity; 
    protected Context context; 
    protected DatabaseHelper databaseHelper; 

    @BeforeClass 
    public static void setupClass() { 
     // To redirect Robolectric to stdout 
     System.setProperty("robolectric.logging", "stdout"); 
    } 

    @Before 
    public void setup() { 
     activity = (HomeActivity) Robolectric.buildActivity(HomeActivity.class).create().get(); 
     assertNotNull(activity); 
     context = activity.getApplicationContext(); 
     assertNotNull(context); 
     databaseHelper = DatabaseHelper.getInstance(context); 
     assertNotNull(databaseHelper); 
    } 

    @After 
    public void tearDown() { 
     databaseHelper.close(); //This is where singleton INSTANCE var is set to null 
    } 
}