0

I copy a database from assets after that table of data is going lost

this is android studio and copy database from assets

error is = no such table: tbltexts (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM tbltexts

my dbname=texts.db

THIS ERROR IS ON ANDROID PIE

public class Databasehelper extends SQLiteOpenHelper {

    public static String DB_PATH = "/data/data/com.nooshindroid.Rosary/databases/";
    public static final String DATABASE_NAME = "texts.db";
    private static final int DATABASE_VERSION = 1;
    public static final String tblname ="tbltexts";
    public static final String id="id";
    public static final String arabic ="Arabic_text";
    public static final String english ="English_text";
    public static final String counter ="Counter";
    public static final String goal ="Goal";
    private Context context;
    private SQLiteDatabase database;
    public Databasehelper(Context context) {
        super(context, DATABASE_NAME, null,DATABASE_VERSION);
             this.context = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public void copyDataBase() throws IOException {
        InputStream myInput = context.getAssets().open(DATABASE_NAME);
        String outFileName = DB_PATH + DATABASE_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        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 class DbHandler {


    private SQLiteDatabase database;
    private  String name;
    private Databasehelper dbHelper;
    public DbHandler(Context context){
        dbHelper=new Databasehelper(context);
    }
    public void open(){
        database=dbHelper.getWritableDatabase();
    }

    public  void close(){
       database.close();
    }

    public String display(int row,int field){

        Cursor cursor=database.query(dbHelper.tblname,null,null,null,null,null,null,null);
            cursor.moveToPosition(row);
           name=cursor.getString(field);
        return name;
    }



    public boolean insert(String txtarab,String txtenglish,int goal) {

        ContentValues cv = new ContentValues();
        cv.put(dbHelper.arabic, txtarab);
        cv.put(dbHelper.english, txtenglish);
        cv.put(dbHelper.goal,goal);
        long result = database.insert(dbHelper.tblname, null, cv);
        database.close();

        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public Integer count(){
        Cursor cursor=database.query(dbHelper.tblname,null,null,null,null,null,null);
        int s=cursor.getCount();
        return s;
    }
    public void delete(int id){
        database.delete(dbHelper.tblname,"id="+id,null);
    }
    public void update(String arab, String englsih, int id){
        ContentValues contentValues=new ContentValues();
        contentValues.put(dbHelper.arabic,arab);
        contentValues.put(dbHelper.english,englsih);
        database.update(dbHelper.tblname,contentValues,"id="+id,null);
    }

    public void counterupdate(int id,int counter){
        ContentValues contentValues=new ContentValues();
        contentValues.put(dbHelper.counter,counter);
        database.update(dbHelper.tblname,contentValues,"id="+id,null);
    }

}
Zoe
  • 27,060
  • 21
  • 118
  • 148
dornadev
  • 1
  • 1
  • 8
  • Your code is proper but. if you delete any table at runtime then please put comment temporary and check, it may delete tablet somewhere – Haresh Ramani Apr 23 '19 at 11:08
  • I suggest you look at [this answer](https://stackoverflow.com/questions/55336900/a-pre-populated-database-does-not-work-at-api-28-throws-no-such-table-exceptio/55346290#55346290) – MikeT Apr 23 '19 at 11:28

1 Answers1

1

You should not hard-code the database path, there is no guaranty that it will be /data/data/<package_id>/databases, for example on my phone (Android Pie, API 26) it is /data/user/<user_id>/<package_id>/databases. The documentation even warns that the path may change, as application can be moved to adopted storage.

If the path is wrong (and if the exception from copyDataBase() is catched) you end up with the empty database created automatically by the SQLiteOpenHelper.

A solution could be to replace :

String outFileName = DB_PATH + DATABASE_NAME;

with :

String outFileName = context.getDatabasePath(DATABASE_NAME);
bwt
  • 17,292
  • 1
  • 42
  • 60