I am not having a lot of luck figuring out how to query with an int and a string. I have found examples with two ints and I have found examples with two strings. I have tried different combinations and nothing seems to work. Below are two examples of what I have tried and have not worked. What am I doing wrong. I have used both inpspection_id and item_name on there own and they both work.
Try 1:
public int getId(int inspection_id ,String item_name)throws Exception
{
Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME},
"inspection_id=" + inspection_id +"item_name='"+ item_name + "'", null, null, null, null);
int id=c.getColumnIndex(KEY_ROWID);
c.moveToFirst();
String result=c.getString(id);
int primId=Integer.parseInt(result);
c.close();
return primId;
}
Try 2:
public int getId(int inspection_id ,String item_name)throws Exception
{
Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME},
"inspection_id=" + inspection_id +"AND KEY_ITEM_NAME = ?", new String[]{item_name}, null, null, null);
int id=c.getColumnIndex(KEY_ROWID);
c.moveToFirst();
String result=c.getString(id);
int primId=Integer.parseInt(result);
c.close();
return primId;
}
Try 3:
public int getId(int inspection_id ,String item_name)throws Exception
{
String s_id= Integer.toString(inspection_id);
Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME},
"KEY_INSPECTION_ID = ? AND KEY_ITEM_NAME = ?",
new String[] { s_id, item_name }, null, null, null);
int id=c.getColumnIndex(KEY_ROWID);
c.moveToFirst();
String result=c.getString(id);
int primId=Integer.parseInt(result);
c.close();
return primId;
}
try it this way, with single quotes for the int ones.
"inspection_id='" + inspection_id +"'item_name='"+ item_name + "'"
Related
here is my code for using that string
String item = item1.getText().toString();
item = item.toLowerCase();
String date = getDate();
Datahelper edited = new Datahelper(this);
edited.open();
String returnedprice = edited.getprice(item,date);
String returneddetail = edited.getdetail(item,date);
edited.close();
price.setText(returnedprice);
details.setText(returneddetail);
and this is my method in sqlite
public String getprice(String item ,String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item,date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
String price = c.getString( c.getColumnIndex(KEY_PRICE));
return price;
}
return null;
}
public String getdetail(String item, String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item, date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
return detail;
}
return null;
}
my app gets crash when using this code,, i dont know waht's wrong in the code if anyone need i can post the whole code pls help me
the column index has nothing in common with the returned set:
String price = c.getString( c.getColumnIndex(KEY_PRICE));
in c you have only 5 columns (0 to 4) ( String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS}; ) and c.getColumnIndex(KEY_PRICE) return the actual index in the table which is probably higher than 4.
instead of
String price = c.getString( c.getColumnIndex(KEY_PRICE));
try
String price = c.getString(3);
the corect code is -
public String getprice(String item ,String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item,date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
c.moveToFirst();
String price = c.getString( c.getColumnIndex(KEY_PRICE));
return price;
}
return null;
}
public String getdetail(String item, String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item, date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
c.moveToFirst();
String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
return detail;
}
return null;
}
why does when i use a string as a where condition in my database, my program just force close. though it is working fine if i use number as a query condition. please help me, thanks
public ArrayList<Contact> getAvailableList()
{
// TODO Auto-generated method stub
ArrayList<Contact> results = new ArrayList<Contact>();
String[] columns = new String[]{KEY_NAME, KEY_NUMBER, KEY_STATUS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_STATUS +"=available" , null, null, null, KEY_NAME);
String sName = "";
String sNum = "";
String status = "";
int iName = c.getColumnIndex(KEY_NAME);
int iNumber = c.getColumnIndex(KEY_NUMBER);
int iStatus = c.getColumnIndex(KEY_STATUS);
Contact contact;
for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext())
{
contact = new Contact();
sName += c.getString(iName);
sNum += c.getString(iNumber);
status += c.getString(iStatus);
contact.setName(sName);
//contact.setPhoneNumber(sNum);
contact.setPhoneNumber("0".concat(sNum));
contact.setStatus(status);
results.add(contact);
sName = "";
sNum = "";
status = "";
}
return results;
}
You need to wrap strings in SQL with quotes, like this:
// Add these v v
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_STATUS +"='available'",
null, null, null, KEY_NAME);
Or if you want to use dynamic data, you should use the selectionArgs parameter:
String status = "available";
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_STATUS +"=?",
new String[] {status}, null, null, KEY_NAME);
This approach simplifies keeping track of matching quotes in complex Java/SQL Strings, more importantly it protect you from SQL injections.
You forgot ' ' around available
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_STATUS +"='available'" , null, null, null, KEY_NAME);
I'm a noob to android and im am trying to query a sqlite table. I have read several tutorials and know how to query a table to return an entire column of values, but i can't seem to figure out how to query a just a single value from a column by string. Any help is greatly appreciated. here is my code:
My successful code to query and return values for entire column:
public String getValue() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iValue) + "\n";
}
return result;
}
My attempt to query column for single value by string:
public String getSingleValue(String aCoin) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
if (c != null){
c.moveToFirst();
result= c.getString(1);
return result;
}
return null;
}
Change this
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
to
String[] columns = new String[]{ KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
String result = "";
if (c != null){
c.moveToFirst();
result= c.getString(0);
return result;
}
return null;
If you want to only get the name column.
I have a problem with "last_insert_rowid()".
In my DB-Helper-Class I am doing the following:
public int getLastID() {
final String MY_QUERY = "SELECT last_insert_rowid() FROM "+ DATABASE_TABLE5;
Cursor cur = mDb.rawQuery(MY_QUERY, null);
cur.moveToFirst();
int ID = cur.getInt(0);
cur.close();
return ID;
}
But when I call this in my intent:
int ID = mDbHelper.getLastID();
Toast.makeText(this, "LastID: " + ID, Toast.LENGTH_SHORT).show();
I get always "0" back. That can not be, cause I have a lot of entries in my DB and the autoincrement value should be much higher.
What I am doing wrong?
OK, that was the right hint John ;-)
The query should look like this:
public int getHighestID() {
final String MY_QUERY = "SELECT MAX(_id) FROM " + DATABASE_TABLE5;
Cursor cur = mDb.rawQuery(MY_QUERY, null);
cur.moveToFirst();
int ID = cur.getInt(0);
cur.close();
return ID;
}
You don't need to put table name with last_insert_rowid() it will automatically select last inserted id from session. so you can just simply use this function as well.
public int getHighestID() {
final String MY_QUERY = "SELECT last_insert_rowid()";
Cursor cur = mDb.rawQuery(MY_QUERY, null);
cur.moveToFirst();
int ID = cur.getInt(0);
cur.close();
return ID;
}
I have to following code. I want this to return an array e.g. arg[] that contains at arg[0] the number of the rows of my cursor and at arg[1] String(0) of my cursor. Since one is integer and the other is string I have a problem. Any ideas how to fix this?
public String[] getSubcategoriesRow(String id){
this.openDataBase();
String[] asColumnsToReturn = new String[] {SECOND_COLUMN_ID,SECOND_COLUMN_SUBCATEGORIES,};
Cursor cursor = this.dbSqlite.query(SECOND_TABLE_NAME, asColumnsToReturn, SECOND_COLUMN_SUBCATEGORIES + "= \"" + id + "\"", null, null, null, null);
String string = cursor.getString(0);
int count = cursor.getCount();
String arg[] = new String[]{count, string};
cursor.close();
return arg;
}
The cursor and the results and correct i just need to compine them to an array in order to return that.
Either:
Use an Object[] (an object array) instead of a String[] — not recommended, or
Create a new, meaningful class to hold the data.
public Subcategory getSubcategoriesRow(String id){
this.openDataBase();
String[] asColumnsToReturn = new String[] {SECOND_COLUMN_ID,SECOND_COLUMN_SUBCATEGORIES,};
Cursor cursor = this.dbSqlite.query(SECOND_TABLE_NAME, asColumnsToReturn, SECOND_COLUMN_SUBCATEGORIES + "= \"" + id + "\"", null, null, null, null);
String string = cursor.getString(0);
int count = cursor.getCount();
Subcategory toReturn = new Subcategory(count, string);
cursor.close();
return toReturn;
}