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);
Related
I want to retrieve a specific user from a SQLite database on Android with a provided username using a Cursor. I've tried this:
String[] fields = new String[] { "username", "email", "dateRegister" };
Cursor c = db.query(tableName, fields, "username ='1234'", null);
But it isn't working. How can I retrieve a specific row with the information of a unique column?
try this
db.query(tableName, null, "username = ?", new String[]{"username"}, null, null, null);
lots of guys asked the same question ,so please search efficient.
String query = "select * from " + tableName + " where "+ KEY_USERNAME + " = '" + uname + "'";
SQLiteDatabase sql = this.getReadableDatabase();
Cursor cur = sql.rawQuery(query, null);
return cur;
Please find what is wrong here
here _id and chek is the column of my db i to write a query to search those which has id =id and check =number
public Cursor query(int id){
return myDataBase.query("question", null,"_id = "+id+ "AND " + "chek ="+number,null, null, null, null);
}
May this help you:
Replace your query with these lines..
public Cursor query(int id){
return myDataBase.query("question", null,"_id = "+id+ " AND " + "chek ="+number,null, null, null, null);
keep space between the double quotes in And :" And " otherwise the string will be Example: _id=3And check =9
Firstly check your sql query syntax are right or not
You can use raw query
String SQL= "select * from question where _id = "+id+ " and chek ="+number+";
Log.d("SQL::", SQL);
Cursor cursor = db.rawQuery(SQL, null);
try
return myDataBase.query("question", null,"_id = "+id+ " AND " + "chek ="+number,null, null, null, null);
Replace your query with these lines if not yet solved..
public Cursor query(int id){
return myDataBase.query("question", null,"(_id = "+id+ " AND " + "chek ="+number+")",null, null, null, 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);
My cursor is returning records twice even though I set the distinct to true:
return myDataBase.query(true, DB_TABLE, new String[]
{"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
FYI,
public Cursor getData(String[] symptoms) {
String where = KEY_SYMPTOMS + "= ?";
String orStr = " OR ";
StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
builder.append(orStr).append(where);
return myDataBase.query(true, DB_TABLE, new String[]
{"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
}
Or I tried to change to rawQuery
return myDataBase.rawQuery("SELECT DISTINCT " + KEY_CONDITIONS + " FROM "
+ DB_TABLE + " " + builder.toString() + symptoms.toString(), null);
My LogCat says:
03-02 22:57:02.634: E/AndroidRuntime(333): FATAL EXCEPTION: main
03-02 22:57:02.634: E/AndroidRuntime(333): android.database.sqlite.SQLiteException: near "=": syntax error: , while compiling: SELECT DISTINCT conditions FROM tblSymptoms symptoms= ? OR symptoms= ?[Ljava.lang.String;#405550f8
Please help me identify what seems to be missing in here. Any help is truly appreciated.
Solution
You want DISTINCT conditions but Android requires the _id column which is a problem because you cannot mix and match: SELECT _id, DISTINCT condition.... However you can use the GROUP BY clause instead:
return myDataBase.query(DB_TABLE, new String[] {"rowid as _id", KEY_CONDITIONS},
builder.toString(), symptoms, KEY_CONDITIONS, null, null);
Explanations
This query:
return myDataBase.rawQuery("SELECT DISTINCT " + KEY_CONDITIONS + " FROM "
+ DB_TABLE + " " + builder.toString() + symptoms.toString(), null);
Failed because you are passing String[] symptoms in the wrong parameter, try:
return myDataBase.rawQuery("SELECT DISTINCT " + KEY_CONDITIONS + " FROM "
+ DB_TABLE + " " + builder.toString(), symptoms);
This query:
return myDataBase.query(true, DB_TABLE, new String[] {"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
Failed because DISTINCT is looking at both the id and condition columns. It is the equivalent of: SELECT DISTINCT(_id, conditions) ... You, obviously, only want distinct conditions...
you have two general option two do this Task
use row query **and
**use DISTINCT keyword
for using row query you can directly use function of mr.Sam
and for using DISTINCT keyword you can use
public Cursor query (boolean distinct, String table, String[] columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)
because query stucher is retunsthis:
query(false, table, columns, selection, selectionArgs, groupBy,
having, orderBy, null /* limit */);
in this code first arg is for DISTINCT so provide it as true.
so your query will like this
Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN1 ,COLUMN2, COLUMN_NAME_3 }, null, null, COLUMN2, null, null, null);
BasicallyDISTINCT keyword returns a column name only once if it apperase more then one time.
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