访问在MainActivity中创建的SQLiteOpenHelper

时间:2017-07-21 17:44:14

标签: android android-sqlite android-context main-activity

我在MainActivity中创建了一个SQLiteOpenHelper对象:

    public class ExchangeActivity extends AppCompatActivity {


public CurrencyDBHelper db;
private Handler handler;
private int delay = 30000;
private DataHandler dataHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    //create CurrencyDBHelper object
    db = new CurrencyDBHelper(this);
    Log.v("DBTag", "DB created");


    //Activity and UI
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exchange);
    }}

我的CurrencyDBHelper类代码:

public class CurrencyDBHelper extends SQLiteOpenHelper {

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

//constructor
public CurrencyDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE CURRENCY ("
            + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "NAME TEXT, "
            + "RATE REAL); ");
}

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

}

我需要从另一个班级调用db。我怎么能从其他班级做到这一点?

我尝试不在MainActivity中创建SQLiteOpenHelper,但它对我不起作用。好像我必须使用Context,但我不喜欢这样做。

请帮忙。 谢谢!

1 个答案:

答案 0 :(得分:3)

1)将DBHelper类移动到它自己的类中,即 CurrencyDBHelper.java 应该是: -

public class CurrencyDBHelper extends SQLiteOpenHelper {

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

    //constructor
    public CurrencyDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE CURRENCY ("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "NAME TEXT, "
                + "RATE REAL); ");
    }

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

    }
}

2)在您要使用数据库的活动中使用: -

db = new CurrencyDBHelper(this);
  

请注意!这不会创建数据库。数据库只会是   在尝试打开它时创建,例如该   getWRiteableDatabasegetReadableDatabase被调用。

所以3)

SQLiteDatabase mydb = db.getWritableDatabase();

将导致数据库被创建(即将调用DBHelper的onCreate方法。)

  

请注意! onCreate只会在生命周期内被调用一次   数据库文件。因此,如果你想修改结构   数据库,在开发它时可能最容易清除应用数据   或者卸载然后重新安装App。

在此阶段,表格中没有数据。

然而,您可以查询空表,例如: -

        Cursor csr = mydb.query("CURRENCY",null,null,null,null,null,null,null);
        Log.v("MYDB","Table CURRENCY has " +
                Integer.toString(csr.getCount()) +
                " rows"
        );
        for (int i=0; i < csr.getColumnCount(); i++) {
            Log.v("MYDB","Table CURRENCY has a column named " +
                    csr.getColumnName(i)
            );
        }
        csr.close();

使用活动可以是: -

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new CurrencyDBHelper(this);
        SQLiteDatabase mydb = db.getWritableDatabase();
        Cursor csr = mydb.query("CURRENCY",null,null,null,null,null,null,null);
        Log.v("MYDB","Table CURRENCY has " +
                Integer.toString(csr.getCount()) +
                " rows"
        );
        for (int i=0; i < csr.getColumnCount(); i++) {
            Log.v("MYDB","Table CURRENCY has a column named " +
                    csr.getColumnName(i)
            );
        }
        csr.close();
    }

日志的输出将是: -

07-21 17:38:07.290 1886-1886/mjt.cardoniser V/MYDB: Table CURRENCY has 0 rows
07-21 17:38:07.290 1886-1886/mjt.cardoniser V/MYDB: Table CURRENCY has a column named _id
07-21 17:38:07.290 1886-1886/mjt.cardoniser V/MYDB: Table CURRENCY has a column named NAME
07-21 17:38:07.290 1886-1886/mjt.cardoniser V/MYDB: Table CURRENCY has a column named RATE