Menus

Sunday, 19 February 2017

Android - SQLite Tips




Android sqlite check if inserted new value


insert() method returns the row ID of the newly inserted row, or -1 if an error occurred.
Like this:
long rowInserted = db.insert(AddNewPhysicalPerson, null, newValues);
if(rowInserted != -1)
    Toast.makeText(myContext, "New row added, row id: " + rowInserted, Toast.LENGTH_SHORT).show();
else
    Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show();

Called from main activity for save item record to SQLite table and return status true or false.

public boolean insertItem(HashMap<String, String> queryValues) {

    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();


    values.put("edate",queryValues.get("edate"));
    values.put("item",queryValues.get("item"));
    values.put("amount",queryValues.get("amount"));
    values.put("number",queryValues.get("number"));
    values.put("count",queryValues.get("count"));

        long rowInserted = database.insert("customerentries", null, values);

        if(rowInserted != -1) {
            database.close();
            return true;
        }
        else{
            database.close();
            return false;
        }

}


Click Here




Android SQLite find for max value in a field


How to get the Max value SQLite Android



The SQLite MAX function is an aggregate function that returns the maximum value of all values in a group. 


    public int getMaxid(){

        String selectQuery = "SELECT max(id) as id FROM customerentries";
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);

        cursor.moveToFirst();

        int maxid = cursor.getInt(cursor.getColumnIndex("id"));

        return maxid;
       
    }

Solution for - 

How to find the maximum value of the column in sqlite database



Related Links




No comments:

Post a Comment