6

我收到以下错误:

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);
}
4

3 回答 3

8

可能这可以帮助您找到正确的数据库路径

如何指定数据库文件

这是选择文件C:\work\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 放在项目根目录中

于 2013-02-21T10:16:39.527 回答
1

将扩展名更改 .db.sqlite

connection = DriverManager.getConnection("jdbc:sqlite:Path\\To\\apartments.sqlite");
于 2013-02-21T10:16:36.057 回答
0

在 Android 平台上从 Robolectric 运行单元测试时,我遇到了同样的问题。这个问题原来与在我的 DatabaseHelper 类上使用单例模式有关,我的所有单元测试都在重用它,因为它是由基类为我的所有单元测试创​​建的静态对象。

在我的情况下,解决方法是在每次测试后将缓存 DatabaseHelper 中的单例实例的静态变量设置为 null。所以基类对我来说看起来像这样:

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
    }
}
于 2014-05-30T11:53:51.670 回答