2013-02-21 115 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 
    } 
}