使用查询在sqlite中从多列执行多个值?

时间:2017-03-11 11:18:08

标签: android sqlite android-sqlite

我已提及此link并能够进行数据库查询以进行过滤。 目前我使用以下代码从数据库中过滤7 BusTypes ,以下代码工作正常,但我现在必须为 boardingpoint droppoints 应用过滤器也。我怎么能这样做?

我的数据库包含多个boarding_point列表作为一个字符串,因为每个城市都有多个登机,例如。, [city pointone,city pointtwo,city point_three] 作为bording_point中的字符串。现在我想用一个城市点来查询,如果我用 city pointone 进行搜索,它应该给我所有有这一点的公共汽车..

public final class DatabaseConstants {

// To prevent someone from accidentally instantiating the contract class,
// make the constructor private.

private DatabaseConstants(){
}

public static class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "temp_table";
    public static final String ID = "id";
    public static final String FROM_CITY_NAME = "fromcityname";
    public static final String SRC_TIME = "srctime";
    public static final String TO_CITY_NAME = "tocityname";
    public static final String DES_TIME = "destime";
    public static final String DISTANCE = "distance";
    public static final String BUS_ID = "busid";
    public static final String OPERATOR_NAME = "operatorname";
    public static final String BUS_TYPE = "bustype";
    public static final String BUS_SEAT = "busseat";
    public static final String DURATION = "total_duration";
    public static final String BOARDING_POINT = "boarding_points";
    public static final String DROPPING_POINT = "dropping_points";

    public static final String CREATE_TABLE = ("CREATE TABLE " + TABLE_NAME + " ( "
            + ID + " TEXT PRIMARY KEY , "
            + FROM_CITY_NAME + " TEXT, "
            + SRC_TIME + " TEXT, "
            + TO_CITY_NAME + " TEXT, "
            + DES_TIME + " TEXT, "
            + DISTANCE + " TEXT, "
            + BUS_ID + " TEXT, "
            + OPERATOR_NAME + " TEXT, "
            + BUS_TYPE + " TEXT, "
            + BUS_SEAT + " TEXT, "
            + DURATION + " TEXT, "
            + BOARDING_POINT + " TEXT, "
            + DROPPING_POINT + " TEXT )");

    public static final String DROP_TABLE = ("DROP TABLE IF EXISTS " + TABLE_NAME);
}

}

//模型类

    public class BusDataModel {
    private String id;
    private String fromcityname;
    private String src_time;
    private String tocityname;
    private String des_time;
    private String distance;
    private String busid;
    private String operatorname;
    private String bustype;
    private String busseat;
    private String busduration;
    private String boarding_point;
    private String droppint_point;

    public BusDataModel() {
    }

    public BusDataModel(String id, String fromcityname, String src_time, String tocityname, String des_time,
                        String distance, String busid, String operatorname, String bustype, String busseat,String busduration,
                        String boarding_point,String droppint_point) {
        this.id = id;
        this.src_time = src_time;
        this.des_time = des_time;
        this.fromcityname = fromcityname;
        this.tocityname = tocityname;
        this.distance = distance;
        this.busid = busid;
        this.operatorname = operatorname;
        this.bustype = bustype;
        this.busseat = busseat;
        this.busduration=busduration;
        this.boarding_point=boarding_point;
        this.droppint_point=droppint_point;
    }

    public String getBusduration() {
        return busduration;
    }

    public void setBusduration(String busduration) {
        this.busduration = busduration;
    }

    public String getid() {
        return id;
    }

    public String getSrc_time() {
        return src_time;
    }

    public String getDes_time() {
        return des_time;
    }

    public String getFromcityname() {
        return fromcityname;
    }

    public String getTocityname() {
        return tocityname;
    }

    public String getDistance() {
        return distance;
    }

    public String getBusid() {
        return busid;
    }

    public String getOperatorname() {
        return operatorname;
    }

    public String getBustype() {
        return bustype;
    }

    public String getBusseat() {
        return busseat;
    }

    public void setid(String id) {
        this.id = id;
    }

    public void setSrc_time(String src_time) {
        this.src_time = src_time;
    }

    public void setDes_time(String des_time) {
        this.des_time = des_time;
    }

    public void setFromcityname(String fromcityname) {
        this.fromcityname = fromcityname;
    }

    public void setTocityname(String tocityname) {
        this.tocityname = tocityname;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }

    public void setBusid(String busid) {
        this.busid = busid;
    }

    public void setOperatorname(String operatorname) {
        this.operatorname = operatorname;
    }

    public void setBustype(String bustype) {
        this.bustype = bustype;
    }

    public void setBusseat(String busseat) {
        this.busseat = busseat;
    }

    public String getBoarding_point() {
        return boarding_point;
    }

    public void setBoarding_point(String boarding_point) {
        this.boarding_point = boarding_point;
    }

    public String getDroppint_point() {
        return droppint_point;
    }

    public void setDroppint_point(String droppint_point) {
        this.droppint_point = droppint_point;
    }
}

// sqlite class

public class FilterData extends SQLiteOpenHelper {
    private Context context;
    List<BusDataModel> filterdData = new LinkedList<>();
    public static final String DATABASE_NAME = "temp_user";

    public FilterData(Context context) {
        super(context, DATABASE_NAME, null, 3);
        this.context = context;
        onCreate(this.getWritableDatabase());
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
        db.execSQL(CREATE_TABLE);
    }

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


    /**
     * Method to save all Data into Database
     *
     * @param busDataModel to get all Data Generically
     */
    public boolean addBusDetails(BusDataModel busDataModel) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(ID, busDataModel.getid());
        values.put(FROM_CITY_NAME, busDataModel.getFromcityname());
        values.put(SRC_TIME, busDataModel.getSrc_time());
        values.put(TO_CITY_NAME, busDataModel.getTocityname());
        values.put(DES_TIME, busDataModel.getDes_time());
        values.put(DISTANCE, busDataModel.getDistance());
        values.put(BUS_ID, busDataModel.getBusid());
        values.put(OPERATOR_NAME, busDataModel.getOperatorname());
        values.put(BUS_TYPE, busDataModel.getBustype());
        values.put(BUS_SEAT, busDataModel.getBusseat());
        values.put(DURATION, busDataModel.getBusduration());
        values.put(BOARDING_POINT, busDataModel.getBoarding_point());
        values.put(DROPPING_POINT, busDataModel.getDroppint_point());

        long isInserted = db.insert(TABLE_NAME, null, values);
        db.close();
        if (isInserted == -1)
            return false;
        else
            return true;
    }

public List<BusDataModel> setFilterByBusType(List<String> bus_types, List<String> boarding_types, List<String> dropping_types) {

        List<BusDataModel> busList = new LinkedList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        String[] data = {ID, FROM_CITY_NAME, SRC_TIME, TO_CITY_NAME, DES_TIME,
                DISTANCE, BUS_ID, OPERATOR_NAME, BUS_TYPE, BUS_SEAT, DURATION, BOARDING_POINT, DROPPING_POINT};

        String selection = BUS_TYPE + " IN (?,?,?,?,?,?,?)";
        String[] selectionArgs = (String[]) bus_types.toArray(new String[bus_types.size()]);

        Cursor cursor = db.query(TABLE_NAME, data, selection, selectionArgs,
                null, null, null);

        BusDataModel busDataModel;
        if (cursor.moveToFirst()) {
            do {
                busDataModel = new BusDataModel();
                busDataModel.setid(cursor.getString(cursor.getColumnIndex(ID)));
                busDataModel.setFromcityname(cursor.getString(cursor.getColumnIndex(FROM_CITY_NAME)));
                busDataModel.setSrc_time(cursor.getString(cursor.getColumnIndex(SRC_TIME)));
                busDataModel.setTocityname(cursor.getString(cursor.getColumnIndex(TO_CITY_NAME)));
                busDataModel.setDes_time(cursor.getString(cursor.getColumnIndex(DES_TIME)));
                busDataModel.setDistance(cursor.getString(cursor.getColumnIndex(DISTANCE)));
                busDataModel.setBusid(cursor.getString(cursor.getColumnIndex(BUS_ID)));
                busDataModel.setOperatorname(cursor.getString(cursor.getColumnIndex(OPERATOR_NAME)));
                busDataModel.setBustype(cursor.getString(cursor.getColumnIndex(BUS_TYPE)));
                busDataModel.setBusseat(cursor.getString(cursor.getColumnIndex(BUS_SEAT)));
                busDataModel.setBusduration(cursor.getString(cursor.getColumnIndex(DURATION)));
                busDataModel.setBoarding_point(cursor.getString(cursor.getColumnIndex(BOARDING_POINT)));
                busDataModel.setDroppint_point(cursor.getString(cursor.getColumnIndex(DROPPING_POINT)));

                busList.add(busDataModel);
            } while (cursor.moveToNext());
        }
        db.close();
        cursor.close();

        return busList;

    }

    }

1 个答案:

答案 0 :(得分:0)

  

我的数据库包含多个boarding_point列表作为一个字符串,因为每个城市都有多个登机,例如。,[city pointone,city pointtwo,city point_three]作为bording_point中的字符串。

首先,您需要解析字符串,以便拥有每个城市。然后,您可以使用这些城市构建查询。