【问题标题】:Java: Missing Database errorJava:缺少数据库错误
【发布时间】:2013-02-21 09:30:15
【问题描述】:

我收到以下错误:

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

【问题讨论】:

  • 它说 SQL error or missing database ,我猜这不是表问题
  • 如何检查不会是路径问题?
  • 我认为您将数据库名称与表名称混淆了。您的数据库名称是公寓和桌子的名称也一样吗?
  • 我认为是路径问题。尝试在getConnection()中给出完整路径
  • @FestusTamakloe 我的数据库名称是 main,我找不到提及数据库名称的方法。这是 SQLite

标签: java database sqlite sqlexception


【解决方案1】:

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

如何指定数据库文件

这是一个选择文件的示例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 放在项目根目录中

【讨论】:

  • 我试过这个:getConnection("jdbc:sqlite:"+getClass().getResource("apartments.db")); 现在它给出了错误: 'file:/path/to/apartments.db' 的路径:'/paht/to/file:' 不存在 我不明白为什么要在其中添加 file 字符串结束。
  • 连接连接 = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");不适合你?然后尝试将您的数据库复制并粘贴到您的项目根目录中并使用 getConnection("jdbc:sqlite:"+getClass().getResource("apartments.db"));
  • 它在 build/class 中闲逛。看到这个:java.sql.SQLException:'file:/xxxx/xx/demo/build/classes/events/apartments.db'的路径:'/xxxx/xx/demo/file:'不存在
  • 我已将文件放在每个子文件夹和根文件夹中,可以从某处挑选但没有:)
  • 如果你把它放在根目录下然后尝试调用这个 getConnection("jdbc:sqlite:"+getClass().getResource("apartments.db"));完全没有别的
【解决方案2】:

将扩展名.db 更改为.sqlite

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

【讨论】:

  • 我已明确将 .db 赋予文件名。改变并没有什么不同。
  • 谢谢!终于解决了我试图解决几个小时的问题
【解决方案3】:

我在 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-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多