Failed to open database/ Failed to change locale for (database) to 'en_US'

前端 未结 4 876
误落风尘
误落风尘 2021-01-20 03:36

I have read solution from Failed to change locale for db '/data/data/my.easymedi.controller/databases/EasyMediInfo.db' to 'en_US' but it doesnt help me. I st

相关标签:
4条回答
  • 2021-01-20 03:38

    Probably the database file is actually corrupted. You can try to open it with sqlitebrowser

    Also, you can do some other checks and try to restore it in case it is corrupted, if you install sqlite in your computer. You can follow this steps:

    1. Get sqlite here and install it
    2. Open command a add path to sqlite oyour environnment variable path to allow you to launch sqlite commands everywhere
    3. Go to the directory, where you have the database, and launch SQLite:

      sqlite3 yourfile.db
      
    4. You can check the database integrity by:

      pragma integrity_check;
      
    5. If it says that the file is ok, then we are lost! If it is corrupted, lets try to restore it.

    Sometimes the best solution for a corrupted sqlite3 database is simple yet effective: Dump and Restore

    1. Export your data to an sql file:

      echo .dump | sqlite3.exe yourdbname.db > yourdbname.sql
      
    2. Change file name mv yourdbname.db yourdbname.db.original

    3. Create a new database with your sql.

      sqlite3.exe -init yourdbname.sql yourdbname.db
      
    4. Open the new file with sqlite browser, and see what happens!

    0 讨论(0)
  • 2021-01-20 03:38

    I couldn't put all these codes in the comments, so please try these: Keep in mind that I am not adding all the boilerplates... ok?

    Change checkDb method:

    private boolean checkDataBase(){
         File dbFile = new File( DATABASE_PATH, DATABASE_NAME );
         return dbFile.exists();
    }
    

    Change createDb method:

    public void createDataBase() {
        if(checkDb()){
            //do nothing
        } else {
            copyDatabase();
        }
    }
    

    Change copyDatabase() method:

    private void copyDataBase(){
          getReadableDatabase();
    
          //add the rest of your current implementation
    
    }
    

    Change accessibility of your constructor to private and add these methods:

    private DBHelper mInstance = null;
    public static DBHelper getInstance(Context context) {
       if(mInstance == null)
            mInstance = new DBHelper(context)
       return mInstance;
    }
    //remove the old openDatabase method
    public SQLiteDatabase openDatabase() {
       return getReadableDatabase(); //or you can use getWritableDatabase();
    }
    

    Besides I noticed that your db version in the posted code is 18 while in the logs that you posted it's saying 5 or 6? Can you post the latest logs...

    0 讨论(0)
  • 2021-01-20 03:41

    The problem that you get is that the file cannot be opened. Probably its something wrong with your path (it could be slightly different between diferent android versions), and anyways, you should never hardcode it.

    You can get database path with context.getDatabasePath(); you pass the desired name to the file (no matter if it exists or not). And actually you are supposed to do it that way

    For that, at any place you are using something like String outFileName = DB_PATH + DB_NAME; you should change it for

     String outFileName =myContext.getDatabasePath(DB_NAME).getPath() ;
    

    and that's the real valid location for your file. so , your copyDataBase() will be like this

     private void copyDataBase() throws IOException {
    
        // Open your local db as the input stream
        InputStream myInput = context.getAssets().open("db/" + DATABASE_NAME);
    
        // Path to the just created empty db
         String outFileName =myContext.getDatabasePath(DB_NAME).getPath() ;
    
        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
    
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
    
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    
    }
    

    and the same change in your private boolean checkDataBase()

    0 讨论(0)
  • 2021-01-20 04:00

    try this

    db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);
    
    0 讨论(0)
提交回复
热议问题