I have a SQLite database in which I am trying to read the last 8 rows in database using ID.
I am having trouble with the query. Can't seem to get it right.
public String lastEightRows(){
String[] columns = new String[] {KEY_ROWID, KEY_COLORS};
String result30 = " ";
String where = "ORDER BY" + KEY_ROWID + "DESC LIMIT" + 8;
//This is where I am not able to get the query right
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, where, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iColors = c.getColumnIndex(KEY_COLORS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result30 = result30 + c.getString(iRow) + " " + c.getString(iColors) + " " + "\n";
}
return result30;
}
I got this error message
03-26 22:58:27.420: E/AndroidRuntime(1368): android.database.sqlite.SQLiteException: near "DESC": syntax error (code 1): , while compiling: SELECT _id, persons_colors FROM personsTable WHERE _id DESC LIMIT 8
From Android SQLiteDatabase API:
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Query the given table, returning a Cursor over the result set.
Try this code instead
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, KEY_ROWID + " DESC", "8");
sqlitedatabase.query() defined query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
So, try change this.
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, KEY_ROWID + " DESC", "8");
Related
Anyone has an idea how this is done? I have a regular search query below that displays matching data in a listview, i tried putting COLUMN NAME + " ASC" at the order part, but i got errors.
public Cursor getSearch(String query)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_NAME + " LIKE ?",
new String[] {"%"+ query+ "%" }, null, null, null,
null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
The 'Order by' clause should be the 8th argument in the query call:
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_NAME + " LIKE ?",
new String[] {"%"+ query+ "%" }, null, null, COLUMN_NAME + " ASC", null);
See: Documentation
how can I add more than one selection argument?
for example this is one argument selection
inputNumber + "= ?"
new String[]{String.valueOf(numberToCheck)}
full cursor code
Cursor cursor = sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, inputNumber + "= ?", new String[]{String.valueOf(numberToCheck)},
null, null, null, null);
how can i add another condition to the sql statement? like for example
input_number = numberToCheck AND name = "XXX"
input_number is an integer column and name is a column of Strings
sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, input_number + "= ? AND "+ name"= ?", new String[]{String.valueOf(numberToCheck), "XXX"},
null, null, null, null);
Try this:
Cursor cursor = sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, inputNumber + "= ? AND "+name+ " = ?", new String[]{String.valueOf(numberToCheck,nametocheck)},
databse.query(String dbName, String[] columnNames, String whereClause, String[] selectionArgs, String[] groupBy, String[] having, String[] orderBy)
Cursor cursor = database.query(YOUR_TABLE_NAME ,
new String[] {NAME_OF_COLUMNS_WHICH_IS_TO_BE_SELECT} ,
input number + "='" + numberToCheck + "' AND " +
name + "='XXX'",
null, null, null, null);
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.
i want to get the row from databas which name = jiten
but is gives error in syntax can anyone tell me what is the right syntax for
getting the single row
here is my code for database class
public void getdata()
{
Cursor cursor = myDataBase.query("emp", new String[] {"email","name"}, " name='jiten'",new String[]{}, null, null, null);
Log.e("running", "cursor run");
if(cursor!=null)
{
Log.e("running", "curosr is not null");
while(cursor.moveToFirst())
{
Log.e("running", "curosr while loop enter");
String temp = (cursor.getString(cursor.getColumnIndex(email)));
String temp2 =(cursor.getString(cursor.getColumnIndex(name)));
Log.e("running", "id email" +temp+ " name"+temp2);
}
}
}
You are missing the selectionArgs
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
You may include ?s in selection, which will be replaced by the values
from selectionArgs, in order that they appear in the selection. The
values will be bound as Strings.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
in your case it would be:
Cursor cursor = myDataBase.query("emp", new String[] {"email","name"}, "name=?",new String[]{"jiten"}, null, null, null);
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