Lord_Dizzie
Lord_Dizzie

Reputation: 37

How can I store an ArrayList<GeoPoint> in SQLite?

First and foremost, I've never worked with databases before. After reading through multiple post, it sounded a good idea to store the data that my program is gathering in an SQLite database. At the moment, my problem deals with storing an ArrayList of GeoPoints. I know that this can not be stored in an sqlite database. I've read through some of the other like questions and found that a solution may be to create a seperate table, and store all of the points in two columns of that table.

In my Database Adapter, I wrote:

// Make a New GeoPoint Table
public void geopointTable(String routeName)
{
    String GeoTable = "CREATE TABLE " + routeName + "geopoints (_id INTEGER PRIMARY KEY, "
        + "KEY_GEOLONG REAL, "
        + "KEY_GEOLAT REAL);";

    db.execSQL(GeoTable);
}

// Insert into GeoPoint Table
public long insertGeopoints (double longitude, double latitude, String routeName)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_GEOLONG, longitude);
    initialValues.put(KEY_GEOLAT, latitude);
    return db.insert(routeName + "geopoints", null, initialValues);
}

In my main activity that gathers the geopoints and stores them in an array, I wrote:

db.open();

    db.geopointTable(routeName);
    Iterator<GeoPoint> i = points.iterator();
    while (i.hasNext()) {
        db.insertGeopoints(i.next().getLongitudeE6(), i.next()
                .getLatitudeE6(), routeName);

This results in a fatal exception. I guess, in the end, I'd like to have a column in my row that hosts the name of the table that hosts the geopoints. Does anyone know an easier way to do this, or perhaps can see the problem(s) with the code.

Upvotes: 1

Views: 2557

Answers (1)

Piotr
Piotr

Reputation: 833

while (i.hasNext()) {
    db.insertGeopoints(i.next().getLongitudeE6(), i.next()
            .getLatitudeE6(), routeName);

i.next() moves iterator to the next element, so for each iteration, you are inserting longitude from one elem, and latitude from next. If number of elements in array is odd, you are getting OutOfBound exception

About SQL: You probably should use one table for all routes:

 CREATE TABLE route (id INTEGER PRIMARY KEY,
   route_name text, 
   KEY_GEOLONG REAL, 
   KEY_GEOLAT REAL)"  

Upvotes: 1

Related Questions