0
votes

I created a database named dbase using SQLiteManager.It contains a table named comments.Its fields are id(primary key,integer autoincrement)and comment(text). And so its extension is .sqlite.I saved the database in assets folder of my app.i.e ex1\app\build\intermediates\assets.I want to retrieve the table contents to a textview.But,I can't open the database.Can anyone help me?

My DBHElper class extending SQLiteOpenHelper is:

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



 public class DBHElper extends SQLiteOpenHelper {


private static String DB_PATH ="/data/data/com.example.iyas.ex1/databases/";
private static String DB_NAME = "dbase.sqlite";
private SQLiteDatabase myDataBase;
private static final int DATABASE_VERSION = 1;
//TABLE NAME : comments;column id integer autoincrement,comment text

   public DBHElper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
                }

public boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{

        String myPath = DB_PATH + DB_NAME;
        checkDB =SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){
                    Log.d("checkDatabase", e.toString());
    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

public void openDataBase() throws SQLException {

    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);

}

@Override
public void close() {

    if(myDataBase != null)
        myDataBase.close();
    super.close();

}

@Override
    public void onCreate(SQLiteDatabase db) {



}

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

                }


}

My Activity class:

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MyActivity extends Activity {

TextView tv;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    dbQuery();
}

public void dbQuery() {
    try {

        DBHElper helper = new DBHElper(this.getApplicationContext());
        boolean dbExist = helper.checkDataBase();

        if (dbExist) {
            helper.openDataBase();
            Cursor c = db.rawQuery("SELECT comment from comments", null);

            if (c != null) {
                c.moveToFirst();
                {
                    do {
                        String comment = c.getString(1);
                        tv = (TextView) findViewById(R.id.labels);
                        tv.setText(comment);
                    } while (c.moveToNext());
                }
            }

        } else {
            Log.e("IN onStart()", "NO DATABASE EXISTS");
        }

    } catch (SQLiteException se) {

        Log.e("myapp in my activity", se.toString());
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
1
boolean dbExist = helper.checkDataBase(); > What is the value of this boolean? - Nadeem Iqbal
The code seems to be based on a very old blog post with bugs. For the "database from assets" problem, consider using github.com/jgilfelt/android-sqlite-asset-helper - laalto

1 Answers

0
votes

You can use SQLiteAssetsHelper to open your DB. You can download its jar from here.

And you can use it like this:-

public class AssetsHelper extends SQLiteAssetHelper {

  private static final String DATABASE_NAME = "dbase.sqlite";
  private static final int DATABASE_VERSION = 1;

  public AssetsHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

  }
}