0

我还真的掌握了我们在 android 编程中发现的很多内容。所以我尝试创建一个函数来删除我的所有表,这是我的部分代码:

public class DBAdapter {
       private static class DbHelper extends SQLiteOpenHelper {
        private boolean databaseCreated = false;
        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void deleteTables(){
        Log.d("DBAdapter","dlm drop tables pre");
        this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_TV+"];");
        this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_CAMERA+"];");
        this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_GPS+"];");
       }
}
}

以及我要调用函数 deleteTables 的部分

public class UpdateDatabase {
     public void updateTable(String table,JSONObject jsonObject){
    DBAdapter db = new DBAdapter(this);
        db.deleteTables();
    }
}

但当然它会返回一个错误,因为 DBAdapter 需要一个上下文。公共类 UpdateDatabase 不是活动类。从活动类调用 DbAdapter db = new DBAdapter(this) 只会找到。那么我如何找到解决这个问题的方法呢?

谢谢

4

2 回答 2

2

您可以将构造函数添加到UpdateDatabase接受 aContext并存储它,以便它可供updateTable. 像这样的东西:

public class UpdateDatabase {
    private final Context mContext;

    public UpdateDatabase(Context context){
        mContext = context;
    }

    public void updateTable(String table,JSONObject jsonObject){
        DBAdapter db = new DBAdapter(mContext);
        db.deleteTables();
    }
}

现在,无论何时你new UpdateDatabase()都需要这样做new UpdateDatabase(..context..)。如果您是从 执行此Activity操作,那么您可以执行此操作new UpdateDatabase(this)

于 2012-06-11T16:31:55.053 回答
0

嗨看看这段代码..

public class DbManager
{
// the Activity or Application that is creating an object from this class.
Context context;
CustomSQLiteOpenHelper helper;
// a reference to the database used by this application/object
protected SQLiteDatabase db;
private static DbManager INSTANCE;
// These constants are specific to the database. 
protected final String DB_NAME = "yourDB";
protected final int DB_VERSION = 1;



public DbManager(Context context)
{
    this.context = context; 
    // create or open the database
    helper = new CustomSQLiteOpenHelper(context);
    this.db = helper.getWritableDatabase();
}
public static DbManager getInstance(Context context){
    if(INSTANCE == null)INSTANCE = new DbManager(context);
    return INSTANCE;
    }
public void db_Close()
{
    if(helper!=null){
        helper.close();
    }
    this.db.close();
}



private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{

    public CustomSQLiteOpenHelper(Context context)
    {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // This string is used to create the database.
        // execute the query string to the database.
        //db.execSQL(newTableQueryString);
        Log.i("DataBaseManager", "Create Table");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
        // OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
    }

}
}

// Inherit the DbManager Class
 public class DataCollection extends DbManager {

public DataCollection(Context context){
        super(context);
}
   public void deleteTable(String TABLE_NAME){
    try {db.execSQL("DROP TABLE "+TABLE_NAME);}//.delete(TABLE_NAME, null, null);}
    catch (Exception e){
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }
}
于 2012-06-11T16:27:22.020 回答