SQLite Android cannot open database file

后端 未结 5 1231
夕颜
夕颜 2021-01-18 13:34

Edit: I tried this on my phone and it works, can anyone tell me why it does not work on an emulator?

I am trying to open a database on android, but it is throwing an

相关标签:
5条回答
  • 2021-01-18 14:11

    I was looking for a solution to this problem and I didn't find anything that worked... but fortunately I did something that solved it. This is what I did:

    Open your adb shell and remove the database you are trying to open. For those who don't know how to use the Linux terminal:

    // first open adb shell from a command line

    adb shell
    

    // then these are the commands you need (change YOUR.APP.PACKAGE for your app package, it should look like com.domain.apilcation )

    cd /data/data/YOUR.APP.PACKAGE/databases/
    ls
    

    The last command (ls) will list all the databases that you created for your project and now you can remove the database that you can't open. First, you should copy it, so you don't lose any important data. (I will suppose that your database is called your_database.db)

    cp your_database.db your_database.db_backup
    rm your_database.db
    

    Then you should launch your app, it should create a new database and for me this solved the problem permanently.

    I hope this helps!

    0 讨论(0)
  • 2021-01-18 14:20

    Although not relevant for your current issue, the onUpgrade method in SQLiteOpenHelper is also bound to fail in the future: If the action you want to take when a new version of the db is deployed, is to start from scratch, you will need to delete the old tables first.

    Edit After some research I found out that your problem is rather common:

    Android SQLiteOpenHelper cannot open database file

    the 3 links provided with this answer

    https://stackoverflow.com/a/8957994/2177061

    are particularly interesting and provide some kind of workaround, should you not find a clean way to make the bug disappear.

    That said if I where in you, as a first attempt to solve your problem before delving with complex workarounds, I would try to update all Android Development Tools (the SDK in particular, but updating the eclipse plugin won't hurt), completely remove your app from both the emulator and devices, and run it again.

    Good luck!

    Edit 2 Disregard the following remark (it's not correct, I leave it here for reference)

    Your code doesn't work because the SQL query is wrong: you spelled

    integet promary
    

    where it should have been

    integer primary
    

    (besides, it happens to me too that I get crazy with the Android db classes only to find that the problem resides in the SQL queries :) )

    Hope this helps

    0 讨论(0)
  • 2021-01-18 14:24

    try this on your constructor

    public youeConstructor(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            context.openOrCreateDatabase(DATABASE_NAME, context.MODE_PRIVATE, null);
        }
    
    0 讨论(0)
  • 2021-01-18 14:27

    This may be a little late, but hope this helps for whoever gets this problem (since I can't find a definitive solution around).

    I think I know the reason for this cause (at least for my case). Looking in the DDMS --> File Explorer, you'd realize that the Database Folder (/data/data//databases/) does not exist, which is why the application cannot create the database file in that non-existent folder. If you can create a databases folder in some manner, you can avoid this problem.

    Because I'm lazy, I just used the /data/data//files/ folder when I'm in Emulator mode. You can get the files dir using this:

    context.getFilesDir().getPath()
    

    This worked beautifully for me in the Emulator.

    Hope this helps someone.

    In case you want to see some code:

    String dbFilename = "example.db";
    try
    {       
        File databaseFile = getDatabasePath(dbFilename);        
        SQLiteDatabase _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
    } catch (Exception e)
    {
        String databasePath =  getFilesDir().getPath() +  "/" + dbFilename;
        File databaseFile = new File(databasePath); 
        _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
    }
    

    Edit:

    I tried logging into Facebook (my app has FB integration) on the Emulator and /databases folder appeared after that (and persisted). Not sure what happened, but it's possible to create that folder somehow. Something for another expert around here to shed light on.

    0 讨论(0)
  • 2021-01-18 14:27
    Why are you keeping code complex try this
    
    
    public SQLiteDatabase sampleDB;
        public String COLUMN_ID="_id";
        public String COLUMN1="username";
        public String COLUMN2="password";
        public String TABLE_NAME="Androdata";
    
    sampleDB =  this.openOrCreateDatabase(TABLE_NAME, MODE_PRIVATE, null);
    
    private void insertDB() {
            sampleDB.execSQL("INSERT INTO " +
                    TABLE_NAME +
                    " Values ('1','Androviewer','viewer');");   
            System.out.println("Inserted data successfully....");
        }
        private void createDB() {
            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    TABLE_NAME+ "(" + COLUMN_ID
                    + " integer primary key autoincrement, " + COLUMN1
                    + " text not null,"+ COLUMN2
                    + " text not null);");
            System.out.println("Database created successfully....");
        }
    
    0 讨论(0)
提交回复
热议问题