0

J'utilise une base de données externe dans l'application Android et il fonctionne très bien sur tous les émulateurs et sur les appareils réels samsung. Mais quand je vérifie sur le téléphone intelligent d'Acer, mon application allant se briser avec l'exception suivante:Problème "pas une telle table" dans android sur certains appareils

android.database.sqlite.SQLiteException: no such table: 

C'est étrange pour moi. J'ai vérifié sous le dossier de données dans l'explorateur de dossier où mes bases de données et toutes les tables sont présentes.

Je ne comprends pas pourquoi cela se passe.

Veuillez me guider. Merci à l'avance

Code de classe d'aide sql est comme suit:

public class MyDatabaseHelper extends SQLiteOpenHelper { 

    // System path of application database. 
    private static String DB_PATH = MyApplication.getAppContext() 
      .getFilesDir().getParentFile().getPath() 
      + "/databases/"; 
    private static String DB_NAME = "myDB"; 
    private SQLiteDatabase myDataBase; 
    private final Context myContext; 

    /** 
    * Constructor Takes and keeps a reference of the passed context in order to 
    * access to the application assets and resources. 
    * 
    * @param context 
    */ 
    public MyDatabaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

    /** 
    * Creates a empty database on the system and rewrites it with own database. 
    * 
    */ 
    public void createDataBase() throws IOException { 

     checkDataBase(); 
     // Creates empty database default system path 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 

    /** 
    * Checks if the database already exist to avoid re-copying the file each 
    * time whenever the application opened. 
    * 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDataBase() { 

     SQLiteDatabase checkDB = null; 
     try { 
      String myPath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY); 
     } catch (SQLiteException e) { 
      e.printStackTrace(); 
     } 

     if (checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 

    /** 
    * Copies database from local assets-folder to the just created empty 
    * database in the system folder and from where it can be accessed and 
    * handled using byte stream transferring. 
    * 
    */ 
    private void copyDataBase() throws IOException { 

     // Open local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME + ".db"); 

     // Path of the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 

     // Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 

     // transfer bytes from the input file to the output file 
     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(); 
    } 

    public SQLiteDatabase openDataBase() throws SQLException { 

     // Opens the database 
     String myPath = DB_PATH + DB_NAME; 
     return SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } 

    @Override 
    public synchronized void close() { 
     if (myDataBase != null) 
      myDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 
    } 
} 
+0

ajoutez ici la classe auxiliaire Java. – Sajmon

+0

Utilisez-vous le chemin db par défaut? – Abx

Répondre

1

Enfin j'ai résolu ma question.

Je viens d'obtenir la base de données lisible dans la base de données et fermez-le juste après l'avoir ouvert à nouveau. Mon code complet est le suivant:

public class MyDatabaseHelper extends SQLiteOpenHelper { 

    // System path of application database. 
    private static String DB_PATH = MyApplication.getAppContext() 
      .getFilesDir().getParentFile().getPath() 
      + "/databases/"; 
    private static String DB_NAME = "myDB"; 
    private SQLiteDatabase myDataBase; 
    private final Context myContext; 

    /** 
    * Constructor Takes and keeps a reference of the passed context in order to 
    * access to the application assets and resources. 
    * 
    * @param context 
    */ 
    public MyDatabaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

    /** 
    * Creates a empty database on the system and rewrites it with own database. 
    * 
    */ 
    public void createDataBase() throws IOException { 

     checkDataBase(); 
     SQLiteDatabase db_Read = null; 

     // Creates empty database default system path 
     db_Read = this.getReadableDatabase(); 
     db_Read.close(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 

    /** 
    * Checks if the database already exist to avoid re-copying the file each 
    * time whenever the application opened. 
    * 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDataBase() { 

     SQLiteDatabase checkDB = null; 
     try { 
      String myPath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY); 
     } catch (SQLiteException e) { 
      e.printStackTrace(); 
     } 

     if (checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 

    /** 
    * Copies database from local assets-folder to the just created empty 
    * database in the system folder and from where it can be accessed and 
    * handled using byte stream transferring. 
    * 
    */ 
    private void copyDataBase() throws IOException { 

     // Open local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME + ".db"); 

     // Path of the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 

     // Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 

     // transfer bytes from the input file to the output file 
     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(); 
    } 

    public SQLiteDatabase openDataBase() throws SQLException { 

     // Opens the database 
     String myPath = DB_PATH + DB_NAME; 
     return SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } 

    @Override 
    public synchronized void close() { 
     if (myDataBase != null) 
      myDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 
    } 
} 

J'ai trouvé ma réponse here:

0

Vous avez probablement le chemin de hardcoded la base de données. Pour différents appareils, cela peut être différent. Utilisez Envirenment.getExternalStorageDirectory() pour créer le chemin d'accès à votre base de données.

0

essayer avec cette chaîne private static DB_PATH = "/ data/data/votre nom de package/bases de données /";

Questions connexes