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) 

Source Link

Document

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

Usage

From source file:com.fan3cn.fishrecorder.ContentFragment.java

/**
 * ??/*from   w  ww  .  j  a va 2 s.co m*/
 * @param view
 */
private void handleCompanyEvent(final View view) {
    Button addButton = (Button) view.findViewById(R.id.button_add);
    addButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText nameET = (EditText) view.findViewById(R.id.editText_com_name);
            EditText phoneET = (EditText) view.findViewById(R.id.editText_com_phone);
            EditText faxET = (EditText) view.findViewById(R.id.editText_com_fax);
            EditText emailET = (EditText) view.findViewById(R.id.editText_com_email);
            EditText addressET = (EditText) view.findViewById(R.id.editText_com_addr);
            CheckBox ckBox = (CheckBox) view.findViewById(R.id.checkBox_is_default);

            int isDefault = ckBox.isChecked() ? 1 : 0;

            String name = nameET.getText().toString();
            String phone = phoneET.getText().toString();
            String fax = faxET.getText().toString();
            String email = emailET.getText().toString();
            String address = addressET.getText().toString();

            if (name.isEmpty() || phone.isEmpty() || fax.isEmpty() || email.isEmpty() || address.isEmpty()) {
                new AlertDialog.Builder(getActivity()).setTitle("??").setPositiveButton("", null)
                        .setMessage("?").show();
                return;
            }
            SQLiteDatabase db = MainActivity.getDbHelper().getWritableDatabase();

            Cursor cursor = db.query(Constants.table.get(menuId), null, "is_default=?", new String[] { 1 + "" },
                    null, null, null);

            if (isDefault == 1 && cursor.getCount() > 0) {
                //?
                ContentValues cv1 = new ContentValues();
                cv1.put("is_default", 0);
                db.update(Constants.table.get(menuId), cv1, "is_default=?", new String[] { 1 + "" });
            }

            ContentValues cv = new ContentValues();
            cv.put("name", name);
            cv.put("tel", phone);
            cv.put("fax", fax);
            cv.put("email", email);
            cv.put("address", address);
            cv.put("is_default", isDefault);
            db.insert(Constants.table.get(menuId), null, cv);
            //??  
            db.close();

            Toast.makeText(getActivity(), "?!", Toast.LENGTH_SHORT).show();
        }
    });

    Button clearButton = (Button) view.findViewById(R.id.button_clear);

    clearButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText nameET = (EditText) view.findViewById(R.id.editText_com_name);
            EditText phoneET = (EditText) view.findViewById(R.id.editText_com_phone);
            EditText faxET = (EditText) view.findViewById(R.id.editText_com_fax);
            EditText emailET = (EditText) view.findViewById(R.id.editText_com_email);
            EditText addressET = (EditText) view.findViewById(R.id.editText_com_addr);
            CheckBox ckBox = (CheckBox) view.findViewById(R.id.checkBox_is_default);

            nameET.setText("");
            phoneET.setText("");
            faxET.setText("");
            emailET.setText("");
            addressET.setText("");
            ckBox.setChecked(false);
        }
    });

}

From source file:edu.uillinois.wseemann.uicombatschedule.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    caldroidFragment = new CustomCaldroidFragment();

    if (savedInstanceState != null) {
        caldroidFragment.restoreStatesFromKey(savedInstanceState, "CALDROID_SAVED_STATE");
    } else {/*from  w w w . j a v  a2  s. com*/
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);

        caldroidFragment.setArguments(args);
    }

    // Attach to the activity
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(android.R.id.content, caldroidFragment);
    t.commit();

    // Setup listener
    final CaldroidListener listener = new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
            String strDate = null;
            String info = null;

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int month = cal.get(Calendar.MONTH) + 1;
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int year = cal.get(Calendar.YEAR);

            String stringDate = month + "/" + day + "/" + year;

            Database database = new Database(MainActivity.this);
            SQLiteDatabase db = database.getReadableDatabase();

            Cursor cursor = db.query(Database.DATES_TABLE_NAME, null, Database.DATE + " = ?",
                    new String[] { stringDate }, null, null, null);

            if (cursor.moveToNext()) {
                strDate = cursor.getString(cursor.getColumnIndex(Database.DATE));
                info = cursor.getString(cursor.getColumnIndex(Database.INFO));
            }

            cursor.close();

            db.close();
            database.close();

            if (strDate != null && info != null) {
                ScheduleDialog dialog = ScheduleDialog.newInstance(strDate, info);
                dialog.show(getSupportFragmentManager(), "schedule");
            }
        }

        @Override
        public void onChangeMonth(int month, int year) {

        }

        @Override
        public void onLongClickDate(Date date, View view) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int month = cal.get(Calendar.MONTH) + 1;
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int year = cal.get(Calendar.YEAR);

            if (month == 7 && day == 5) {
                String text = getString(R.string.butts);
                // show the easter egg for Katie :)
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }

            mDate = date;
            startActionMode();
        }

        @Override
        public void onCaldroidViewCreated() {

        }

    };

    caldroidFragment.setCaldroidListener(listener);
}

From source file:com.dm.wallpaper.board.databases.Database.java

public int getCategoryCount(String category) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, "LOWER(" + KEY_CATEGORY + ") LIKE ?",
            new String[] { "%" + category.toLowerCase(Locale.getDefault()) + "%" }, null, null, null);
    int count = cursor.getCount();
    cursor.close();//from   w  ww . j  a  va  2  s.c  om
    db.close();
    return count;
}

From source file:com.rs.TCOfflineStatementCollection.java

/**
 * return a list of all local statements from db
 * @return List of LocalStatementsItem/*from   w ww  .  ja  v a  2 s . co  m*/
 */
public List<LocalStatementsItem> getCachedStatements() {
    List<LocalStatementsItem> statementArray = new ArrayList<LocalStatementsItem>();

    Cursor cursor;
    SQLiteDatabase database;
    TCLocalStorageDatabaseOpenHelper dbHelper;
    dbHelper = new TCLocalStorageDatabaseOpenHelper(appContext);
    database = dbHelper.getWritableDatabase();
    cursor = database.query(TCOfflineDataManager.LOCAL_STATEMENT_TABLE_NAME, null, null, null, null, null,
            LocalStatements.CREATE_DATE + " DESC"); //query for all the statements

    cursor.moveToFirst(); //go to the beginning of the query and then loop through all the packages, adding them to the return List
    while (!cursor.isAfterLast()) {
        LocalStatementsItem thisPackage = new LocalStatementsItem();
        thisPackage.id = cursor.getInt(0);
        thisPackage.statementId = cursor.getString(cursor.getColumnIndex("statementId"));
        thisPackage.statementJson = cursor.getString(cursor.getColumnIndex("statementJson"));
        thisPackage.createDate = cursor.getLong(cursor.getColumnIndex("createDate"));
        thisPackage.postedDate = cursor.getLong(cursor.getColumnIndex("postedDate"));
        thisPackage.querystring = cursor.getString(cursor.getColumnIndex("querystring"));

        statementArray.add(thisPackage);
        cursor.moveToNext();
    }

    cursor.close();
    database.close();

    return statementArray;
}

From source file:com.dm.wallpaper.board.databases.Database.java

public List<Category> getCategories() {
    List<Category> categories = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, null, null, null, null, KEY_NAME);
    if (cursor.moveToFirst()) {
        do {/*from w  w  w . j av a  2  s .  com*/
            Category category = new Category(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getInt(3) == 1, cursor.getInt(4) == 1, 0);
            categories.add(category);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return categories;
}

From source file:com.dm.wallpaper.board.databases.Database.java

public List<Wallpaper> getWallpapers() {
    List<Wallpaper> wallpapers = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, null, null, null, null, KEY_ADDED_ON + " DESC, " + KEY_ID);
    if (cursor.moveToFirst()) {
        do {//from w  w  w.ja v  a  2  s .co m
            Wallpaper wallpaper = new Wallpaper(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getString(4), cursor.getString(5));
            wallpapers.add(wallpaper);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

From source file:com.dm.wallpaper.board.databases.Database.java

public List<Wallpaper> getFavoriteWallpapers() {
    List<Wallpaper> wallpapers = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, KEY_FAVORITE + " = ?", new String[] { "1" }, null, null,
            KEY_ADDED_ON + " DESC, " + KEY_ID);
    if (cursor.moveToFirst()) {
        do {/*from  w ww. j  av  a  2s.  c om*/
            Wallpaper wallpaper = new Wallpaper(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4), cursor.getInt(6) == 1);
            wallpapers.add(wallpaper);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

From source file:com.dm.wallpaper.board.databases.Database.java

public List<Wallpaper> getFilteredWallpapers() {
    List<Wallpaper> wallpapers = new ArrayList<>();
    List<String> selected = getSelectedCategories(false);
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return wallpapers;

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        } else {/* w w  w  .j  a va  2  s  .  co  m*/
            CONDITION.append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        }
        selection.add("%" + item.toLowerCase(Locale.getDefault()) + "%");
    }
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, KEY_ADDED_ON + " DESC, " + KEY_ID);
    if (cursor.moveToFirst()) {
        do {
            Wallpaper wallpaper = new Wallpaper(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4), cursor.getInt(6) == 1);
            wallpapers.add(wallpaper);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

From source file:ru.valle.safetrade.BuyActivity.java

private void loadState(final long id) {
    cancelAllTasks();/* ww  w. j  a  v a2 s  . c o  m*/
    loadStateTask = new AsyncTask<Void, Void, TradeRecord>() {
        @Override
        protected TradeRecord doInBackground(Void... params) {
            SQLiteDatabase db = DatabaseHelper.getInstance(BuyActivity.this).getReadableDatabase();
            Cursor cursor = db.query(DatabaseHelper.TABLE_HISTORY, null, BaseColumns._ID + "=?",
                    new String[] { String.valueOf(id) }, null, null, null);
            ArrayList<TradeRecord> tradeRecords = DatabaseHelper.readTradeRecords(cursor);
            return tradeRecords.isEmpty() ? null : tradeRecords.get(0);
        }

        @Override
        protected void onPostExecute(TradeRecord tradeRecord) {
            tradeInfo = tradeRecord;
            loadStateTask = null;
            rowId = tradeRecord.id;
            encryptedPrivateKeyView.setText(tradeRecord.encryptedPrivateKey);
            addressView.setText(tradeRecord.address);
            confirmationCodeView.setText(tradeRecord.confirmationCode);
            intermediateCodeView.setText(tradeRecord.intermediateCode);
            MainActivity.updateBalance(BuyActivity.this, id, tradeInfo.address, new Listener<AddressState>() {
                @Override
                public void onSuccess(AddressState result) {
                    if (result != null) {
                        balanceView.setText(BTCUtils.formatValue(result.getBalance()) + " BTC");
                    }
                }
            });
        }
    };
    if (Build.VERSION.SDK_INT >= 11) {
        loadStateTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        loadStateTask.execute();
    }
}

From source file:syncthing.android.settings.AppSettings.java

public void removeCredentials(Credentials creds) {
    SQLiteDatabase _db = db.getWritableDatabase();
    Cursor c = null;// w ww. j  a v  a  2  s . c o m
    try {
        _db.beginTransaction();
        _db.delete(CredentialsDB.SCHEMA.TABLE, credentialsDeviceIdSel, new String[] { creds.id });
        c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, credentialsDefaultSel, null, null, null, null);
        if (c != null && c.getCount() == 0) {
            c.close();
            //no default set a new one
            c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, null, null, null, null, null);
            if (c != null && c.moveToFirst()) {
                ContentValues cv = new ContentValues();
                cv.put(CredentialsDB.SCHEMA.DEFAULT, 1);
                _db.update(CredentialsDB.SCHEMA.TABLE, cv, idSel, new String[] { c.getString(0) });
            }
        }
        _db.setTransactionSuccessful();
    } finally {
        _db.endTransaction();
        if (c != null)
            c.close();
    }
}