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 boolean isCityExist(Context context, String city) {
    File file = new File(context.getFilesDir(), DATABASE_NAME);
    SQLiteDatabase db = context.openOrCreateDatabase(file.getAbsolutePath(), Context.MODE_PRIVATE, null);
    Cursor cursor = db.rawQuery("select count(1) from " + TABLE_NAME + " where DQXX02=?",
            new String[] { city });
    cursor.moveToNext();/*from www .ja v a 2 s .  c o m*/
    return cursor.getInt(0) > 0;
}

From source file:Main.java

private static ArrayList<String> readStrings(SQLiteDatabase db, String query, int colIdx) {
    ArrayList<String> rows = new ArrayList<String>();
    Cursor c = db.rawQuery(query, null);
    while (c.moveToNext()) {
        rows.add(c.getString(colIdx));/*from   w w  w .  ja v a  2s .c o  m*/
    }
    c.close();
    return rows;
}

From source file:Main.java

static String getAccessTokenFromTable(Context context, String tableName) {
    String token = null;/*from www . j av a2s.com*/
    try {
        SQLiteDatabase db = context.openOrCreateDatabase(DEPRECATED_DATABASE_NAME, 0, null);
        Cursor c = db.rawQuery(
                "SELECT " + DEPRECATED_ACCESS_TOKEN_COLUMN + " FROM " + tableName + " WHERE local_id=0", null);
        if (c.moveToFirst() && c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN) != -1) {
            token = c.getString(c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN));
        }
        c.close();
        db.close();
    } catch (SQLException e) {
        // DB doesn't exist
    }
    return token;
}

From source file:Main.java

public static List<String> GetColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;/* w w  w.  ja  v a 2 s . co  m*/
    try {
        c = db.rawQuery("select * from " + tableName + " limit 1", null);
        if (c != null) {
            ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (c != null)
            c.close();
    }
    return ar;
}

From source file:Main.java

/**
 * Get columns for table/*w w w  . j a  va 2 s  . c  o m*/
 * @param db
 * @param tableName
 * @return
 */
public static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;
    try {
        c = db.rawQuery("select * from " + tableName + " limit 1", null);
        if (c != null) {
            ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (c != null)
            c.close();
    }
    return ar;
}

From source file:Main.java

/**
 * to query columns names of a Table from database
 * @author wajdihh//ww  w .j  av  a2 s . c  om
 * @param pDatabase
 * @param pTableName
 * @return list of columns
 */
public static ArrayList<String> getTableColumnsNames(SQLiteDatabase pDatabase, String pTableName) {
    ArrayList<String> lListOfColumns = new ArrayList<String>();
    Cursor lCursor = pDatabase.rawQuery("PRAGMA table_info(" + pTableName + ")", null);
    while (lCursor.moveToNext())
        lListOfColumns.add(lCursor.getString(1));
    return lListOfColumns;
}

From source file:Main.java

private static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;/*from   w  ww . j ava 2s.co m*/
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}

From source file:Main.java

private static List<String> getTableColumns(final SQLiteDatabase database, final String tableName) {
    List<String> ret = null;
    try {/*from   w  w  w. jav  a  2s . c o  m*/
        final Cursor cur = database.rawQuery("SELECT * FROM " + tableName + " LIMIT 1", null);
        if (cur != null) {
            ret = new ArrayList<String>(Arrays.asList(cur.getColumnNames()));
        }
        cur.close();
    } catch (final Exception exception) {
        exception.printStackTrace();
    }
    return ret;
}

From source file:Main.java

private static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;//ww w. j  a v a2 s  .  c om
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}

From source file:Main.java

private static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> columns = new ArrayList<String>();
    Cursor cursor = null;/*  w w w.  j av  a2 s.  c om*/
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}