I got this :
Cursor c = db.query("Org", null, null, null, null, null, null);
which means I choose a table "Org", but together with this I need to make this :
Cursor c = db.rawQuery(" SELECT "+ id + " AS _id")
because SimpleAdapter need to have an _id field necessarily for some reason or it will crash with an error. How do I combine this 2 into one query?
The second parameter of the query function is the list of columns.
If you want to rename a column, you cannot just blindy return all columns but have to list the desired columns:
String[] columns = new String[] { id+" AS _id", "Name", "Color", "whatever..." };
Cursor c = db.query("Org", columns, null, null, null, null, null);
For your statement : Cursor c = db.query("Org", null, null, null, null, null, null); the second parameter is wrong, you shoukd mention the column names in it.
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Whereas for,
Cursor c = db.rawQuery(" SELECT "+ id + " AS _id from Org");
means that you Select id and create an alias of it using AS into _id, and you are selecting this id from Org table.
so now you will be able to access the result from this query from the column name _id, and in order to access the result use:
c.moveToFirst();
while (c.moveToNext())
{
System.out.println(c.getString(c.getColumnIndex("_id"));
}
Related
I am trying for select statement in android SQLite
i am able to select all records in a cursor if i do below
String tableName = "Test";
String[] columns = {mobile};//mobile has a datatype number in db
Cusrsor c = m_sqlLiteDatabase.query(tableName, columns, null,null, null, null, null);
c.getCount(); // 11
but does not return any value if i add a conditon as below
long mobile = 123456;
m_sqlLiteDatabase.query(tableName, columns, "mobile=" + mobile ,null, null, null, null);
c.getCount(); // 0
is anything i am doing wrong?
how to use long
try to change your query code line with this
m_sqlLiteDatabase.query(tableName, columns, " mobile = ? ", new String[]{String.valueOf(mobile)} ,null, null, null);
Reference
query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy)
TABLE_NAME (COL1, COL2, COL3, COL4);
Cursor cur = db.query(TABLE_NAME, null, null, null, null, null, COL1);
This way we get the entries ordered by COL1 values. If some of them has the same COL1 value SQLite uses the same order as when they were stored by the previous insertions.
Is it possible to define many degrees of sorting? For example, if two or more entries has the same COL1 value then the one with the smaller COL2 value will go first and so on.
Android site says that we can specify only a String (column) for the sorting.
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
If I use a
db.rawQuery("sql code")
the string of sql code will be compiled at runtime so the program will be slower. I would like to avoid that if not necessary. Are there any other ways?
With String orderBy you could specify multiple columns as this:
String order = COL1 + ", " +COL2 + ", " + COL3;
Cursor cur = db.query(TABLE_NAME, null, null, null, null, null, order);
I'm new to SQLite. I'm using this query in order to extract all rows from a column for a specific user:
Cursor c = db.query(true, TABLE, COLUMN, USER + "= '" + user + "'", null, null, null, null, null);
This is an example of table:
|group-----ID|
|------------|
|one-------me|
|one------you|
|two-------me|
|one-------me|
So the query for user "me" extracts "one, two, one". How can I filter the result in order to obtain a value just one time? Like "one, two"
Thanks
Use the Group By parameter with the appropriate column name:
Cursor c = db.query(true, TABLE, COLUMN, USER + "= '" + user + "'", null,
"group", null, null, null);
Since there are nine parameters in this method and three nearly identical methods...
public Cursor query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy, /* this one */
String having,
String orderBy,
String limit)
I'm trying to query only the first data from the table.
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null",
null, null, null, null);
Could somebody tell me how to implement a query where only the first data is retrieved from a table?
Solution
I think I solved the answer:
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null",
null, null, null, null , "1");
I used limit 1 first, but the application crashed. Only if I pass the number as a String will it work. The query has 8 parameters while we're using the limit parameter.
limit 1
From Documentation:
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
so, put your last argument to be "limit 1".
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null,
null, null, null, "limit 1");
I am trying to use this query upon my Android database, but it does not return any data. Am I missing something?
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String select = "Select _id, title, title_raw from search Where(title_raw like " + "'%Smith%'" +
")";
Cursor cursor = db.query(TABLE_NAME, FROM,
select, null, null, null, null);
startManagingCursor(cursor);
return cursor;
This will return you the required cursor
Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"},
"title_raw like " + "'%Smith%'", null, null, null, null);
Alternatively, db.rawQuery(sql, selectionArgs) exists.
Cursor c = db.rawQuery(select, null);
This will also work if the pattern you want to match is a variable.
dbh = new DbHelper(this);
SQLiteDatabase db = dbh.getWritableDatabase();
Cursor c = db.query(
"TableName",
new String[]{"ColumnName"},
"ColumnName LIKE ?",
new String[]{_data+"%"},
null,
null,
null
);
while(c.moveToNext()){
// your calculation goes here
}
I came here for a reminder of how to set up the query but the existing examples were hard to follow. Here is an example with more explanation.
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);
Parameters
table: the name of the table you want to query
columns: the column names that you want returned. Don't return data that you don't need.
selection: the row data that you want returned from the columns (This is the WHERE clause.)
selectionArgs: This is substituted for the ? in the selection String above.
groupBy and having: This groups duplicate data in a column with data having certain conditions. Any unneeded parameters can be set to null.
orderBy: sort the data
limit: limit the number of results to return
Try this, this works for my code
name is a String:
cursor = rdb.query(true, TABLE_PROFILE, new String[] { ID,
REMOTEID, FIRSTNAME, LASTNAME, EMAIL, GENDER, AGE, DOB,
ROLEID, NATIONALID, URL, IMAGEURL },
LASTNAME + " like ?", new String[]{ name+"%" }, null, null, null, null);