gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:49:09 24 4
gpt4 key购买 nike

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

}

我的列表类(class):

在这个类中,我想根据数据库中的值创建一个 ListView 。

如何访问这些值?

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. MySQLiteHelper 没有 getLength() 方法,但这没关系。 (更多内容见最后)
  2. read() 方法不是公共(public)的,它除了填充 MySQLiteHelper 类的表成员/字段之外什么也不做。
  3. 您没有利用您创建的 getTable() 方法(该方法也不是公开的)。

让我们继续并公开 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:)

关于java - Android - 如何从其他类访问已创建的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28919926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com