Java: Missing Database error

后端 未结 3 800
悲&欢浪女
悲&欢浪女 2020-12-06 07:35

I am getting the following error:

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

In realit

相关标签:
3条回答
  • 2020-12-06 08:11

    May be this can help you to get right path to you database

    How to Specify Database Files

    Here is an example to select a file C:\work\mydatabase.db (in Windows)

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

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

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

    How to Use Memory Databases SQLite supports on-memory database management, which does not create any database files. To use a memory database in your Java code, get the database connection as follows:

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

    This also can help

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

    assumes that apartments.db is put in project root directory

    0 讨论(0)
  • 2020-12-06 08:16

    I had the same problem when running my unit tests from Robolectric on Android platform. The problem turned out to be related to something to do with using a singleton pattern on my DatabaseHelper class which was being reused by all my unit tests as it was a static object created by a base class for all my unit tests.

    In my case the fix was to set the static variable caching the singleton instance inside the DatabaseHelper to null after each test. SO the 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 讨论(0)
  • 2020-12-06 08:20

    Change The Extension .db to .sqlite

    connection = DriverManager.getConnection("jdbc:sqlite:Path\\To\\apartments.sqlite");
    
    0 讨论(0)
提交回复
热议问题