u_kami
u_kami

Reputation: 575

gps data in sqlite

I'd like know how to create a database in sqlite which contains gps data(latitude,longitude) and then retrieving from the database to display the data on the map?

please I need it urgent

Thanks

Upvotes: 0

Views: 3226

Answers (1)

Vinoth
Vinoth

Reputation: 1359

public class DBAdapter{
public static final String KEY_ROWID = "_id";
public static final String KEY_latitude  = "latitude";
public static final String KEY_longitude = "longitude";
          Cursor cur=null;

private static final String DATABASE_NAME = "MyDB.sqlite";
private static final String DATABASE_TABLE = "gpsCordinates";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE = 
        "create table gpsCordinates(_id integer primary key autoincrement,"
+ "latitude text not null, longitude text not null);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter (Context ctx){
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper (Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try{
            db.execSQL(DATABASE_CREATE);
        }
        catch (SQLException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG,"Upgrading database from version" 
        +oldVersion + " to " +newVersion + ", This will remove all previous data");
        db.execSQL("Drop TABLE IF EXISTS");
        onCreate(db);
    }
}

public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;

}

public DBAdapter openReadable() throws SQLException {
    db = DBHelper.getReadableDatabase();
    return this;

}

public void close(){
    DBHelper.close();
}
//*-- inserts a new contact into database --*//
public long insertContact(String latitudE, String longitudE){
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_latitude, latitudE);
    initialValues.put(KEY_longitude, longitudE);
    return db.insert(DATABASE_TABLE, null, initialValues);

}

public Cursor retrieveRecord(){
  cur= db.rawQuery("YOUR SQL QUERY", null);
    return cur;
}

This should take care of creating a database. Use the cursor to retrieve your values.

Upvotes: 1

Related Questions