因此,我试图从 MySQLiteHelper 类访问我的自定义函数,该活动从 MyApplication 中提取应用程序范围的数据库,但 Eclipse 说我的方法无法被 SQLiteDatabase 类识别(这就是 db )。如何转移我的方法以便在我的数据库中使用它们?
public class MyApplication extends Application {
private static SQLiteDatabase db = null;
@Override
public void onCreate() {
super.onCreate();
MySQLiteHelper SQLiteHelper = new MySQLiteHelper( this );
db = SQLiteHelper.getWritableDatabase();
}
public static SQLiteDatabase getDB() {
return db;
}
}
这是活动:
public class MainActivity extends Activity {
MyApplication app;
private SQLiteDatabase db;
int sessionNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
app = (MyApplication) getApplication();
db = MyApplication.getDB();
// it's complaining about the following line because getLastSession is in my MySQLiteHelper class:
sessionNo = db.getLastSession() + 1;
}
MySQLiteHelper 类的功能:
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_RECORDS = "records";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_SESSION = "session";
public static final String COLUMN_ROUNDNO = "roundno";
public static final String COLUMN_ARROWS = "arrows";
public static final String COLUMN_ROUNDSUM = "roundsum";
public static final String COLUMN_ROUNDAVE = "average";
private static final String DATABASE_NAME = "records.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_RECORDS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_SESSION
+ " integer, " + COLUMN_ROUNDNO + " integer, " + COLUMN_ARROWS
+ " integer, " + COLUMN_ROUNDSUM + " integer, " + COLUMN_ROUNDAVE
+ " double " + ");";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
onCreate(db);
}
public void addRecord(Record record) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_SESSION, record.getSession()); // Contact Name
values.put(COLUMN_ROUNDNO, record.getRoundno()); // Contact Phone Number
values.put(COLUMN_ARROWS, record.getArrows()); // Contact Phone Number
values.put(COLUMN_ROUNDSUM, record.getRoundsum());
values.put(COLUMN_ROUNDAVE, record.getAverage());
// Inserting Row
db.insert(TABLE_RECORDS, null, values);
db.close(); // Closing database connection
}
public ArrayList<Record> getAllRecords() {
ArrayList<Record> recordList = new ArrayList<Record>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_RECORDS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Record record = new Record();
record.setSession(cursor.getInt(1));
record.setRoundno(cursor.getInt(2));
record.setArrows(cursor.getInt(3));
record.setRoundsum(cursor.getInt(4));
record.setAverage(cursor.getDouble(5));
recordList.add(record);
} while (cursor.moveToNext());
}
cursor.close();
return recordList;
}
public int getLastSession() {
SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + TABLE_RECORDS;
Cursor cursor = db.rawQuery(countQuery, null);
if (cursor != null) {
cursor.moveToLast();
return cursor.getInt(1);
} else {
return 0;
}
}
}