Example usage for android.database.sqlite SQLiteDatabase query

List of usage examples for android.database.sqlite SQLiteDatabase query

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase query.

Prototype

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
        String having, String orderBy, String limit) 

Source Link

Document

Query the given table, returning a Cursor over the result set.

Usage

From source file:com.rener.sea.DBHelper.java

public Person findPersonById(long id) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_PERSON, new String[] { DBSchema.PERSON_ID, },
            DBSchema.PERSON_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();//from w w w . jav  a 2  s.co  m
        Person person = new Person(cursor.getLong(0), this);
        db.close();
        cursor.close();

        return person;
    }
    return new Person(-1, this);
}

From source file:com.rener.sea.DBHelper.java

public User findUserByUsername(String username) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS,
            new String[] { DBSchema.USER_ID, DBSchema.USER_USERNAME, DBSchema.USER_PASSHASH,
                    DBSchema.USER_PERSON_ID, DBSchema.USER_SALT },
            DBSchema.USER_USERNAME + "=?", new String[] { String.valueOf(username) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();//from ww  w  . java 2 s  .  com
        User user = new User(cursor.getLong(0), this);
        db.close();
        cursor.close();

        return user;
    }
    return new User(-1, this);

}

From source file:com.rener.sea.DBHelper.java

public List<User> getAllUsers() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_ID }, null, null, null, null,
            null, null);//from  w  w w .j a  v a 2 s . com
    ArrayList<User> users;
    users = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            users.add(new User(cursor.getLong(0), this));
        }

        db.close();
        cursor.close();

    }
    return users;

}

From source file:com.rener.sea.DBHelper.java

public List<Appointment> getAllAppointments() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_APPOINTMENTS, new String[] { DBSchema.APPOINTMENT_ID }, null, null,
            null, null, "date(" + DBSchema.APPOINTMENT_DATE + ") DESC", null);
    ArrayList<Appointment> Appointments;
    Appointments = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Appointments.add(new Appointment(cursor.getLong(0), this,
                    context.getResources().getString(R.string.date_format_medium)));
        }/*from w w  w  .  java 2 s . c  o  m*/

        db.close();
        cursor.close();

    }
    return Appointments;

}

From source file:com.rener.sea.DBHelper.java

public List<Flowchart> getAllFlowcharts() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_FLOWCHART, new String[] { DBSchema.FLOWCHART_ID },
            DBSchema.STATUS + " =? ", new String[] { String.valueOf(1) }, null, null,
            DBSchema.FLOWCHART_NAME + " COLLATE NOCASE", null);
    ArrayList<Flowchart> flowcharts;
    flowcharts = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            flowcharts.add(new Flowchart(cursor.getLong(0), this));
        }//w  w w  .ja v a  2s . co  m

        db.close();
        cursor.close();

    }
    return flowcharts;

}

From source file:com.rener.sea.DBHelper.java

public List<Report> getAllReports() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_REPORT, new String[] { DBSchema.REPORT_ID, DBSchema.REPORT_NAME },
            DBSchema.REPORT_STATUS + " !=? ", new String[] { String.valueOf(-1) }, null, null,
            "date(" + DBSchema.REPORT_DATE_FILED + ") DESC, " + DBSchema.REPORT_NAME + " COLLATE NOCASE", null);
    ArrayList<Report> reports;
    reports = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            reports.add(new Report(cursor.getLong(0), cursor.getString(1), this));
        }/*from   w  w  w . j av  a2s  .  com*/

        db.close();
        cursor.close();

    }
    return reports;

}

From source file:com.rener.sea.DBHelper.java

public List<Location> getAllLocationsOf(long agentID) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_LOCATION,
            new String[] { DBSchema.LOCATION_ID, DBSchema.LOCATION_ADDRESS_ID, DBSchema.LOCATION_NAME },
            DBSchema.STATUS + " !=? AND " + DBSchema.LOCATION_AGENT_ID + " =? ",
            new String[] { String.valueOf(-1), String.valueOf(agentID) }, null, null,
            DBSchema.LOCATION_NAME + " COLLATE NOCASE", null);
    ArrayList<Location> location;
    location = new ArrayList<>();
    if ((cursor != null) && (cursor.getCount() > 0)) {

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            location.add(new Location(cursor.getLong(0), cursor.getLong(1), cursor.getString(2), this));
        }/*w w  w.j  a  va 2  s  .c  o m*/

        db.close();
        cursor.close();

    }
    return location;

}

From source file:com.rener.sea.DBHelper.java

public List<Location> getAllLocations() {
    String username = context/*w w  w.  j  ava  2s  . c  o  m*/
            .getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE)
            .getString(context.getString(R.string.key_saved_username), null);
    if (isAgent(username)) {
        return getAllLocationsOf(getUserId(username));
    } else {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(DBSchema.TABLE_LOCATION,
                new String[] { DBSchema.LOCATION_ID, DBSchema.LOCATION_ADDRESS_ID, DBSchema.LOCATION_NAME },
                DBSchema.STATUS + " !=? ", new String[] { String.valueOf(-1) }, null, null,
                DBSchema.LOCATION_NAME + " COLLATE NOCASE", null);
        ArrayList<Location> location;
        location = new ArrayList<>();
        if ((cursor != null) && (cursor.getCount() > 0)) {

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                location.add(new Location(cursor.getLong(0), cursor.getLong(1), cursor.getString(2), this));
            }

            db.close();
            cursor.close();

        }
        return location;
    }

}

From source file:com.rener.sea.DBHelper.java

private JSONArray getSpecialization() {

    JSONArray data;// www .j a v a 2  s .  com
    data = new JSONArray();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_SPECIALIZATION,
            new String[] { DBSchema.SPECIALIZATION_ID, DBSchema.SPECIALIZATION_NAME }, DBSchema.MODIFIED + "=?",
            new String[] { DBSchema.MODIFIED_YES }, null, null, null, null);
    if (cursor.moveToFirst()) {
        if ((cursor != null) && (cursor.getCount() > 0))
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                JSONObject map = new JSONObject();
                try {
                    if (!cursor.isNull(0))
                        map.put(DBSchema.SPECIALIZATION_ID, cursor.getString(0));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(1))
                        map.put(DBSchema.SPECIALIZATION_NAME, cursor.getString(1));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                data.put(map);
            }
    }
    db.close();
    cursor.close();
    return data;

}

From source file:com.rener.sea.DBHelper.java

private JSONArray getCategory() {

    JSONArray data;/*w  w  w  .ja v a 2 s .  c  o  m*/
    data = new JSONArray();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_CATEGORY,
            new String[] { DBSchema.CATEGORY_ID, DBSchema.CATEGORY_NAME }, DBSchema.MODIFIED + "=?",
            new String[] { DBSchema.MODIFIED_YES }, null, null, null, null);
    if (cursor.moveToFirst()) {
        if ((cursor != null) && (cursor.getCount() > 0))
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                JSONObject map = new JSONObject();
                try {
                    if (!cursor.isNull(0))
                        map.put(DBSchema.CATEGORY_ID, cursor.getString(0));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    if (!cursor.isNull(1))
                        map.put(DBSchema.CATEGORY_NAME, cursor.getString(1));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                data.put(map);
            }
    }
    db.close();
    cursor.close();
    return data;

}