I am using a SQLite to store data in Android application . I need to dump data from a file having relationship between 2 places . e.g.
The table will have the following fields
Location1 Location2 Distance
X Y 12
X Z 13
Y X 12
if I dump data as a row , then I am duplicating the data i.e. the distance between the location X & location Y . I am dumping twice , once from X to Y & the other from Y to X , but it will be help me in making the SQL query as I can directly give the Location1 & Location 2 value & get the distance.
If I want to avoid the duplication of data , how can I proceed ?
I will enter only once data between location 1 & 2 but how will I write the select query.
If anyone has any suggestions/sample code regarding the database structure & the queries , kindly post them.
Also which approach will give me a faster processing incase of Android applications
Warm Regards,
CB
I’m not 100% clear what you’re asking. You are saying that you are duplicating data because the difference between the two fields Location1 and Location2 is the same thing as the Distance field? If that’s the case then you could just keep Location1 and Location2. If you need the difference make your query like so:
“abs” gives you the absolute value.
As far as processing times, I feel like this would be slower than having the Distance field in the table, but then we go back to your duplicate problem.
Does that help? Or did I misinterpret your question?
UPDATE
I think I understand. You have a bunch of locations V, W, X, Y, Z, etc. The user will select from dropdown boxes Location1 and Location2, then your app will supply the distance between the two.
So say the user selects Y for Location1 and W for Location2, your app will then return Y – W.
You are over-complicating this. Your database only needs a record for every location ie:
Then when the user selects Location1 and Location2, simply pull the LocationCoordinates from the database and then get the difference. Depending on how you have the locations stored in the database, you can even do the whole thing within SQL. But for now I’d suggest you just use a SELECT statement to get the locations and find the difference(distance) from within your code.