CursorIndexOutOfBoundsException: - android

I am new to the android i am trying to get the cursor values related to the particular column value. but it's showing null.
public int getDashBoardCount() {
String countQuery = "SELECT * FROM " + DASHBOARD_TABLE + " WHERE " + VECHICAL_TYPE + " = " + 'c';
From here also i am getting no such column: c (code 1): , while compiling: SELECT * FROM dashboard_table WHERE vehicle_type = c
String countQuery = "SELECT * FROM " + DASHBOARD_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}
public DashBoardAvaCapModel getVechicalRelAva(String vechical_type) {
SQLiteDatabase db = null;
Cursor cursor = null;
DashBoardAvaCapModel dash = null;
try {
db = this.getReadableDatabase();
cursor = db.query(DASHBOARD_TABLE, new String[]{_ID,
AVAILABLE, CAPACITY, BOOKED, VECHICAL_TYPE}, VECHICAL_TYPE + "=?",
new String[]{String.valueOf(vechical_type)}, null, null, null, null);
int idkey = cursor
.getColumnIndex(_ID);
int available = cursor
.getColumnIndex(AVAILABLE);
int capactity = cursor
.getColumnIndex(CAPACITY);
int booked = cursor
.getColumnIndex(BOOKED);
int vechicaltype = cursor
.getColumnIndex(VECHICAL_TYPE);
if (cursor != null && cursor.moveToFirst())
dash = new DashBoardAvaCapModel(Long.valueOf(cursor.getString(idkey)),
cursor.getString(available), cursor.getString(capactity), cursor.getString(booked), cursor.getString(vechicaltype));
Log.e("dash", "" + cursor.getString(available));
} catch (final Exception ex) {
String exp = String.valueOf(ex);
Log.e("Databa", "" + exp);
} finally {
cursor.close();
db.close();
}
return dash;
}

After writing this line
cursor = db.query(DASHBOARD_TABLE, new String[]{_ID,
AVAILABLE, CAPACITY, BOOKED, VECHICAL_TYPE}, VECHICAL_TYPE + "=?",
new String[]{String.valueOf(vechical_type)}, null, null, null, null);
pls try to write
cursor.moveToFirst();
For first one you can use like
String countQuery = "SELECT * FROM " + DASHBOARD_TABLE + " WHERE " + VECHICAL_TYPE + " = '" + c + "'"
where c is any variable
Hope this will help you

Related

How to return a string value from sqlite query?

Here is my query. I want to get the string value and return the single value to the activity class from where I have called the getSelectedMerchantCode function. My code is not working. Its returning "error"
public String getSelectedMerchantCode(String merchantname){
String selection = "Error";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
if(c.getCount() == 1){
c.moveToFirst();
selection = c.getString(c.getColumnIndex("merchantCode"));
return selection;
}
c.close();
db.close();
return null;
}
Try this way..
public List<String> getMyItems(String name) {
List<String> stringList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
String name = (c.getString(c.getColumnIndex("Item_Name")));
stringList.add(name);
c.moveToNext();
}
}
return stringList;
}

android.database.sqlite.SQLiteException: no such column: caolumn value [duplicate]

This question already has answers here:
android.database.sqlite.SQLiteException: no such column
(5 answers)
Closed 6 years ago.
i am fetching and match value from database table column but its give exception no such column name in android butandroid.database.sqlite.SQLiteException: no such column:speech but speech is value in column but its give this error
i am using this code
public List<TextJson> getDetailtospinner(String text_spinner1) {
SQLiteDatabase db = this.getReadableDatabase();
List<TextJson> textList = null;
try{
textList = new ArrayList<TextJson>();
String[] columns = new String[]{KEY_ID,KEY_NAME};
Cursor cursor = db.query(TABLE_TEXTS, columns, KEY_NAME + "=" + text_spinner1, null, null, null, null);
//Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_TEXTS + " WHERE " + KEY_CATE + " = " + text_spinner1 , null);
int rows = cursor.getCount();
if (cursor != null) {
Log.e("0_0","0_0 1=>");
for(int i=0;i<rows;i++) {
while (cursor.moveToNext()) {
TextJson txtlist = new TextJson();
Log.e("0_0", ":( ==>");
txtlist.setId(cursor.getInt(0));
txtlist.setText_status(cursor.getString(1));
textList.add(txtlist);
}
}
}
db.close();
}catch (Exception e){
Log.e("error","is==> " +e);
}
return textList;
}
Enclose where condition value with ' if the values is not integer.
Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_TEXTS + " WHERE " + KEY_CATE + " = '" + text_spinner1 +"'", null);

Android SQLite rawQuery() method

I created a table with three columns id, name and discipline.
I want to find the student name given the discipline.
Following is my method:
String findstudent(String disc){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String find = "SELECT * FROM "+ TABLE_STUDENTS + " WHERE "+KEY_DISCIPLINE +" = " +disc ;
Cursor cursor = sqLiteDatabase.rawQuery(find,null);
cursor.moveToFirst();
String found =cursor.getString(1);
return found;
}
When I use it, the application stops working.
I am not sure. But, you can try this way
Cursor.moveToFirst();
do{
//get the data
String found =cursor.getString(cursor.getColumnIndexOrThrow("COLUMN NAME"));
}while(Cursor.moveNext);
Try the following code:
String find = "SELECT *<enter code here>
FROM "+ TABLE_STUDENTS + "
WHERE "+KEY_DISCIPLINE +" = '" + disc + "'" ;
Try this:
String findstudent(String disc){
Cursor cursor;
cursor = this.db.query(TABLE_STUDENTS,null, KEY_DISCIPLINE +" = "+ disc, null, null, null, null,null );
cursor.moveToFirst();
String found =cursor.getString(0);
return found;
}
Hope it Helps!!
public Cursor findstudent(String disc){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String[] find = new String[]{ col1,col2,col3};//all fields in db in need
String order=col1;
Cursor c=db.query(TABLE_STUDENTS, find, KEY_DISCIPLINE +" = " +disc, null, null, null, order);
c.moveToFirst();
db.close();
return c;
}
In the activity include this:
DataBaseHandler db = new DataBaseHandler(this);
try {
Cursor c = db.displayName(your_disc);
startManagingCursor(c);
if (!c.moveToFirst()) {
System.out.println("!Move " + posnumber);
} else {
String idname = c.getString(c
.getColumnIndex(DataBaseHandler.KEY_NAME));
System.out.println("null:");
System.out.println("Move " + idname);
return true;
}
c.close();
stopManagingCursor(c);
} catch (Exception ex) {
}

Android SQLite Query: Trouble with WHERE clause

Please let me know why my where clause isn't working. I tried using the query instead of rawquery but no luck.
try {
String categoryex = "NAME";
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
MyData = dbHelper.getWritableDatabase();
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + where Category = '+categoryex'" , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("Category"));
String age = c.getString(c.getColumnIndex("Text_Data"));
results.add( firstName + " Directions: " + age);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (MyData != null)
MyData.execSQL("DELETE FROM " + tableName);
MyData.close();
}
try... (you left out a double-quote before where.
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" +categoryex + "'" , null);
I think you should use rawQuery in this form:
rawQuery("SELECT * FROM ? where Category = ?", new String[] {tableName, categoryex});
I think it's more secure this way.
Your quotes are buggered:
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" + categoryex + "'" , null);
You also should read up on SQL injection attacks.
it will be more easy if you use this technique instead of rawQuery,its easy way change your table name, columns and where conditions accordingly.
public ArrayList<Invitees> getGroupMembers(String group_name) {
ArrayList<Invitees> contacts = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {COLUMN_CONTACT, COLUMN_PHONE_NUMBER};
String selection = COLUMN_GROUP_NAME + "=?";
String[] selectionArgs = {group_name};
Cursor cursor = db.query(GROUPS_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
do {
Invitees invitees = new Invitees();
invitees.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)));
invitees.setInviteePhone(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PHONE_NUMBER)));
contacts.add(invitees);
} while (cursor.moveToNext());
}
return contacts;
}

Cursor returning null

public String getContact(String searchName) {
SQLiteDatabase db = this.getReadableDatabase();
String[] args = { searchName };
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES
+ " WHERE name =? ", args);
String iName = null, iDiretor = null, iGenre = null;
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
iName = cursor.getString(cursor.getColumnIndex(KEY_NAME));
iDiretor = cursor.getString(cursor.getColumnIndex(KEY_DIRECTOR));
iGenre = cursor.getString(cursor.getColumnIndex(KEY_GENRE));
cursor.moveToNext();
}
cursor.close();
The iName variable is working fine but the other two are returning null. Any help?
Use the SQLiteDatabase query methods instead of rawQuery for the best results.
db.query(TABLE_MOVIES, null, "name = ?", args, null);
This is preferred because rawQuery is easy to mess up and doesn't protect against SQL injections.
Try this way:
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES + " WHERE name LIKE ? ", args);
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MOVIES
+ " WHERE name LIKE "+searchName, null); // Put Like When your are comparing String

Categories

Resources