如何从 Android 中的“独立"方法引用 SQLite 数据库 [英] How to refer to to a SQLite database from an 'independent' Method in Android

查看:18
本文介绍了如何从 Android 中的“独立"方法引用 SQLite 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有访问 SQLite 表的代码,该表可以从我的主要"活动中完美运行.我试图通过创建一个单独的类来处理数据插入、删除等,从而使代码更加优雅.

I have code to access a SQLite table that works perfectly from my "main" activity. I'm trying to make the code more elegant by creating a separate class to handle data inserts, deletes, etc.

当我将完全相同的代码从主活动移到一个单独的类时,我收到以下错误:

When I move the exact same code from the main activity to a separate class I get the following error:

方法openOrCreateDatabase(String, int,null) 类型未定义条码

The method openOrCreateDatabase(String, int, null) is undefined for the type BarCode

我注意到通过使用Activity"扩展我的数据库类,错误消失了.但是,现在我在运行代码时收到 NullPointerException.

I notice that by extending my database class with "Activity" the error goes away. However, now I am getting NullPointerException when I run the code.

如何正确提取数据库代码并仍然能够从 Android 中的 Activity 引用它?

How would be the correct way to abstract database code and still be able to refer to it from an activity in Android?

如果我弄错了行话,请见谅,因为我是 Android 和 Java 的新手.

Excuse me if I am getting the jargon wrong because I am a newbie to Android and Java.

完整代码如下:

package com.example.stockcontrol;

import java.util.Locale;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class BarCode extends Activity {

 public BarCode() {

 }

 public void insertBarCode(String upc) {

  SQLiteDatabase db;
        db = openOrCreateDatabase(
         "StockControl.db"
         , SQLiteDatabase.CREATE_IF_NECESSARY
         , null
        );
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);

        ContentValues mUpc = new ContentValues();
        mUpc.put("upc", "444");
        mUpc.put("description", "Box o toast");
        mUpc.put("scan_count", "1");
        db.insertOrThrow("tbl_upc", null, mUpc);

 }

}

推荐答案

与其让您的数据库类扩展 Activity,不如将相关的 Context 传递给该类.您可以在构造函数中传递它,并将对 Context 的引用保留为类变量.

Rather than having your database class extend Activity, you should pass the relevant Context to the class. You can pass it in the constructor and keep a reference to the Context as a class variable.

public class BarCode {
    private final Context mContext;

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

    public void insertBarCode(String upc) {

     SQLiteDatabase db;
           db = mContext.openOrCreateDatabase(
            "StockControl.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
           );
           db.setVersion(1);
           db.setLocale(Locale.getDefault());
           db.setLockingEnabled(true);

           ContentValues mUpc = new ContentValues();
           mUpc.put("upc", "444");
           mUpc.put("description", "Box o toast");
           mUpc.put("scan_count", "1");
           db.insertOrThrow("tbl_upc", null, mUpc);

    }
}

要使用该类,您可以在您的活动中执行此操作:

To use the class, you would do this in your Activity:

BarCode barCode = new BarCode(this);
barCode.insertBarCode("123456");

这篇关于如何从 Android 中的“独立"方法引用 SQLite 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆