Android:如何创建数据库管理类的对象然后使用它?

时间:2014-03-19 03:06:47

标签: java android database sqlite

我正在开发一个Android项目,我有一个应用程序的中央数据库,它可以从同一个应用程序中的不同活动访问/输入。对于Android而言,我仍然是一个菜鸟,并且已经遵循了各种教程,但只是为了让自己稍微陷入困境:/

我遇到的问题是,如果下面发布的代码是我的" base"我将通过访问的对象和方法使用的类,如何使用正确的参数创建对象以使其工作?

我尝试使用以下语法在另一个类中创建DatabaseAdmin类的对象: DatabaseAdmin db = new DatabaseAdmin(); 但它说我需要添加参数,上下文。 当我输入参数为: DatabaseAdmin db = new DatabaseAdmin(getApplicationContext()); 该应用程序通过多个错误保持关闭,因此我认为这不是解决方案。

想法?如何创建一个可以访问此类的对象,并且可以使用所有相应的方法,之后,我是否需要做任何异常的事情来使用/调用它?

(PS,如果我包含太多代码,请试着修剪它,不确定是否有重要的东西可能是一个线索。道歉。如果代码太多,请告诉我,我会进一步修改它)

这是我的代码:

    //imports Cropped for space saving
    //This class manages the database for the entire application

    public class DatabaseAdmin extends Activity
    {
    DatabaseHelper dbhelper;
    Context ctx;

    //Create an object of the SQLite database which we can use to open and close it
    SQLiteDatabase db;

    //SQL Variables
    public static final String TABLE_NAME = "table";
    public static final String COLUMN_NAME_AT_RISK = "at_risk";
    public static final String TEXT_TYPE = " TEXT";
    public static final String COMMA = ",";

    //String to create the database
    public static final String SQL_CREATE_DATABASE =
            "CREATE TABLE " + 
            TABLE_NAME + " (" +
            COLUMN_NAME_AT_RISK + " REAL 1417" 
            " )";

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "RSRToolbox.db";

    public DatabaseAdmin(Context ctx) {

        this.ctx = ctx;
        dbhelper = new DatabaseHelper(ctx);
    }

    //DatabaseHelper
    private class DatabaseHelper extends SQLiteOpenHelper {

        //Constructor for DatabaseHelper
        public DatabaseHelper(Context ctx) {
            super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            //Creates database
            try {
            db.execSQL(SQL_CREATE_DATABASE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            db.execSQL("DROP TABLE IF EXIST commissions");
            onCreate(db);
        }

        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

    //Method to open the database
    public DatabaseAdmin openDB(){
        db = dbhelper.getWritableDatabase();
        return this;
    }

    //Method for closing the database
    public void closeDB(){
        dbhelper.close();
    }

    //Method to insert data into the database
    public long InsertData(String column_name, String value){

        ContentValues content = new ContentValues();
        content.put(column_name, value);
        return db.insertOrThrow(TABLE_NAME, null, content);
    }

    public Cursor getData(String[] column_name){

        return db.query(TABLE_NAME, column_name, null, null, null, null, null);
    } 
}

1 个答案:

答案 0 :(得分:1)

尝试使用单例模型:

您的App类(在清单中注册):

class MyApp extends Application {
private static MyApp mInstance;

void onCreate() {
   mInstance = this;
}

public Context context() { return mInstance.getApplicationContext(); }

}

您的数据库类:

public MyDatabase {

public final MyDatabase INSTANCE = new MyDatabase();

private DbHelper mHelper;

public Cursor someMyQuery() {
    return mHelper.query(...);
}

private MyDatabase() {
    mHelper = new DbHelper(MyApp.context());
    ...
}

private static class DbHelper extends SQLiteOpenHelper {
   onCreate() {...}
   onUpdate() {...}
}

}

然后从任何地方访问您的数据库:

MyDatabase.INSTANCE.someMyQuery();