nrofis
nrofis

Reputation: 9796

Android - Save location in SQLite database

I have problem with save latitude and longitude in SQLite database. I can save some location like (123.123999, 123.123999) and it will save in the database. But if I want to read it, I get rounded location like (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 querys results:

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

So I can't use the numbers I get to do queries on the data.

I believe that I am not the first that handling with this problem, because it is basic thing. But I didn't found any answers that work on SQLite database.

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

Upvotes: 4

Views: 8663

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33515

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

I suggest you an one quick solution.

Since you know in what format you are storing your coordinates e.q. numbers so there is no problem to store them as TEXT and then retrieve them as TEXT and simple perform parsing back to DOUBLE if you'll want to do some stuff with them.

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

Upvotes: 2

scf
scf

Reputation: 406

You should define an epsilon around the queried value, i.e.

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

You can change your epsilon according to your need. In the example you gave 1e-3 should suffice.

Another option is to round the values when you insert them, but then you'll lose information you might need down the road.

Upvotes: 0

Related Questions