Search code examples
javaandroidandroid-sqlitesqliteopenhelper

Android - How to access already created database from other class?


Here is my SQLiteOpenHelper class :

public class MySQLiteHelper extends SQLiteOpenHelper {

...
//all the code


SQLiteDatabase db;

String url;
String title;
String init_price;
String cur_price;
byte[] image;

// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "ProductsDB";

// Products table name
private static final String TABLE_NAME = "products";

// Products Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_URL = "url";
private static final String KEY_TITLE = "title";
private static final String KEY_INIT_PRICE = "init_price";
private static final String KEY_CUR_PRICE = "cur_price";
private static final String KEY_IMAGE = "image";
private static final String[] COLUMNS = {KEY_ID, KEY_URL, KEY_TITLE, KEY_INIT_PRICE,KEY_CUR_PRICE, KEY_IMAGE};

//An array of database
String[][] table = new String[10][5];
byte[][] images = new byte[10][1];

int len = 0; //no.of rows in the table

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

@Override
public void onCreate(SQLiteDatabase db) {

    this.db = db;

    // SQL statement to create a products table
    String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "url TEXT, " +
            "title TEXT, " +
            "init_price TEXT," +
            "cur_price TEXT," +
            "image BLOB" +
            " );";

    // create products table
    db.execSQL(CREATE_PRODUCTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    // Create tables again
    onCreate(db);
}


void read() {

    int i = 0;

    // 1. build the query
    String query = "SELECT  * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        do {

            table[i][0] = cursor.getString(0);
            table[i][1] = cursor.getString(1);
            table[i][2] = cursor.getString(2);
            table[i][3] = cursor.getString(3);
            table[i][4] = cursor.getString(4);
            images[i] = cursor.getBlob(5);
            i++;
        } while (cursor.moveToNext());
    }
}

String[][] getTable() {
        return table;
    }

    }

My List class :

Here in this class, I want to create a listview from the values in the database.

How do I access those values?

List.java :

public class List extends Activity {

 MySQLiteHelper obj = new MySQLiteHelper(this);

    // I have all the methods to get required data from database.
   // But I'm unable to call those methods directly. Why?
  // I actually can access those methods from a service. 
 // But now, I'm getting "Cannot resolve method" error.



 MySQLiteHelper obj = new MySQLiteHelper(this);

 obj.getLength(); //I'm getting error here
 obj.read(); // It says that it cannot resolve the method.

        }

Why is that? I put the same code in a thread inside List.java class and then I'm able to access those methods.


Solution

    1. MySQLiteHelper has no method for getLength() but that is ok. (more on this at the end)
    2. the read() method is not public, and it really does nothing other than populate the table member/field of the MySQLiteHelper class.
    3. You aren't leveraging the getTable() method you created (which also is not public).

    lets go ahead and make getTable public (consider renaming this as it is really returning table data and not an actual database table, up to you tho), and anytime it is called, lets also call read() anytime this method is called

    // renamed from getTable
    public String[][] getTableData() {
        read();
        return table;
    }
    

    With this small modification, your usage should look something like this:

     MySQLiteHelper obj = new MySQLiteHelper(this);
     String[][] tabledata = obj.getTableData();
     // get the Length  (no getLength() method needed as we will poll the tabledata length property.
     int length = tabledata.length;
    

    I'm going to assume (and I apologize if I'm incorrect) that this your first crack at doing android databases. So it might be good to read a tutorial like this one:

    http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

    HTHS :)