2016-08-10 29 views
0

我在stClass創建數據庫,我想在其他類(ndClass)中使用相同的數據庫。 Android的工作室想讓所有靜態的,所以我做到了,但我無法解析cannot be referenced from a static fieldAndroid:SQLite數據庫的Getter

stClass:

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

ndClass:

Base db = AddDate.getDb(); 

我知道,問題很可能是便便...

回答

-1
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) { 

    } 

} 
+0

[鏈接] https://zapodaj.net/images/b021c721d5efb.jpg 現在看起來這一點。 – Xalion

+0

你正在使用哪個IDE? –

+0

Android Studio 2.1.2 – Xalion

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

}

相關問題