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

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

这是我的 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;
    }

    }

我的 列表 class :

这里 class,我想根据数据库中的值创建一个列表视图。

如何访问这些值?

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 class 内的一个线程中,然后我就可以访问这些方法了。

  1. MySQLiteHelper 没有 getLength() 的方法,但没关系。 (更多内容在最后)
  2. read() 方法不是 public,它除了填充 MySQLiteHelper member/field MySQLiteHelper class 之外什么都不做].
  3. 您没有利用您创建的 getTable() 方法(这也不是 public)。

让我们继续制作 getTable public(考虑重命名它,因为它实际上返回 table 数据而不是实际的数据库 table,由您决定),并且无论何时调用它,我们也可以在调用此方法时调用 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 :)