Get specific data from sqlitedatabase using row id - android

I have two columns in a database, id and city. How can I get all the city data and put it in a string when the row id is less than 3. I am having difficulty in writing a query to get data when the row id is less than 3. Right now my method gets all the data from the database. Thanks.
String[] columns = new String[]{KEY_ROWID, KEY_CITY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result =" ";
int iRow = c.getColumnIndex(KEY_ROWID);
int iCity = c.getColumnIndex(KEY_CITY);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iCity) + "\n";
}
return result;

String[] columns = new String[]{KEY_ROWID, KEY_CITY};
// "note "rowid" is used in fts tables and "id" in normal tables.
// I'm assuming your constant KEY_ROWID chose the correct one.
String where = KEY_ROWID + " <= " + 3;
Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, where, null, null, null, null);
if(!cursor.moveToFirst()){
Log.e("System.out", "cursor is empty");
}else{
int ix_city = cursor.getColumnIndexOrThrow(KEY_CITY);
StringBuilder sb = new StringBuilder();
do{
String city = cursor.getString(ix_city);
Log.i("System.out", "city name " + city);
if(TextUtils.isEmpty(city))
continue;
sb.append(city).append("\n");
}while(cursor.moveToNext());
Log.i("System.out", "the entire list " + sb.toString());
}

Related

how to get a specific data by using a string argument?

i am using this method to get the price of all item that has a category name value bt it is not showing anything...
public long getcostmain(String xyz)throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + xyz, null, null, null, null);
long cost = 0;
for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()){
cost = cost + c.getLong(3);
}
return cost;
}
In your code, when you query data from your database, you need to change your code to following:
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "='" + xyz +"'", null, null, null, null);
You need to put ' mark around your xyz string.

Is it possible to set column width in SQLite table?

I am trying to display a SQLite table in a view. I want my first column to have a width of 20 characters. I have attempted to achieve this effect by using the substring method but I get stringindexoutofboundsexceptions with strings less than 20 characters. Any help is greatly appreciated. Here is my code:
Method for displaying table in view:
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, 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 iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iName).substring(0, 20) + " " + c.getString(iQuantity) + " " + c.getString(iValue) + "\n";
}
Just use c.getString(iName).substring(0, Math.min(20, c.getString(iName).length()) (possibly using a temporary for c.getString(iName)) instead of the substring you have now.
If you don't want them then why select. You can just do
String[] columns = new String[]{ KEY_ROWID, "substr(" + KEY_NAME + ",1,20)", KEY_QUANTITY, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
and get at most 20 chars from the column.

How can I put a string and an integer into the same array?

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;
}

how to get the id of the given row text in android from database

Hello friends i am new to android.well i have created a database table in that i have few rows as follows
_id name city
1 maddy0 xyz0
2 maddy1 xyz1
3 maddy2 xyz2
now what i want to pass a city to the function and get the row id of that city
say i pas xyz0 then it should give me that out put as 1 .for that i tried this
public String getDatatonumber(String s) {
Cursor c = null;
String result = new String();
// TODO Auto-generated method stub
// String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE};
try {
c = ourDatabase.rawQuery(
"select _id from quotes_internal where text ='"+s+"';", null);
// Cursor c = ourDatabase.query(DATABASE_TABLE_LOGICAL, columns,
// null, null, null, null, null);
int iName = c.getColumnIndex(KEY_ROWID);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result=(" " + c.getInt(iName));
}
} catch (Exception e) {
// TODO: handle exception
} finally {
c.close();
}
return result;
}
I put this code in the database helper class and from the main activity I am calling it as
results =new String();
results=db.getDatatonumber(texts).trim();
text.settext(results);
I am not able to understand where I am going wrong I am passing the same values which are there in the database.
c = ourDatabase.rawQuery(
"select _id from quotes_internal where city='"+s+"';", null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result=(" " + c.getColumnIndex("_id"));
}
this will return your row id
You need to user LIKE Operator while comparing with Text
c = ourDatabase.rawQuery("select _id from quotes_internal where text LIKE '" + s.trim() + "';", null );

Adding anything to Section String breaks the android SQLite DB query?

I'm new to Android and SQLite, too. I'm trying to write a method where the DB query gets a specific row from the DB. This version of the method works fine where Selection is set to null:
public String anotherTry(){
String[] columns = new String[]{ KEY_SDATE, KEY_SC, KEY_VE };
Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
if (cursor != null){
cursor.moveToFirst();
result = result
+ cursor.getString(0) + "\n"
+ cursor.getString(1) + "\n"
+ cursor.getString(2) + "\n";
return result;
}
return null;
}
but if I put anything in the Selection argument, it crashes. For example:
public String anotherTry(){
String[] columns = new String[]{ KEY_SDATE, KEY_SC, KEY_VE };
Cursor cursor = db.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + "8", null, null, null, null);
String result = "";
if (cursor != null){
cursor.moveToFirst();
result = result
+ cursor.getString(0) + "\n"
+ cursor.getString(1) + "\n"
+ cursor.getString(2) + "\n";
return result;
}
return null;
}
What's going wrong here? I'm trying to get the 8th row to be displayed. I've tried all the formatting combos that I can think of: KEY_ROWID + "=" + "8", KEY_ROWID = "8", and so on, but no luck. Thanks for any help!
try followin code, i think it wil work for u
Cursor cursor = db.query(DATABASE_TABLE, columns, KEY_ROWID+"=?", new String[] {"8"}, null, null, null);
try the following code:
Cursor cursor = db.query(DATABASE_TABLE, columns, KEY_ROWID + "=8", null, null, null, null);
or i think that nothing exist in your database at KEY_ID=8 bcoz the CursorIndexOutOfBounds exception comes when the query returns no data.Check if database exists at KEY_ROWID=8

Categories

Resources