Android - 如何从其他类访问已创建的数据库?

时间:2015-03-07 20:59:36

标签: java android android-sqlite sqliteopenhelper

这是我的 SQLiteOpenHelper 类:

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;
    }

    }

我的列表类:

在这个类中,我想从数据库中的值创建一个列表视图。

如何访问这些值?

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.

        }

为什么?我将相同的代码放在List.java类的一个线程中,然后我就可以访问这些方法了。

1 个答案:

答案 0 :(得分:1)

  1. MySQLiteHelper没有getLength()的方法,但没关系。 (最后更多内容)
  2. read()方法不公开,除了填充MySQLiteHelper类的表成员/字段外,它实际上什么也没做。
  3. 您没有利用您创建的getTable()方法(也不公开)。
  4. 让我们继续使getTable公开(考虑重命名它,因为它实际上是返回表数据,而不是实际的数据库表,直到你),并且无论何时调用它,我们也可以调用read()任何时候调用此方法

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

    通过这个小修改,您的用法应如下所示:

     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;
    

    我会假设(如果我不对,我道歉)这是你第一次破解Android数据库。所以阅读像这样的教程可能会很好:

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

    HTHS :)