Java:缺少数据库错误

时间:2013-02-21 09:30:16

标签: java database sqlite sqlexception

我收到以下错误:

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

3 个答案:

答案 0 :(得分: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放在项目根目录

答案 1 :(得分:1)

将扩展程序.db更改为.sqlite

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

答案 2 :(得分: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
    }
}