Java Error: Missing Database Error

I get the following error:

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

In fact, the table exists. Below is my code:

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);
}
+5
source share
3 answers

Perhaps this will help you get the right path to your database.

How to specify database files

Here is an example of selecting the file C: \ work \ mydatabase.db (on Windows)

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

UNIX file (Linux, Mac OS X, etc.) / home / leo / work / mydatabase.db

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

How to use memory databases SQLite supports database management on a database that does not create any database files. To use the memory database in your Java code, connect to the database as follows:

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

It may also help.

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

, apartment.db

+7

.db .sqlite

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

Robolectric Android. -, singleton DatabaseHelper, , , .

In my case, the fix was for the static variable to cache the singleton instance inside the DatabaseHelper equal to null after each test. The SO base class looks something like this for me:

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
    }
}
0
source

All Articles