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});
Related
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);
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);
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'm trying to learn how to use database in Android and so far I have learned how to fetch all rows of data in the table, but I'm not sure how I'm going to use a SQL query to search for a value that is equal to the string value I pass to the method below?
I wonder if someone could add some simple code how to query the database with SQL in my code? Preciate the help! Thanks!
Something like this: String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH = filePath";
// Read from database
public String readContacts(String filePath){
String[] columns = new String[]{TABLE_ID, TABLE_IMAGE_PATH, TABLE_CONTACT_NAME, TABLE_CONTACT_NUMBER, TABLE_CONTACT_ADDRESS};
Cursor c = db.query(DB_TABLE, columns, null, null, null, null, null);
int id = c.getColumnIndex(TABLE_ID);
int imagePath = c.getColumnIndex(TABLE_IMAGE_PATH);
int name = c.getColumnIndex(TABLE_CONTACT_NAME);
int number = c.getColumnIndex(TABLE_CONTACT_NUMBER);
int address = c.getColumnIndex(TABLE_CONTACT_ADDRESS);
String result = "";
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(id) + " " + c.getString(imagePath) + " " + c.getString(name) + " " + c.getString(number) + " " + c.getString(address);
}
return result;
}
You can search Query
its for exact string
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '"+filePath+"';
if value contains your string
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '%"+filePath+"%';
if value needs from last
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '%"+filePath+"';
if value needs from first
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '"+filePath+"%';
put these
SQLiteDatabase dbR = this.getReadableDatabase();
Cursor cur = dbR.rawQuery("query", null);
return cur
using this cursor you can get value.
you can do that easily by modifying your query code to:
String[] columns = new String[]{TABLE_ID, TABLE_IMAGE_PATH, TABLE_CONTACT_NAME, TABLE_CONTACT_NUMBER, TABLE_CONTACT_ADDRESS};
Cursor cursor = db.query(ANSWERS_TABLE,
columns,
columns[1] + " = ?", new String[]{filePath}, null, null, null);
Take a look here : SQLiteDataBase Query
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