Android: Getter SQLite 中的数据库

Android: Getter of database in SQLite

我在 stClass 创建了数据库,我想在其他 class( ndClass) 使用相同的数据库。 Android studio 想把所有的都设为静态所以我做了,但我无法解决 cannot be referenced from a static field

stClass:

 public static Base db= new Base(this,null,null,0);// error in "this"
public static Base getDb() {
    return db;
}

第二类:

Base db = AddDate.getDb();

我知道这个问题可能很简单...

public class Base extends SQLiteOpenHelper {

    private static Base sInstance;

    private static final String DATABASE_NAME = "database name";
    private static final String DATABASE_TABLE = "table";
    private static final int DATABASE_VERSION = 1;

    public static synchronized Base getInstance(Context applicationContext) {

        // Make sure it is application context, which will ensure that you
        // don't leak an Activity's context.
        if (sInstance == null) {
            sInstance = new Base(applicationContext);
        }
        return sInstance;
    }

    /**
     * private constructor prevent direct instantiation.
     */
    private Base(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

}
public class Base extends SQLiteOpenHelper {



public static final String DB_NAME ="db_workout2";//name of base
public static  final String DB_TABLE ="dateOfWorkout2";//name of table
public static final int DB_VER =1;
private static Base sInstance;


public static synchronized Base getInstance(Context applicationContext) {


    if (sInstance == null) {
        sInstance = new Base(applicationContext);
    }
    return sInstance;
}
private   Base(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DB_NAME, null, DB_VER);

}





@Override


public void onCreate(SQLiteDatabase db){
    db.execSQL(
            "create table "+ DB_TABLE +"("
            + "nr integer primary key autoincrement,"
            + "name text,"
            + "s1 integer,"
            + "s2 integer,"
            + "s3 integer,"
            + "s4 integer,"
            + "s5 integer,"
            + "weight integer,"
            +"ddate text);"
            +"");

}

public void addWorkoutPlan(String i, int q, int w, int e, int r, int t, int wt,String d) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues wartosci = new ContentValues();

    wartosci.put("name", i);
    wartosci.put("s1", q);
    wartosci.put("s2", w);
    wartosci.put("s3", e);
    wartosci.put("s4", r);
    wartosci.put("s5",t);
    wartosci.put("weight",wt);
    wartosci.put("ddate",d);



    db.insertOrThrow(DB_TABLE, null, wartosci);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
    onCreate(db);

}
public List<String> getDateLabels(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + DB_TABLE;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}

}