1

Androidで2つのテーブルを作成したい。そして、SQLiteOpenHelper calss を使用してそのテーブルを作成しました。私がやろうとしているのは、テーブルごとに 2 つの HelperClass を作成したことです。

public class ConsignmentDetailsDbHelper extends SQLiteOpenHelper{

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "del_med.db";

// Contacts table name
private static final String TABLE_CONSIGNMENT_DETAILS = "tbl_consignment_details";

// Contacts Table Columns names
private static final String CONSIGNMENT_ID = "consignment_id";
private static final String CONSIGNMENT_REF_NO = "consignment_ref_no";

public ConsignmentDetailsDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE_CONSIGNMENT_DETAILS = "CREATE TABLE " +    TABLE_CONSIGNMENT_DETAILS + "("
            + CONSIGNMENT_ID + " INTEGER PRIMARY KEY," + CONSIGNMENT_REF_NO + " TEXT "+")";
    db.execSQL(CREATE_TABLE_CONSIGNMENT_DETAILS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONSIGNMENT_DETAILS);

    // Create tables again
    onCreate(db);
}
}

2番目のヘルパークラスは次のとおりです。

public class BoxDetailsDbHelper extends SQLiteOpenHelper{

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "del_med.db";

// Contacts table name
private static final String TABLE_BOX_DETAILS = "tbl_box_details";

// Contacts Table Columns names
private static final String BOX_ID = "box_id";
private static final String CONSIGNMENT_ID = "consignment_id";
private static final String BOX_CODE = "box_code";

public BoxDetailsDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE_BOX_DETAILS = "CREATE TABLE " + TABLE_BOX_DETAILS + "("
            + BOX_ID +"INTEGER PRIMARY KEY,"+ CONSIGNMENT_ID + " INTEGER," + BOX_CODE + " TEXT "+")";
    Log.d("CREATE_TABLE_BOX_DETAILS", CREATE_TABLE_BOX_DETAILS);
    db.execSQL(CREATE_TABLE_BOX_DETAILS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOX_DETAILS);

    // Create tables again
    onCreate(db);
}
}

私のランチャーアクティビティでは、次のように呼び出しました:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ConsignmentDetailsDbHelper cdh = new ConsignmentDetailsDbHelper(this);
    BoxDetailsDbHelper bdh = new BoxDetailsDbHelper(this);

     /**
     * CRUD Operations
     * */
    // Inserting ConsignmentDetails
    Log.d("Insert: ", "Inserting ..");
    cdh.addConsignmentDetails(new ConsignmentDetailsVO(101, "R2523"));
    cdh.addConsignmentDetails(new ConsignmentDetailsVO(102, "R2524"));
    cdh.addConsignmentDetails(new ConsignmentDetailsVO(103, "R2525"));

    // Inserting BoxDetails
    bdh.addBoxDetails(new BoxDetailsVO(201, 101, "b23652"));
    bdh.addBoxDetails(new BoxDetailsVO(202, 101, "b23653"));
    bdh.addBoxDetails(new BoxDetailsVO(203, 102, "b23654"));
    bdh.addBoxDetails(new BoxDetailsVO(203, 102, "b23655"));
    bdh.addBoxDetails(new BoxDetailsVO(203, 103, "b23656"));
    bdh.addBoxDetails(new BoxDetailsVO(203, 103, "b23657"));

     // Reading all contacts
    Log.d("Reading: ", "Reading all contacts..");
    List<BoxDetailsVO> BoxDetailsVOList = bdh.getAllBoxDetails();      

    for (BoxDetailsVO bd : BoxDetailsVOList) {
        String log = "BoxId: "+bd.getBoxId()+" ,CosignmentId: " +  bd.getConsignmentId() + " ,BoxCode: " + bd.getBoxCode();
        // Writing Contacts to log
        Log.d("Name: ", log);
    }
}
}

しかし、テーブル box_details の作成でエラーが発生しています。2 つの DbHelper クラスを作成して、上記のように使用できることを知りたいです。または、単一の DbHelper クラスを作成する必要があります。このために私を助けてください。

ありがとうございます。

4

0 に答える 0