I fail queries with arguments, What am I doing wrong?
I need the following query:
cursor = bd.rawQuery("SELECT * FROM user WHERE user.name=?", new String[]{"%"+nombre+"%"});
I tried the following and they also fail:
cursor = bd.rawQuery("SELECT * FROM user WHERE name=?", new String[]{"%"+nombre+"%"});
cursor = bd.rawQuery("SELECT * FROM user WHERE user.name=?", new String[]{nombre});
This works:
cursor = bd.rawQuery("SELECT * FROM user ", null);
Thanks for your help
As Mike said in your comments
cursor = bd.rawQuery("SELECT * FROM user WHERE name LIKE ? ", new String[]{"%" + nombre + "%"});
or if you really wanna use like this:
cursor = bd.rawQuery("SELECT * FROM user ", null); where second parameter is null.
use escaped string instead:
cursor = bd.rawQuery("SELECT * FROM user WHERE name LIKE '%" + nombre.replaceAll("'","''") + "%' ", null );
or use GLOB like here described.
Please try this, List out users with name foo.
cursor = bd.rawQuery("SELECT * FROM user WHERE name = ?", new String[] {"foo"});
OR
cursor = bd.rawQuery("SELECT * FROM user WHERE name LIKE ?", new String[] {"foo"});
SQLiteDatabase rawQuery document reference
try this
cursor = bd.rawQuery("SELECT * FROM user WHERE name= '%"+ nombre + "%'", null);
Related
I am selecting row based on user id but non is returning
select all is working and i am sure of the id in the where
can somone help me what is wrong with the query
String sql = "SELECT * FROM " + TABLE_Txns
+ " WHERE " + COLUMN_USERID + " = ?";
SQLiteDatabase db = this.getReadableDatabase();
List<TransactionModel> savedTransactions = new ArrayList<>();
Cursor cursor = db.rawQuery(sql, null
, new String[]{getLogin_id()});
Use rawQuery() this way:
Cursor cursor = db.rawQuery(sql, new String[]{getLogin_id()});
The 2nd parameter is the selection arguments.
The signature you used with 3 parameters is this:
rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal)
and you don't need that.
I have this content resolver query which translates to select * from book where favflag = 1.
But now I want to change it so that it translate to select * from book where favflag = 1 AND bookid = 2;
How do I achieve this?
Here is the content resolver query:
Cursor BookCursor = getActivity().getContentResolver().query(
BookContract.BookEntry.CONTENT_URI,
null,
BookContract.BookEntry.COLUMNFAVFLAG + " = ?",
new String[]{"1"},
null);
Since there is no BookContract in the Android SDK that I am aware of, I assume this is your own class. In that case, we do not know the API of that class.
With that in mind, try:
Cursor BookCursor = getActivity().getContentResolver().query(
BookContract.BookEntry.CONTENT_URI,
null,
BookContract.BookEntry.COLUMNFAVFLAG + " = ? AND "+BookContract.BookEntry.COLUMNID + " = ?",
new String[]{"1", "2"},
null);
Substitute in the proper value for BookContract.BookEntry.COLUMNID.
Try this
Cursor BookCursor = getActivity().getContentResolver().query(
BookContract.BookEntry.CONTENT_URI,
null,
BookContract.BookEntry.COLUMNFAVFLAG + " = ? AND "+BookContract.BookEntry.COLUMNBOOKID + " = ?", // Use your variable here in place of COLUMNBOOKID
new String[]{"1", "2"},
null);
how to write
select * from customerdetails where locality = " something"
in the form of
"Select * from customerdetails where locality =?" + locality + ";";
in this case lacality is variable
Anyone can please solve my problem
Thanks in advance
Cursor c = db.query("customerdetails", null, "locality =?", new String[]{locality}, null, null, null);
or you can use raw query like
db.rawQuery("Select * from customerdetails where locality = ? ", new String[]{locality});
I am trying to execute a query to retrieve some data from the table.
Below is my query:
cursor = myDB.rawQuery("SELECT * FROM myTable WHERE myNumber="+"'myCustomNumber'", null);
where myNumber is type of 'TEXT'
But every time I am getting cursor.getCount() = 0;
It seems there's a mistake in my query.
Please let me know the correct way of writing above query.
In your query the parameter passed as myNumber has become a text due the double quotes around it.
Change it to,
cursor = myDB.rawQuery("SELECT * FROM myTable WHERE myNumber = ' " + myCustomNumber + "'", null);
I think you should be use below code :
cursor = myDB.rawQuery("SELECT * FROM myTable WHERE myNumber='"+myCustomNumber+"'", null);
i hope it will work for you.
Try this
String phoneNo = "0000000000";
String query = "Select * from where mynumber = '" +phoneNo+ "'";
cursor = myDB.rawQuery(query,null);
you can execute that query i.e.
rawQuery("SELECT * FROM myTable WHERE myNumber= = ? ", new String[] {"myCustomNumber"});
In the query that you have used, the number has not been passed in quotes.
cursor = myDB.rawQuery("SELECT * FROM myTable WHERE myNumber="+"'myCustomNumber'", null);
That query would mean myNumber= myCustomNumber literally, and not a variable value.
You need to add the quotes before and after your myCustomNumber like:
cursor = myDB.rawQuery("SELECT * FROM myTable WHERE myNumber="+"'"+myCustomNumber+"'", null);
My question is why this query does not work?
Cursor c = db.rawQuery("SELECT * FROM tbl_staff WHERE PMajor = '%" + spin.getSelectedItem().toString() + "%'", null);
Cursor c: it is a cursor for handling my query
tbl_staff: my table that consist of PName, PMajor, PCert
spin: is spinner that has values which I need for my database query.
When I use:
if (c.moveToNext())
else (log.d("error query","couldn't do the query!");)
It goes to else statement and moveToNext() doesn't work.
Instead of using =, which checks for equality, use keyword LIKE, which matches a pattern.
Cursor c = db.rawQuery("SELECT * FROM tbl_staff WHERE PMajor LIKE '%" + spin.getSelectedItem().toString() + "%'", null);