Skip to content

Instantly share code, notes, and snippets.

@esteedqueen
Last active December 13, 2016 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esteedqueen/eeda9b96bbf6c246ea57db8f231ea28a to your computer and use it in GitHub Desktop.
Save esteedqueen/eeda9b96bbf6c246ea57db8f231ea28a to your computer and use it in GitHub Desktop.
working with sqliteopenhelper
public class MyDBHandler extends SQLiteOpenHelper {
// ...existing methods...
public static class StudentDetailCursor extends SQLiteCursor {
/** The query for this cursor */
private static final String QUERY =
"SELECT _id, _studentname, _phone, _address, lat, lng, zom FROM students ";
/** Cursor constructor */
private StudentDetailCursor(SQLiteDatabase db, SQLiteCursorDriver driver,
String editTable, SQLiteQuery query) {
super(db, driver, editTable, query);
}
/** Private factory class necessary for rawQueryWithFactory() call */
private static class Factory implements SQLiteDatabase.CursorFactory{
@Override
public Cursor newCursor(SQLiteDatabase db,
SQLiteCursorDriver driver, String editTable,
SQLiteQuery query) {
return new StudentDetailCursor(db, driver, editTable, query);
}
}
/* Accessor functions -- one per database column */
public long getColStudentsId() {
return getLong(getColumnIndexOrThrow("_id"));
}
public String getColPhone() {
return getString(getColumnIndexOrThrow("_phone"));
}
public String getColAddress(){
return getString(getColumnIndexOrThrow("_address"));
}
public long getColLatitude() {
return getLong(getColumnIndexOrThrow("lat"));
}
public long getColLongitude() {
return getLong(getColumnIndexOrThrow("lng"));
}
public long getColZoom() {
return getLong(getColumnIndexOrThrow("zom"));
}
}
/** Returns a Student for the specified studentID
* @param studentID The _id of the student
*/
public StudentDetailCursor getStudentDetails(String studentID) {
String sql = StudentDetailCursor.QUERY + studentID;
SQLiteDatabase d = getReadableDatabase();
StudentDetailCursor c = (StudentDetailCursor)
d.rawQueryWithFactory(
new StudentDetailCursor.Factory(),
sql,
null,
null);
c.moveToFirst();
return c;
}
}
private MyDBHandler.StudentDetailCursor student;
MyDBHandler db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new MyDBHandler(this);
student = db.getStudentDetails(student_id);
student_lng = student.getColLongitude
student_lat = student.getColLatitude
double lng = Double.parseDouble(student_lng);
double lat = Double.parseDouble(student_lat);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment