In my project I have table named places as follows:
places(int id auto increment, string placename, double latitude, double longitude, string placedetails)
Now, I have created two classes as follows:
Place.java (for model (getters and setters))
public class Place {
private String mname;
private double mlatitude,mlongitude;
private String mplacedetails;
public Place(String name, double latitude, double longitude,String placedetails)
{
mname = name;
mlatitude = latitude;
mlongitude = longitude;
mplacedetails = placedetails;
}
public Place() {
}
public void setname(String placename)
{
mname = placename;
}
public String getname()
{
return mname;
}
public void setlatitude(double latitude)
{
mlatitude=latitude;
}
public double getlatitude()
{
return mlatitude;
}
public void setlongitude(double longitude)
{
mlongitude = longitude;
}
public double getlongitude()
{
return mlongitude;
}
public void setPlacedetails(String placedetails)
{
mplacedetails = placedetails;
}
public String getplacedetails()
{
return mplacedetails;
}
}
MySDQLiteHelper.java (class that extends SQLiteOpenHelper
)
public class MySqliteHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MyApplicationDatabase";
private static final String KEY_ID = "id";
private static final String KEY_PLACENAME = "placename";
private static final String KEY_PLACEDETAILS = "placedetails";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private static final String TABLE_NAME = "places";
public MySqliteHelper(Context context) {
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//actual query = create table places (id primary key autoincrement, placename taxt, latitude real, longitude real);
String query = "CREATE TABLE " +TABLE_NAME + "( " + KEY_ID+
" INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PLACENAME+
" TEXT, "+ KEY_PLACEDETAILS+
" TEXT, "+ KEY_LATITUDE+
" REAL, "+KEY_LONGITUDE+ " REAL)";
db.execSQL(query);
Log.d("my", "Successfully created table: " + query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS places");
this.onCreate(db);
}
public void addPlaces(Place place)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues convalues = new ContentValues();
convalues.put(KEY_PLACENAME,place.getname());
convalues.put(KEY_LATITUDE,place.getlatitude());
convalues.put(KEY_LONGITUDE,place.getlongitude());
convalues.put(KEY_PLACEDETAILS,place.getplacedetails());
db.insert(TABLE_NAME, null, convalues);
Log.d("my","db.insert(TABLE_NAME, null, convalues)");
Log.d("my", "Values inserted");
db.close();
}
public Place getPlace(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID, KEY_LATITUDE, KEY_LONGITUDE,
KEY_PLACENAME, KEY_PLACEDETAILS }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Place place = new Place(cursor.getString(1),
cursor.getDouble(2), cursor.getDouble(3),cursor.getString(4));
return place;
}
public List<Place> getAllPlaces() {
List<Place> placeList = new ArrayList<Place>();
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d("my","query fired");
Log.d("my",cursor.toString());
if (cursor.moveToFirst()) {
Log.d("my","in movetofirst()");
do {
Log.d("my","in do");
Place place = new Place();
place.setname(cursor.getString(1));
Log.d("my","place name");
place.setlatitude(cursor.getDouble(2));
place.setlongitude(cursor.getDouble(3));
place.setPlacedetails(cursor.getString(4));
placeList.add(place);
} while (cursor.moveToNext());
}
return placeList;
}
}
Each time I call addPlace
from activity on click of button like:
mpostplacename = (EditText) findViewById(R.id.edittext_placename);
mpostplacedetails = (EditText) findViewById(R.id.edittext_placedetails);
gps = new GPSTracker(RememberActivity.this);
if(gps.canGetLocation()) {
mylatitude = gps.getLatitude();
mylongitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLatitude: " + mylatitude + "\nLongitude: " + mylongitude, Toast.LENGTH_LONG).show();
myplacename = mpostplacename.getText().toString();
myplacedetails = mpostplacedetails.getText().toString();
if(myplacename==null)
{
myplacename = mylatitude+" "+mylongitude;
}
if(myplacedetails==null)
{
myplacedetails = "No details given about this place";
}
MySqliteHelper db = new MySqliteHelper(this);
db.addPlaces(new Place(myplacename, mylatitude, mylongitude, myplacedetails));
When I want to show stored data in Log like:
MySqliteHelper db = new MySqliteHelper(this);
List<Place> places = db.getAllPlaces();
for (Place p : places) {
String log = "name:" + p.getname() + " ,details: " + p.getplacedetails();
Log.d("my", log);
}
After inserting placename and placedetails (both as some string values like blablabla etc.), log shows place name correct but place details wrong:
D/my﹕ name:sdsdsda ,details: 0
D/my﹕ name:aaaaaaaaaaa ,details: 0
Why getplacedetails
is not working? I couldn't found any mistake in cursor index. may be the error is in index...because it can not find place details on index. Can anyone help me?
Replace lines of code like this:
place.setname(cursor.getString(1));
With something like this:
place.setname(cursor.getString(cursor.getColumnIndex(KEY_PLACENAME)));
It looks like the column corresponding to KEY_PLACEDETAILS
is actually the 2nd column, and you're trying to get something from the 4th column. Using cursor.getColumnIndex()
to get the column matching with each key is safer anyways, in case your database changes later and to help avoid bugs like this.