简体   繁体   中英

Read SQLite datas in same method OR Read datas from two table on same method

I have a problem; My projecy is I have 2 databases, First for Favorite location with x,y(Lat,Long) datas in sqlite db. Second is campaigns database same x,y coordinates. There is bunch of different datas like 45.12345 and 35.12345 I want to read the datas from databases and calculate the distance between them, I am using cursor to move next data but I can not reach the favorite location in Campaigns db or opposite how I can read the datas in same method or field to calculate the distance ?

Code 1 for first db:

public List<DataModel> listFavoriteLocation(){
    db = new DatabaseHelper(this);
    SQLiteDatabase mydb = db.getWritableDatabase();
    List<DataModel> data=new ArrayList<>();
    Cursor csr = mydb.rawQuery("select * from "+TABLE+" ;",null);
    StringBuffer stringBuffer = new StringBuffer();
    DataModel dataModel = null;
    while (csr.moveToNext()) {
        dataModel= new DataModel();
        String FAVCurrentLocationLAT = csr.getString(csr.getColumnIndexOrThrow("FAVCurrentLocationLAT"));
        String FAVCurrentLocationLONG = csr.getString(csr.getColumnIndexOrThrow("FAVCurrentLocationLONG"));
        dataModel.setFAVCurrentLocationLAT(FAVCurrentLocationLAT);
        dataModel.setFAVCurrentLocationLONG(FAVCurrentLocationLONG);
        stringBuffer.append(dataModel);
        data.add(dataModel);

    }
    for (DataModel mo:data ) {
this.List_FAVCurrentLocationLAT = mo.getFAVCurrentLocationLAT();
        this.List_FAVCurrentLocationLONG = mo.getFAVCurrentLocationLONG();

         Log.i("helloLAT",""+List_FAVCurrentLocationLAT); //OK
         Log.i("helloLONG",""+List_FAVCurrentLocationLONG); //OK
    }
    return data;
}

Code 2 for second db:

public List<DataModelCampaigns> Campaigns(){
    db2 = new DatabaseHelperCampaigns(this);
    SQLiteDatabase mydb2 = db2.getWritableDatabase();
    List<DataModelCampaigns> data=new ArrayList<>();
    Cursor csr2 = mydb2.rawQuery("select * from "+TABLE2+" ;",null);
    StringBuffer stringBuffer = new StringBuffer();
    DataModelCampaigns dataModel = null;
    while (csr2.moveToNext()) {
        dataModel= new DataModelCampaigns();
        String CampaignsLAT = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLAT"));
        String CampaignsLONG = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLONG"));
        dataModel.setCampaignsLAT(CampaignsLAT);
        dataModel.setCampaignsLONG(CampaignsLONG);
        stringBuffer.append(dataModel);
        data.add(dataModel);
    }
    for (DataModelCampaigns mo:data   ) {
        this.CampaignsLAT = mo.getCampaignsLONG();
        this.CampaignsLONG = mo.getCampaignsLAT();
        Log.i("helloCampaignsLAT",""+CampaignsLAT); //OK
        Log.i("helloCampaignsLONG",""+CampaignsLONG); //OK
    }
    return data;
}

Code for calculation not necessary for answer but someone can have benefit when reached from google searches

double theta = foo2 - y;
        double dist = Math.sin(Math.toRadians(foo)) * Math.sin(Math.toRadians(x)) + Math.cos(Math.toRadians(foo)) * Math.cos(Math.toRadians(x)) * Math.cos(Math.toRadians(theta));
        dist = Math.acos(dist); // radian
        dist = Math.toDegrees(dist); //degree
        dist = dist * 60 * 1.1515; //to km
        dist = dist * 1.609344; // to km...

        if (dist <= 50)
        {
            //Log.i("NEAR",""+dist);
        }

One way, based upon your code could be to not try to use the single last value stored as as String that is :-

FAVCurrentLocationLAT, FAVCurrentLocationLONG, CampaignsLAT and CampaignsLONG

as per (for Last pair) :-

    String CampaignsLAT = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLAT"));
    String CampaignsLONG = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLONG"));

But to instead use the returned List of objects from each method.

eg as an example of processing the lists together ( not in a method but could be placed into a method very easily, just that I believe it's clearer more concise to not make it a method for demonstration ):-

    addTestData();

    mydatamodellist = listFavoriteLocation(); // declared using List<DataModel> mydatamodellist;
    mydatamodelcampaignslist = Campaigns(); // declared using List<DataModelCampaigns> mydatamodelcampaignslist; 
    for (DataModel dm: mydatamodellist) {
        for (DataModelCampaigns dmc: mydatamodelcampaignslist) {
            Log.i("VALUES",
                    "Favourite is " + dm.getFAVCurrentLocationLAT() + "(lat) by " + dm.getFAVCurrentLocationLONG() + "(long)" +
                            "\tCampaign is " + dmc.getCampaignsLAT() + "(lat) by " + dmc.getCampaignsLONG() + "(long)"
            );
        }
    }

Where addTestData adds data as per :-

    db.add("1.1","10.10");
    db.add("6.6","6.6");
    db.add("7.7","7.7");
    db.add(new DataModel("8.8","8.8"));


    db2.add("1.1","1.1");
    db2.add("2.2","2.2");
    DataModelCampaigns dmc = new DataModelCampaigns("3.3","3.3");
    db2.add(dmc);
    db2.add("4.4","4.4");
    db2.add(new DataModelCampaigns("1.1","10.1"));

The Log includes :-

2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 1.1
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLONG: 10.10
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 6.6
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLONG: 6.6
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 7.7
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLONG: 7.7
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 8.8
2019-04-30 09:48:42.796 7610-7610/q.campaigns I/helloLONG: 8.8
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 1.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 1.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 2.2
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 2.2
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 3.3
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 3.3
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 4.4
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 4.4
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 10.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 1.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 1.1(lat) by 10.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 1.1(lat) by 10.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 1.1(lat) by 10.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.802 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.802 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.802 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.803 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 1.1(lat) by 10.1(long)

By adding the class :-

public class FavoutiteCampaignLocationDifference {

    private DataModel Fav;
    private DataModelCampaigns Campaign;

    FavoutiteCampaignLocationDifference(DataModel fav, DataModelCampaigns campaign) {
        this.Fav = fav;
        this.Campaign = campaign;
    }

    public DataModel getFav() {
        return Fav;
    }

    public void setFav(DataModel fav) {
        Fav = fav;
    }

    public DataModelCampaigns getCampaign() {
        return Campaign;
    }

    public void setCampaign(DataModelCampaigns campaign) {
        Campaign = campaign;
    }

    public String getDifference() {

        //<<<<<<<<<< assumes the same elevation/height >>>>>>>>>>

        double earth_radius = 6371;
        double fav_lattitude = Double.valueOf(Fav.getFAVCurrentLocationLAT());
        double fav_longitude = Double.valueOf(Fav.getFAVCurrentLocationLONG());
        double cam_lattitude = Double.valueOf(Campaign.getCampaignsLAT());
        double cam_longitude = new Double(Campaign.getCampaignsLONG());
        double lat_distance = Math.toRadians(fav_lattitude - cam_lattitude);
        double long_distance = Math.toRadians(fav_longitude - cam_longitude);
        double arc = (Math.sin(lat_distance / 2)
                * Math.sin(lat_distance /2 ))
                +  Math.cos(Math.toRadians(fav_lattitude)) * Math.cos(Math.toRadians(cam_lattitude))
                * Math.sin(long_distance / 2) * Math.sin(long_distance / 2);
        double c = 2 * Math.atan2(Math.sqrt(arc), Math.sqrt(1 - arc));
        double distance = Math.pow(earth_radius * c  * 1000,2);
        return String.valueOf(distance);
    }
} 

and then using :-

    mydatamodellist = listFavoriteLocation(); // declared using List<DataModel> mydatamodellist;
    mydatamodelcampaignslist = Campaigns(); // declared using List<DataModelCampaigns> mydatamodelcampaignslist;
    for (DataModel dm: mydatamodellist) {
        for (DataModelCampaigns dmc: mydatamodelcampaignslist) {
            Log.i("VALUES",
                    "Favourite is " + dm.getFAVCurrentLocationLAT() + "(lat) by " + dm.getFAVCurrentLocationLONG() + "(long)" +
                            "\tCampaign is " + dmc.getCampaignsLAT() + "(lat) by " + dmc.getCampaignsLONG() + "(long)" +
                            " \n\tDistance is " + (new FavoutiteCampaignLocationDifference(dm,dmc).getDifference()) + " km" //<<<<<<<<<< ADDED
            );
        }

Results in :-

2019-04-30 13:17:00.210 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 1.1(lat) by 1.1(long) 
        Distance is 1.0011393900913772E12 km
2019-04-30 13:17:00.210 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 2.2(lat) by 2.2(long) 
        Distance is 7.859529733692336E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 3.3(lat) by 3.3(long) 
        Distance is 6.306550755502897E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 4.4(lat) by 4.4(long) 
        Distance is 5.3532678200287164E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long)    Campaign is 1.1(lat) by 10.10(long) 
        Distance is 0.0 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 1.1(lat) by 1.1(long) 
        Distance is 7.460634458912574E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 2.2(lat) by 2.2(long) 
        Distance is 4.772175072844561E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 3.3(lat) by 3.3(long) 
        Distance is 2.6825415833231607E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 4.4(lat) by 4.4(long) 
        Distance is 1.1912923776776952E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long)  Campaign is 1.1(lat) by 10.10(long) 
        Distance is 5.24682799338737E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 1.1(lat) by 1.1(long) 
        Distance is 1.0734024701552842E12 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 2.2(lat) by 2.2(long) 
        Distance is 7.449624475249066E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 3.3(lat) by 3.3(long) 
        Distance is 4.764262727131514E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 4.4(lat) by 4.4(long) 
        Distance is 2.677605323409585E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long)  Campaign is 1.1(lat) by 10.10(long) 
        Distance is 6.093089156683494E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 1.1(lat) by 1.1(long) 
        Distance is 1.4595734067322017E12 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 2.2(lat) by 2.2(long) 
        Distance is 1.0716189231163793E12 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 3.3(lat) by 3.3(long) 
        Distance is 7.43589252928275E11 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 4.4(lat) by 4.4(long) 
        Distance is 4.754615910210995E11 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long)  Campaign is 1.1(lat) by 10.10(long) 
        Distance is 7.537882177227869E11 km

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM