Example usage for android.database.sqlite SQLiteDatabase rawQuery

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

Introduction

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

Prototype

public Cursor rawQuery(String sql, String[] selectionArgs) 

Source Link

Document

Runs the provided SQL and returns a Cursor over the result set.

Usage

From source file:net.smart_json_database.JSONDatabase.java

private String getPropterty(SQLiteDatabase db, String key, String defaultValue) {
    String returnValue = defaultValue;

    Cursor c = db.rawQuery("SELECT * FROM " + TABLE_Meta + " WHERE key = ?", new String[] { key });

    if (c.getCount() > 0) {
        //int key_col = c.getColumnIndex("key");
        int value_col = c.getColumnIndex("value");

        c.moveToFirst();/*from  w  ww .  j a v  a2  s . com*/

        if (c != null) {
            if (c.isFirst()) {
                do {
                    returnValue = c.getString(value_col);
                    if (Util.IsNullOrEmpty(returnValue)) {
                        returnValue = defaultValue;
                    }
                    break;
                } while (c.moveToNext());
            }
        }
    }
    c.close();

    return returnValue;
}

From source file:com.snt.bt.recon.database.DBHandler.java

/**
 * Compose JSON out of SQLite records/*from   w  ww  .  ja va 2  s . co  m*/
 * @return
 */
public <T> String composeJSONfromSQLite(Class<T> cl) throws InstantiationException, IllegalAccessException {
    T inst = cl.newInstance();
    List<T> list = new ArrayList<T>();

    // Select All Query
    String selectQuery;
    Cursor cursor;
    SQLiteDatabase db = this.getWritableDatabase();
    if (inst instanceof Trip) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_TRIPS + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "' or "
                + KEY_UPLOAD_STATUS + " = '" + "partial" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Trip trip = new Trip();
                trip.setSessionId(UUID.fromString(cursor.getString(0)));
                trip.setImei(cursor.getString(1));
                trip.setTransport(cursor.getString(2));

                trip.setTimestampStart(cursor.getString(3));
                trip.setTimestampEnd(cursor.getString(4));
                trip.setAppVersion(cursor.getString(5));

                // Adding contact to list
                list.add((T) trip);
            } while (cursor.moveToNext());
        }
        cursor.close();

    } else if (inst instanceof GPSLocation) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_LOCATIONS + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                GPSLocation location = new GPSLocation();
                location.setLocationId(UUID.fromString(cursor.getString(0)));
                location.setSessionId(UUID.fromString(cursor.getString(1)));
                location.setTimestamp(cursor.getString(2));
                location.setLatitude(cursor.getFloat(3));
                location.setLongitude(cursor.getFloat(4));
                location.setSpeed(cursor.getFloat(5));
                location.setBearing(cursor.getFloat(6));
                location.setAltitude(cursor.getFloat(7));
                location.setAccuracy(cursor.getFloat(8));

                // Adding contact to list
                list.add((T) location);
            } while (cursor.moveToNext());
        }
        cursor.close();

    }

    else if (inst instanceof BluetoothClassicEntry) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_BC + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                BluetoothClassicEntry bc_entry = new BluetoothClassicEntry();
                bc_entry.setId(cursor.getInt(0));
                bc_entry.setSessionId(UUID.fromString(cursor.getString(1)));
                bc_entry.setLocationId(UUID.fromString(cursor.getString(2)));
                bc_entry.setTimestamp(cursor.getString(3));
                bc_entry.setMac(cursor.getString(4));
                bc_entry.setType(cursor.getInt(5));
                bc_entry.setRssi(cursor.getInt(6));
                bc_entry.setDeviceName(cursor.getString(7));
                bc_entry.setBcClass(cursor.getString(8));

                // Adding contact to list
                list.add((T) bc_entry);
            } while (cursor.moveToNext());
        }
        cursor.close();

    }

    else if (inst instanceof BluetoothLowEnergyEntry) {
        // Select All Query
        selectQuery = "SELECT * FROM " + TABLE_BLE + " where " + KEY_UPLOAD_STATUS + " = '" + "no" + "'";
        cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                BluetoothLowEnergyEntry ble_entry = new BluetoothLowEnergyEntry();
                ble_entry.setId(cursor.getInt(0));
                ble_entry.setSessionId(UUID.fromString(cursor.getString(1)));
                ble_entry.setLocationId(UUID.fromString(cursor.getString(2)));
                ble_entry.setTimestamp(cursor.getString(3));
                ble_entry.setMac(cursor.getString(4));
                ble_entry.setRssi(cursor.getInt(5));
                ble_entry.setDeviceName(cursor.getString(6));
                ble_entry.setBleAdvData(cursor.getString(7));

                // Adding contact to list
                list.add((T) ble_entry);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    db.close();

    // return contact list
    Gson gson = new GsonBuilder().create();
    //Use GSON to serialize Array List to JSON
    Log.d("DatabaseTest", "composeJSONfromSQLite " + cl.getName() + " " + gson.toJson(list));

    return gson.toJson(list);
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public int getResponseFilterCount() {
    String countQuery = "SELECT  * FROM " + TABLE_RESPONSE_FILTERS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);

    int count = cursor.getCount();
    cursor.close();/*from  ww  w  .j a  v  a  2  s  .c o  m*/

    // return count
    return count;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public int getDomainFilterCount() {
    String countQuery = "SELECT  * FROM " + TABLE_DOMAIN_FILTERS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);

    int count = cursor.getCount();
    cursor.close();// w  ww  . j a v  a  2 s .c  o m

    // return count
    return count;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public List<FilterFile> getAllDomainFilterFiles() {
    List<FilterFile> filterFiles = new ArrayList<>();
    String selectQuery = "SELECT " + KEY_SOURCE + ", COUNT(*) AS " + KEY_COUNT + " FROM " + TABLE_DOMAIN_FILTERS
            + " WHERE " + KEY_SOURCE + " IS NOT NULL GROUP BY " + KEY_SOURCE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);
    if (c.moveToFirst()) {
        do {//from  w  ww.jav  a  2  s  .c o  m

            FilterFile filterFile = new FilterFile();
            filterFile.setSource((c.getString(c.getColumnIndex(KEY_SOURCE))));
            filterFile.setFilterCount(c.getInt(c.getColumnIndex(KEY_COUNT)));
            filterFiles.add(filterFile);
        } while (c.moveToNext());
    }

    return filterFiles;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public List<ResponseFilter> getAllResponseFilters() {
    List<ResponseFilter> responseFilters = new ArrayList<ResponseFilter>();
    String selectQuery = "SELECT  * FROM " + TABLE_RESPONSE_FILTERS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {/* ww  w . j  a  v  a2 s .c om*/
            ResponseFilter responseFilter = new ResponseFilter();
            responseFilter.setId(c.getInt(c.getColumnIndex(KEY_ID)));
            responseFilter.setContent((c.getString(c.getColumnIndex(KEY_CONTENT))));
            responseFilter.setSource((c.getString(c.getColumnIndex(KEY_SOURCE))));
            responseFilter.setModified(c.getString(c.getColumnIndex(KEY_MODIFIED)));

            responseFilters.add(responseFilter);
        } while (c.moveToNext());
    }

    return responseFilters;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public List<ResponseFilter> getAllUserResponseFilters() {
    List<ResponseFilter> responseFilters = new ArrayList<ResponseFilter>();
    String selectQuery = "SELECT  * FROM " + TABLE_RESPONSE_FILTERS + " WHERE " + KEY_SOURCE + " IS NULL ";

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {/*from  w w w  .ja v a2  s.co  m*/
            ResponseFilter responseFilter = new ResponseFilter();
            responseFilter.setId(c.getInt(c.getColumnIndex(KEY_ID)));
            responseFilter.setContent((c.getString(c.getColumnIndex(KEY_CONTENT))));
            responseFilter.setSource((c.getString(c.getColumnIndex(KEY_SOURCE))));
            responseFilter.setModified(c.getString(c.getColumnIndex(KEY_MODIFIED)));

            responseFilters.add(responseFilter);
        } while (c.moveToNext());
    }

    return responseFilters;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public List<FilterFile> getAllResponseFilterFiles() {
    List<FilterFile> filterFiles = new ArrayList<>();
    String selectQuery = "SELECT " + KEY_SOURCE + ", COUNT(*) AS " + KEY_COUNT + " FROM "
            + TABLE_RESPONSE_FILTERS + " WHERE " + KEY_SOURCE + " IS NOT NULL GROUP BY " + KEY_SOURCE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);
    Log.e("tag", selectQuery);
    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {//from   w  w w .j  av  a2 s  . c  om

            FilterFile filterFile = new FilterFile();
            filterFile.setSource((c.getString(c.getColumnIndex(KEY_SOURCE))));
            filterFile.setFilterCount(c.getInt(c.getColumnIndex(KEY_COUNT)));
            Log.e("tag", filterFile.toString());
            Log.e("tag", filterFile.getTitle());
            filterFiles.add(filterFile);
        } while (c.moveToNext());
    }

    return filterFiles;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public List<String> getAllResponseFiltersForSource(String source) {
    List<String> filters = new ArrayList<>();
    String selectQuery = "SELECT " + KEY_CONTENT + " FROM " + TABLE_RESPONSE_FILTERS + " WHERE " + KEY_SOURCE
            + " = ?";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, new String[] { source });

    int count = 0;

    if (c.moveToFirst()) {
        do {/*from   ww  w  .  j a  v a2 s  . c  o m*/
            filters.add(c.getString(c.getColumnIndex(KEY_CONTENT)));
            count++;
        } while (c.moveToNext() && count < LIMIT);
    }

    if (count == LIMIT) {
        filters.add("--- Omitted " + (c.getCount() - LIMIT) + " entries ---");
    }

    return filters;
}

From source file:eu.operando.operandoapp.database.DatabaseHelper.java

public List<DomainFilter> getAllDomainFilters() {
    List<DomainFilter> domainFilters = new ArrayList<>();
    String selectQuery = "SELECT  * FROM " + TABLE_DOMAIN_FILTERS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {// ww  w  . j  a v a2 s . co m
            DomainFilter domainFilter = new DomainFilter();
            domainFilter.setId(c.getInt(c.getColumnIndex(KEY_ID)));
            domainFilter.setContent((c.getString(c.getColumnIndex(KEY_CONTENT))));
            domainFilter.setSource((c.getString(c.getColumnIndex(KEY_SOURCE))));
            domainFilter.setModified(c.getString(c.getColumnIndex(KEY_MODIFIED)));
            domainFilter.setWildcard((c.getInt(c.getColumnIndex(KEY_WILDCARD))));
            domainFilters.add(domainFilter);
        } while (c.moveToNext());
    }

    return domainFilters;
}