String[] whereArgs parameter of database method in android - android

delete(String table, String whereClause, String[] whereArgs)
update(String table, ContentValues values, String whereClause, String[] whereArgs)
What is this String[] whereArgs? Is it connected with "?" (so called wild card)?

Yes, the String[] whereArgs contains the arguments to be appended to the whereClause.
For example, you want to make a delete query:
delete from InfoTable where name = "ABC" and id = "23"
then the query should be:
delete("InfoTable", "name = ? AND id = ?" , new String[] {"ABC", "23"});

Related

Passing parameters into SQLite querys [duplicate]

I am using the query method of SQLiteDatabase. How do I use the query method?
I tried this:
Cursor cursor = sqLiteDatabase.query(
tableName, tableColumns, whereClause, whereArgs, groupBy, having, orderBy);
tableColumns - columns parameter is constructed as follows.
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
If we need to get all the fields, how should the column parameter to be constructed. Do we need to include all the Field Names in String array?
How do I properly use the query method?
tableColumns
null for all columns as in SELECT * FROM ...
new String[] { "column1", "column2", ... } for specific columns as in SELECT column1, column2 FROM ... - you can also put complex expressions here:
new String[] { "(SELECT max(column1) FROM table1) AS max" } would give you a column named max holding the max value of column1
whereClause
the part you put after WHERE without that keyword, e.g. "column1 > 5"
should include ? for things that are dynamic, e.g. "column1=?" -> see whereArgs
whereArgs
specify the content that fills each ? in whereClause in the order they appear
the others
just like whereClause the statement after the keyword or null if you don't use it.
Example
String[] tableColumns = new String[] {
"column1",
"(SELECT max(column1) FROM table2) AS max"
};
String whereClause = "column1 = ? OR column1 = ?";
String[] whereArgs = new String[] {
"value1",
"value2"
};
String orderBy = "column1";
Cursor c = sqLiteDatabase.query("table1", tableColumns, whereClause, whereArgs,
null, null, orderBy);
// since we have a named column we can do
int idx = c.getColumnIndex("max");
is equivalent to the following raw query
String queryString =
"SELECT column1, (SELECT max(column1) FROM table1) AS max FROM table1 " +
"WHERE column1 = ? OR column1 = ? ORDER BY column1";
sqLiteDatabase.rawQuery(queryString, whereArgs);
By using the Where/Bind -Args version you get automatically escaped values and you don't have to worry if input-data contains '.
Unsafe: String whereClause = "column1='" + value + "'";
Safe: String whereClause = "column1=?";
because if value contains a ' your statement either breaks and you get exceptions or does unintended things, for example value = "XYZ'; DROP TABLE table1;--" might even drop your table since the statement would become two statements and a comment:
SELECT * FROM table1 where column1='XYZ'; DROP TABLE table1;--'
using the args version XYZ'; DROP TABLE table1;-- would be escaped to 'XYZ''; DROP TABLE table1;--' and would only be treated as a value. Even if the ' is not intended to do bad things it is still quite common that people have it in their names or use it in texts, filenames, passwords etc. So always use the args version. (It is okay to build int and other primitives directly into whereClause though)
This is a more general answer meant to be a quick reference for future viewers.
Example
SQLiteDatabase db = helper.getReadableDatabase();
String table = "table2";
String[] columns = {"column1", "column3"};
String selection = "column3 =?";
String[] selectionArgs = {"apple"};
String groupBy = null;
String having = null;
String orderBy = "column3 DESC";
String limit = "10";
Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
Explanation from the documentation
table String: The table name to compile the query against.
columns String: A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data
from storage that isn't going to be used.
selection String: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing
null will return all rows for the given table.
selectionArgs String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they
appear in the selection. The values will be bound as Strings.
groupBy String: A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null
will cause the rows to not be grouped.
having String: A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING
clause (excluding the HAVING itself). Passing null will cause all row
groups to be included, and is required when row grouping is not being
used.
orderBy String: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the
default sort order, which may be unordered.
limit String: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Where clause and args work together to form the WHERE statement of the SQL query. So say you looking to express
WHERE Column1 = 'value1' AND Column2 = 'value2'
Then your whereClause and whereArgs will be as follows
String whereClause = "Column1 =? AND Column2 =?";
String[] whereArgs = new String[]{"value1", "value2"};
If you want to select all table columns, i believe a null string passed to tableColumns will suffice.
if your SQL query is like this
SELECT col-1, col-2 FROM tableName WHERE col-1=apple,col-2=mango
GROUPBY col-3 HAVING Count(col-4) > 5 ORDERBY col-2 DESC LIMIT 15;
Then for query() method, we can do as:-
String table = "tableName";
String[] columns = {"col-1", "col-2"};
String selection = "col-1 =? AND col-2=?";
String[] selectionArgs = {"apple","mango"};
String groupBy =col-3;
String having =" COUNT(col-4) > 5";
String orderBy = "col-2 DESC";
String limit = "15";
query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
db.query(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
TABLE_ROW_ID + "=" + rowID, here = is the where clause. To select all values you will have to give all column names:
or you can use a raw query like this
db.rawQuery("SELECT * FROM permissions_table WHERE name = 'Comics' ", null);
and here is a good tutorial for database.

what is the better way to handle the columns to return variable for SQLite database Android

this database method is returning only the string name from the KEY_NAME column of the database. the other two columns are KEY_ROWID and KEY_HITS.
is it better to make the columns to return array variable like this;
String[] columns = new String[] { KEY_NAME };
because the only the column KEY_NAME is needed, or is it better to always put all the columns in like this;
String[] columns = new String[] {KEY_ROWID, KEY_NAME, KEY_HITS };
full code for the database method shown below;
public String getName(long l){
String[] columns = new String[] {KEY_ROWID, KEY_NAME, KEY_HITS };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID +"="+ l, null, null, null, null);
if(c!= null){
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
EDIT;
after some testing I found out that I can return only the single column name "KEY_NAME" if I am using this;
total = total + c.getInt(c.getColumnIndex(KEY_NAME)); // only need { KEY_NAME };
however, when I use the following code to use a number to get column position "c.getInt(2)" it does not work unless I return all three column names
total = total + c.getInt(2); // requires {KEY_ROWID, KEY_NAME, KEY_HITS };
Why do you want to get data that you don't need.
If you only want to get one column , query the database for that column only, and keep doing so as long as it works for you.
For performance reasons, you should try to fetch as less column as necessary.
However, if you are using an CursorAdapter, you need the id column.

Edit SQLite database record

How can I edit a particular record in a SQLite table given, Database Name, Table Name, int _id ,Column Name and Desired value of record?
EDIT:
My solution was database.update(DataBaseHelper.VFS_DATABASE_TABLE, values, "_id=?", new String[] {id+""});
public int deleteCpShadowEntryById (int id) {
int delRows = mDb.delete(<your_table_name>, "_id" + "=?", new String[] {String.valueOf(id)});
return delRows;
}
But I guess you do not want to delete. You want to update your value. So in this case you should at first select your row, fill the values of this row into a new ContentValues, replace you old value with new and update the row.
Edit: Maybe try this:
ContentValues args = new ContentValues();
args.put(<your_columnName>, newValue);
db.update(<your_table_name>, args, "_id" + "=?", new String[] {String.valueOf(id)});
Should be a simple bit of SQL:
DELETE FROM <table name> WHERE <column name> = <desired value>
SQLiteDatabase has a delete method :
public int delete (String table, String whereClause, String[] whereArgs)
If you have id of the particular record the code would be something like :
String whereArgs[] = new String { id };
db.delete(TABLE_NAME, "_id = ?", whereArgs);

How to use SQLite's "whereClause" inside update method

I'm dealing with an sqlite db in android application.
To update a row in the db I use method of SQLiteDatabase object "update"
SQLiteDatabase.update(String table, ContentValues values, String whereClause, String[] whereArgs)
But here a confusion, how "whereClause" should look like?
Assume I have these values for update.
values.put("name", appInfo.getName());
values.put("package_name", appInfo.getPackageName());
values.put("version_name", appInfo.getVersionName());
I want to update a row where package_mane column equal to "com.mynamespace.db". How "whereClause" should be written here? Thanks.
db.update("table", values, "package_name = ?", new String[]{"com.mynamespase.db"});
Another example:
db.update("table", values, "package_name = ? and other_field = ?", new String[]{"com.mynamespase.db", "test"});
You whereClause should look like this:
String whereClause = "package_mane = ?"; // package_name perhaps?
String[] whereArgs = new String[]{"com.mynamespase.db"};
String from whereArgs are used to substitute ? in whereClause.
You should use it like this:
SQLiteDatabase.update(table, "package_name=?", new String[] { "com.mynamespace.db" });
and prevent other problems and cache inefficiencies if you concat the string.
db.update(TABLE_NAME, values, "package_name=com.mynamespase.db", null);
// or
String[] args = { "com.mynamespase.db" };
db.update(TABLE_NAME, values, "package_name=?", args);

How to get a cursor with distinct values only?

I want to return a cursor only with distinct values of a column.
The column 'Groups' has more items but with only 2 values: 1,2,1,1,1,2,2,2,1
String[] FROM = {Groups,_ID};
public Cursor getGroups(){
//......
return db.query(TABLE_NAME,FROM,null,null,null,null,null);
}
will return a cursor containing {1,2,1,1,1,2,2,2,1} but I would like to contain just {1,2}.
You can have an sql query like this,
public Cursor usingDistinct(String column_name) {
return db.rawQuery("select DISTINCT "+column_name+" from "+TBL_NAME, null);
}
you can use distinct argument while making query like this:
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Follow this doc for more clearity.
You can use below example query, as you have to give column name for distinct
Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN_NAME_1 ,COLUMN_NAME_2, COLUMN_NAME_3 }, null, null, COLUMN_NAME_2, null, null, null);
COLUMN_NAME_2 - name of the column for distinct.
remember to add GROUP BY column names
Use boolean true iin the distinct argument, For example :
public Cursor query (**true**, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);

Categories

Resources