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:Main.java

public static int getGroupCount(SQLiteDatabase db) {
    int result = 0;
    //      SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);

    Cursor cursor = db.rawQuery("select count(*) from classlist;", null);
    cursor.moveToFirst();/*from  w  w  w. java 2s .  co m*/
    result = cursor.getInt(0);
    cursor.close();
    //      db.close();
    return result;

}

From source file:Main.java

public static ArrayList<String> queryDay(SQLiteDatabase db) {
    ArrayList<String> dayList = new ArrayList<String>();

    String sqlStr = "select distinct date from " + TABLE_NAME + " order by _id desc";

    Cursor cursor = db.rawQuery(sqlStr, null);

    while (cursor.moveToNext()) {
        dayList.add(cursor.getString(0));
    }/*from ww  w  . j a  va2 s.c  om*/

    return dayList;
}

From source file:com.xfinity.ceylon_steel.controller.CustomerController.java

public static ArrayList<Customer> getCustomers(Context context) {
    ArrayList<Customer> customers = new ArrayList<Customer>();
    SQLiteDatabaseHelper databaseInstance = SQLiteDatabaseHelper.getDatabaseInstance(context);
    SQLiteDatabase database = databaseInstance.getWritableDatabase();
    Cursor customerCursor = database.rawQuery("select customerId, customerName from tbl_customer", null);
    int customerIdIndex = customerCursor.getColumnIndex("customerId");
    int customerNameIndex = customerCursor.getColumnIndex("customerName");
    for (customerCursor.moveToFirst(); !customerCursor.isAfterLast(); customerCursor.moveToNext()) {
        Customer customer = new Customer(customerCursor.getInt(customerIdIndex),
                customerCursor.getString(customerNameIndex));
        customers.add(customer);//  w  w  w .  j a  v a2s .  c  o  m
    }
    customerCursor.close();
    databaseInstance.close();
    return customers;
}

From source file:Main.java

public static boolean isTableExists(SQLiteDatabase db, String tableName) {
    String sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ? ";
    String paras[] = { tableName };
    try {//from www. j av  a 2s . c  o  m
        Cursor cursor = db.rawQuery(sql, paras);
        cursor.moveToNext();
        String name = cursor.getString(cursor.getColumnIndex("name"));
        if (name.equals(tableName)) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static Map<Integer, List> getCityByPid(int id, File file) {

    String sql = "select cityid,city  from city where provinceid= " + id;
    SQLiteDatabase db = null;
    Cursor c = null;//from   w w w .  ja v a  2  s .  c  o m
    Map<Integer, List> cityData = new HashMap<Integer, List>();
    //List cityList = null;
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        List cityList1 = new ArrayList();
        List cityList2 = new ArrayList();
        while (c.moveToNext()) {
            Map cityMap = new HashMap();
            cityMap.put(c.getString(1), c.getInt(0));
            cityList1.add(cityMap);
            cityList2.add(c.getString(1));
        }
        cityData.put(0, cityList1);
        cityData.put(1, cityList2);

    } catch (Exception e) {
        Log.d("WineStock", "getCityByPid:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return cityData;
}

From source file:Main.java

public static Map<Integer, List> getProvince(File file) {

    String sql = "select provinceid ,province from province ";
    SQLiteDatabase db = null;
    Cursor c = null;//from   w ww.  j a va2  s .  c om
    Map<Integer, List> provinceData = new HashMap<Integer, List>();
    //List provinceList = null;
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        List provinceList1 = new ArrayList();
        List provinceList2 = new ArrayList();
        while (c.moveToNext()) {
            Map provinceMap = new HashMap();
            provinceMap.put(c.getString(1), c.getInt(0));
            provinceList1.add(provinceMap);
            provinceList2.add(c.getString(1));
        }
        provinceData.put(0, provinceList1);
        provinceData.put(1, provinceList2);
    } catch (Exception e) {
        Log.d("WineStock", "getProvince:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return provinceData;
}

From source file:Main.java

public static Map<Integer, List> getAreaByPid(int id, File file) {

    String sql = "select area,areaid  from area where cityid= " + id;
    SQLiteDatabase db = null;
    Cursor c = null;/*from   w w  w .j  a v  a 2 s  .c om*/
    List<String> areaList = null;
    List areaIdList = null;
    Map<Integer, List> areaData = new HashMap<Integer, List>();
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        areaList = new ArrayList<String>();
        areaIdList = new ArrayList<String>();

        while (c.moveToNext()) {
            Map areaMap = new HashMap();
            areaMap.put(c.getString(0), c.getInt(1));
            areaList.add(c.getString(0));
            areaIdList.add(areaMap);
        }
        areaData.put(0, areaList);
        areaData.put(1, areaIdList);
    } catch (Exception e) {
        Log.d("WineStock", "getAreaByPid:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return areaData;
}

From source file:Main.java

public static int queryCount(SQLiteDatabase db, String tableName, String where, String[] whereArgs) {
    StringBuffer stringBuffer = new StringBuffer("select count(*) from ");
    stringBuffer.append(tableName);/* w ww .jav  a2  s  .  c  o  m*/
    if (where != null) {
        stringBuffer.append(" where ");
        stringBuffer.append(where);
    }
    Cursor cursor = db.rawQuery(stringBuffer.toString(), whereArgs);
    cursor.moveToFirst();
    int count = cursor.getInt(0);
    cursor.close();
    return count;
}

From source file:nz.co.wholemeal.christchurchmetro.Stop.java

private static ArrayList<Stop> doArrayListQuery(Context context, String query) {
    ArrayList<Stop> stops = new ArrayList<Stop>();

    Log.d(TAG, "query: " + query);

    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor cursor = database.rawQuery(query, null);

    try {/*from ww w .j  a  va2 s.com*/
        if (cursor.moveToFirst()) {
            do {
                Stop stop = new Stop();
                stop.platformTag = cursor.getString(0);
                stop.platformNumber = cursor.getString(1);
                stop.name = cursor.getString(2);
                stop.roadName = cursor.getString(3);
                stop.latitude = cursor.getDouble(4);
                stop.longitude = cursor.getDouble(5);
                stops.add(stop);
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    Log.d(TAG, "stops.size() = " + stops.size());
    database.close();
    return stops;
}

From source file:org.getlantern.firetweet.util.content.DatabaseUpgradeHelper.java

private static String getCreateSQL(final SQLiteDatabase db, final String table) {
    final SQLSelectQuery.Builder qb = select(new Column("sql"));
    qb.from(new Tables("sqlite_master"));
    qb.where(new Expression("type = ? AND name = ?"));
    final Cursor c = db.rawQuery(qb.buildSQL(), new String[] { "table", table });
    if (c == null)
        return null;
    try {//from  w w  w . ja  va2 s  .co  m
        if (c.moveToFirst())
            return c.getString(0);
        return null;
    } finally {
        c.close();
    }
}