How can I get the minimum value of an database column?
I need to find the minimum _id of a SQLite table.
I tried the following, but had no success:
Cursor c = db.query(MY_DATABASE_TABLE_LAST_REQUEST, new String[] { "min(" + KEY_ROWID + ")" }, null, null,
null, null, null);
int rowID = c.getInt(0);
What am I doing wrong?
Make sure you call moveToFirst before getting the value:
Cursor c = db.query(MY_DATABASE_TABLE_LAST_REQUEST, new String[] { "min(" + KEY_ROWID + ")" }, null, null,
null, null, null);
c.moveToFirst(); //ADD THIS!
int rowID = c.getInt(0);
Related
Is there a way for selecting the rowid from table using SQLiteDatabase instance
Cursor websiteCursor= mDatabaseManipulator.query(mTableName, null, COLUMN_WEBSITENAME+"='"+websiteName+"'", null, null, null, null);
Above cursor doesn't get the rowid. The doc says passing null in second argument will return all columns. But rowid is not returned.
I m getting rowid using rawQuery.
Cursor websiteCursor = mDatabaseManipulator.rawQuery("Select rowid _id, * from " + mTableName + " where " + COLUMN_WEBSITENAME + " = '" + websiteName +"'", null);
I think there is some problem in the way that you are implementing the cursor, try this :-
Cursor websiteCursor= mDatabaseManipulator.query(mTableName, new String[]{rowid_id}, COLUMN_WEBSITENAME+" =? ", new String[]{websiteName}, null, null, null);
I want to fetch record according to item_name..but it gives sql exception no such column: Roti exist.
query of my code is there
db.query(TABLE_1, new String[] { KEY_ITEM_CALORIES }, KEY_ITEM_NAME + "=" + item_name, null, null, null, null, null);
whats is the problem in this query?
you can make like that
Cursor mCursor=db.query(tablename, new String[] {"columnname1","columnname2"},"KEY_ITEM_NAME=?",new String[] { item_name }, null, null,null);
Try this
`Cursor cursor = db.rawQuery
("select * from Table_Name where Column_Name ='"+ value +"'", null)
I am using a content provider and need to query a table where one column of the row = thing1 and another column of that same row = thing2.
i know you can query a table for just one condition like:
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "'", null, null);
now how can i query for two conditions? any help would be appreciated.
This should work:
Cursor c = contentResolver.query(content_uri, projection, "col1=? AND col2=?", args, null);
where args is a String[] of values to substitute in for the ? in the query.
Try this
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "' AND "+variable2 + "='"+thing2+"'" , null, null);
I am trying to build a method to determine if a user exists in an Android sqlite database before inserting, I have tried to build this method to return the number of rows, however If I try a search for like matches like this
String[] columns = new String[] { SCREEN_NAME, NAME, PROFILE_IMAGE_URL,
USER_ID };
Cursor c = friendsDatabase.query(DATABASE_TABLE, columns, SCREEN_NAME
+ " LIKE '" + screenName + "%'", null, null, null, null);
c.moveToFirst();
Integer count= c.getCount();
c.close();
return count;
I get the number of rows returned no problem however the minute I look for an exact match like this
String[] columns = new String[] { SCREEN_NAME, NAME, PROFILE_IMAGE_URL,
USER_ID };
Cursor c = friendsDatabase.query(DATABASE_TABLE, columns, SCREEN_NAME
+ " = '" + screenName + "'", null, null, null, null);
c.moveToFirst();
Integer count= c.getCount();
c.close();
return count;
I get 0 rows every time no matter if it matches or not, Im not sure if theres something wrong with my SQL or maybe im using the wrong arguments to make the statement, any help would go a long way thanks
Try this code it might work
Using Raw Query
Cursor mCount= db.rawQuery("select count(*) from "+ DATABASE_TABLE +" where "+SCREEN_NAME+"='" + SCREEN_NAME + "'", null);
mCount.moveToFirst();
int count= mCount.getInt(0);
mCount.close();
Or using the same code but differently
db.query(DATABASE_TABLE , columns, SCREEN_NAME=?, new String[] { SCREEN_NAME}, null, null, null);
c.moveToFirst();
Integer count= c.getCount();
c.close();
All the examples I see for doing a query show only using one key. Is it possible to use two keys. I have tried the code below and it does not work. I am trying to find the row where date and time match the date and time in the table
public String getPrimaryID(int date, int time )
{
Cursor c= db.query(DB_TABLE, new String[]{KEY_ROWID}, "date="+date+ "time=" + time, null, null, null, null);
int id=c.getColumnIndex(KEY_ROWID);
String result=c.getString(id);
return result;
}
I think you need to AND your query:
Cursor c= db.query(DB_TABLE, new String[]{KEY_ROWID}, "date="+date+ " AND time=" + time, null, null, null, null);
try this :
Cursor c = db.query(TABLENAME, null, "date = ? AND time = ?", new String[] { date, time }, null, null, null, null);
or :
String query ="SELECT * FROM " + TABLENAME + " " + "WHERE "
"date = ? " +
"AND time = ? ";
Cursor c = db.rawQuery(query, new String[] {date, time});
Explanation : variables date and time will replace the ? characters on the query