1

我想在android中创建两个表。我已经使用 SQLiteOpenHelper 类创建了该表。我想要做的是我为每个表创建了 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);
}
}

第二个 Helper 类为:

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 时遇到错误。我想知道是否可以创建两个 DbHelper 类并像上面一样使用它们。或者我需要创建一个 DbHelper 类。请帮我解决这个问题。

先谢谢了。

4

0 回答 0