Hi everybody, I have a db file which is located in assets folder besides I've tried to connect the db in android studio but I failed the code don't contains any error but when I go to the activity that use the db the app stop
Here's the two classes that I use :
Here's the two classes that I use :
Java:
public class DbHelper extends SQLiteOpenHelper {
private static DbHelper sInstance;
private final Context myContext;
private static final String DATABASE_NAME = "databases/firstAid.db.sql";
private String DATABASE_PATH;
private static int DATABASE_VERSION = 1;
public static synchronized DbHelper getInstance (Context context){
if (sInstance == null) { sInstance = new DbHelper(context); }
return sInstance;
}
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
String filesDir = context.getFilesDir().getPath();
DATABASE_PATH = filesDir.substring(0, filesDir.lastIndexOf("/")) + "/databases/";
}
private boolean checkdatabase() {
File dbfile = new File(DATABASE_PATH + DATABASE_NAME);
return dbfile.exists();
}
private void copydatabase() {
final String outFileName = DATABASE_PATH + DATABASE_NAME;
InputStream myInput;
try {
// Ouvre la bdd de 'assets' en lecture
myInput = myContext.getAssets().open(DATABASE_NAME);
// dossier de destination
File pathFile = new File(DATABASE_PATH);
if(!pathFile.exists()) {
if(!pathFile.mkdirs()) {
Toast.makeText(myContext, "Erreur : copydatabase(), pathFile.mkdirs()", Toast.LENGTH_SHORT).show();
return;
}
}
// Ouverture en écriture du fichier bdd de destination
OutputStream myOutput = new FileOutputStream(outFileName);
// transfert de inputfile vers outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Fermeture
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (IOException e) {
e.printStackTrace();
Toast.makeText(myContext, "Erreur : copydatabase()", Toast.LENGTH_SHORT).show();
}
// on greffe le numéro de version
try{
SQLiteDatabase checkdb = SQLiteDatabase.openDatabase(DATABASE_PATH + DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
checkdb.setVersion(DATABASE_VERSION);
}
catch(SQLiteException e) {
// bdd n'existe pas
}
} // copydatabase()
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < newVersion){
//Log.d("debug", "onUpgrade() : oldVersion=" + oldVersion + ",newVersion=" + newVersion);
myContext.deleteDatabase(DATABASE_NAME);
copydatabase();
}
}
}
Java:
public class ContactManager {
private static final String TABLE_NAME = "contacts";
private static final String NOM_CONTACT = "nom";
private static final String NUMERO_CONTACT = "numero";
private static final String SPECIFICATION_CONTACT = "specification";
private static final String CREATE_TABLE_CONTACTS = "CREATE TABLE "+TABLE_NAME+
" (" +
" "+NOM_CONTACT+" TEXT," +
" "+NUMERO_CONTACT+" INTEGER," +
" "+SPECIFICATION_CONTACT+" TEXT" +
");";
private DbHelper myDb;
private SQLiteDatabase db;
public ContactManager(Context context) {
myDb = DbHelper.getInstance(context);
}
public void open() {
db = myDb.getWritableDatabase();
}
public void close() {
db.close();
}
public long ajouterContact(Contact contact) {
ContentValues contentValues = new ContentValues();
contentValues.put(NOM_CONTACT, contact.getNom());
contentValues.put(NUMERO_CONTACT, contact.getNumero());
contentValues.put(SPECIFICATION_CONTACT, contact.getSpecification());
return db.insert(TABLE_NAME, null, contentValues);
}
}
Java:
public class ContactManager {
private static final String TABLE_NAME = "contacts";
private static final String NOM_CONTACT = "nom";
private static final String NUMERO_CONTACT = "numero";
private static final String SPECIFICATION_CONTACT = "specification";
private static final String CREATE_TABLE_CONTACTS = "CREATE TABLE "+TABLE_NAME+
" (" +
" "+NOM_CONTACT+" TEXT," +
" "+NUMERO_CONTACT+" INTEGER," +
" "+SPECIFICATION_CONTACT+" TEXT" +
");";
private DbHelper myDb;
private SQLiteDatabase db;
public ContactManager(Context context) {
myDb = DbHelper.getInstance(context);
}
public void open() {
db = myDb.getWritableDatabase();
}
public void close() {
db.close();
}
public long ajouterContact(Contact contact) {
ContentValues contentValues = new ContentValues();
contentValues.put(NOM_CONTACT, contact.getNom());
contentValues.put(NUMERO_CONTACT, contact.getNumero());
contentValues.put(SPECIFICATION_CONTACT, contact.getSpecification());
return db.insert(TABLE_NAME, null, contentValues);
}
}
Last edited: