0

I am trying to connect SQLite database in Android Studio through SQLiteAssetHelper, I have tried everything, but it is showing ... Unable to Open Database. I don't know what is the problem here. I am new to android development. I have seen other question's answers, but it couldn't help me.

This is my DB Class:

public class MyDB extends SQLiteAssetHelper {

private static final String DB_NAME="Demo.db";
private static final int DB_ver=1;

//private static MyDB myDB=null;



public MyDB(Context context) {
    super(context, DB_NAME, null, DB_ver);
}
}

this is DB Access Class:

public class OpenDBHelper {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static OpenDBHelper openDBHelper ;

private OpenDBHelper(Context context)
{

    this.openHelper = new MyDB(context);

}

public static OpenDBHelper getInstance(Context context)
{
    if(openDBHelper==null)
    {
        openDBHelper=new OpenDBHelper(context.getApplicationContext()) ;

    }
    return openDBHelper;
}

public SQLiteDatabase openwrite()
{
    this.db= openHelper.getReadableDatabase();
    return db;
}


}

and this is the main activity where I am trying to load the data in listview:

public class MainActivity extends AppCompatActivity {

ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv=(ListView) findViewById(R.id.lv_main);
    loadStudents();

}
public void loadStudents()
{

    ArrayList<HashMap<String, String>> userList = GetAllStudents();

    ListAdapter adapter = new SimpleAdapter(this, userList, R.layout.lv_row,new String[]{"name","cast","phone"}, new int[]{R.id.tv_name, R.id.tv_cast, R.id.tv_phone});
    lv.setAdapter(adapter);
}
//GEt All Students to listview
public ArrayList<HashMap<String, String>> GetAllStudents(){
    ArrayList<HashMap<String, String>> userList = new ArrayList<>();


    OpenDBHelper open= OpenDBHelper.getInstance(this);
    SQLiteDatabase db=open.openwrite();

    String query = "SELECT * FROM tbl_bio";
    Cursor cursor = db.rawQuery(query,null);
    if(cursor.getCount()>0) {
        while (cursor.moveToNext()) {
            HashMap<String, String> user = new HashMap<>();
            user.put("name", cursor.getString(1));
            user.put("cast", cursor.getString(2));
            user.put("phone", cursor.getString(3));
            //user.put("pass", cursor.getString(4));
            userList.add(user);
        }
    }


    return  userList;
}
}
5
  • Any reason why you are not using room? developer.android.com/topic/libraries/architecture/room Commented Nov 8, 2018 at 11:00
  • @finki I have some record which I want to load at starting of app.. so I think ready made database of SQLite can help me.. I created database in SQLite DB Browser. Commented Nov 8, 2018 at 11:25
  • That doesn't answer the question. Why do you not use Room? Commented Nov 8, 2018 at 13:04
  • @ZUNJAE you are not really any help here. To op: check this answer here: stackoverflow.com/questions/5086962/… Commented Nov 8, 2018 at 14:27
  • Alright it's fine if I'm not helping, but why would anyone still use SQLiteDatabase manually, without any libraries? Commented Nov 8, 2018 at 14:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.