Android - saving location in SQLite database

I have a problem with keeping latitude and longitude in a SQLite database. I can save some location (123.123999, 123.123999) and it will be saved in the database. But if I want to read it, I get a rounded location (123.124, 123.124).

My table created by this sql segment:

CREATE TABLE locs(id INTEGER PRIMARY KEY AUTOINCREMENT, lat DOUBLE, lng DOUBLE, num INTEGER) 

And the query results:

 SELECT * FROM locs WHERE lat = 123.124; //RETURN NO ROWS SELECT * FROM locs WHERE lat = 123.123999; //RETURN 1 ROW 

Therefore, I cannot use the numbers that I get to make queries on the data.

I believe that I am not the first to deal with this problem, because this is the main thing. But I did not find the answers that work with the SQLite database.

How can I read and write the correct number in db?

+4
source share
2 answers

How can I read and write the correct number in db?

I offer you one quick solution.

Since you know in what format you store your eq coordinates, so there is no problem saving them as TEXT , then get them as TEXT and just do parsing on DOUBLE if you want to do something with them.

 CREATE TABLE locs( id INTEGER PRIMARY KEY AUTOINCREMENT, lat TEXT, lng TEXT, num INTEGER ) 
+2
source

You must define epsilon around the requested value, i.e.

 SELECT * FROM locs WHERE lat BETWEEN value-epsilon AND value+epsilon; 

You can change your epsilon to suit your needs. In the example you gave 1e-3, should be enough.

Another option is to round the values โ€‹โ€‹when they are inserted, but then you will lose information that you may need along the way.

0
source

All Articles