SQLiteException when querying in Android - android

I receive this exception "bind or column index out of range" in this line:
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
I want to check if the given id exists in the database, but always the query throw this exception and I don't know what is wrong. I tried with this and the exception it's the same:
Cursor cursor = db.query(TABLE_NAME, null, ID+" = ?", new String[] { id }, null, null, null);
This is the method I've programmed. If there is something wrong here, please, tell me:
public boolean existsAcc(String id)
{
//Check the parameters
if (id == null)
{
throw new IllegalArgumentException(
"The id parameter cannot be null");
}
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
if (!cursor.moveToFirst())
{
cursor.close();
return false;
}
cursor.close();
return true;
}

I don't know why Chirag deleted his answer, but he was rigth. Changing
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
to
Cursor cursor = db.query(TABLE_NAME, null, ID + " = '" + id + "'", null, null, null, null);
does the trick.

Related

IllegalArgumentException in filtering ListView

I am using this code to filter my listview:
Cursor fetchCountriesByName(String inputText) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
if (inputText == null || inputText.length () == 0) {
String[] columns = new String[] {DBHelper.ID,DBHelper.FNAME,DBHelper.LNAME,DBHelper.ADDRESS };
cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
}
else {
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
I got an error
java.lang.IllegalArgumentException: column 'patient_address' does not exist
But the column names are same. There is a column called patient_address
What's the reason of this error? Please guide.
Try this
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%?%'", new String[]{inputText },
null, null, null, null);
the parameter is error.

SQLite in Android: when I pass any name that contains a quote, it throws and error

Adapter.java
public String getID(String i) throws SQLException
{
db=DBHelper.getReadableDatabase();
String ij="No Track Found";
Cursor mCursor =
db.query(true, TABLE, new String[] {KEY_ID}, KEY_NAME + "=" + "'"+i+"'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
if (mCursor.moveToFirst())
{
ij=mCursor.getString(0);
}
return ij ;
}
The Question is when I pass any song's name that contain "'"(e.g. alexander's blade) , it throws and error. Otherwise all fine.
You should use the selectionArgs parameter:
db.query(true, //distinct
TABLE, //table
new String[] {KEY_ID}, //columns
KEY_NAME + "=?", //selection
new String[] {i}, //selectionArgs
null, //groupBy
null, //having
null, //orderBy
null //limit
);
Using selectionArgs also help to prevent SQL Injection
You need to call your method like this:
Cursor c = bd.query(Constantes.TABELA_APLICATIVO, COLS, "urlandroid = ?", new String[]{url}, null, null, null);
The caracter "?" represents the values on Third parameter(String[]).

Check if data is exist in SQLite Android

I use this function to get specific data from SQlite:
SearchRes getSresultByName(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WIKIRES, new String[] { KEY_ID,
KEY_SNAME, KEY_SJSON }, KEY_SNAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
SearchRes searchres = new SearchRes(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2));
return searchres;
}
And it works just fine, I need to create similar function to test if value is exist in the table, so I tried this:
boolean checkIfExist(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WIKIRES, new String[] { KEY_ID,
KEY_SNAME, KEY_SJSON }, KEY_SNAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor == null)
return false;
else
return true;
}
But I always get TRUE. Can you help me figure out what is the problem?
you should not be checking if cursor is null you should be checking if anything exists in the cursor
if(cursor.moveToFirst()){
return true
}else{
return false;
}
You can try Cursor getCount() to check the number of rows in the result of the query.. Following is the sample code:
boolean checkIfExist(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WIKIRES, new String[] { KEY_ID,
KEY_SNAME, KEY_SJSON }, KEY_SNAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor.getCount() > 0)
return true;
else
return false;
}
answer by #tyczj is also good..

SQLiteDatabase - How to use where clause?

public Cursor fetchTimetable() {
return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, null, null, null, null, null);
}
For example if i wanted TIMETABLE_MODULECODE = 123. How would i do this, i have read its the first null. I have tried this below but still doesnt work
public Cursor fetchTimetable() {
String a = "TIMETABLE_CODE = ADD";
return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, a, null, null, null, null);
}
My working example with where args (it's more clear with using it):
String [] settingsProjection = {
DBContract.Settings._ID,
DBContract.Settings.COLUMN_NAME_USER_ID,
DBContract.Settings.COLUMN_NAME_AUTO_LOGIN
};
String whereClause = DBContract.Settings.COLUMN_NAME_USER_ID+"=?";
String [] whereArgs = {userId.toString()};
Cursor c = db.query(
DBContract.Settings.TABLE_NAME,
settingsProjection,
whereClause,
whereArgs,
null,
null,
null
);
db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
TABLE_ROW_ID + "=" + rowID, here "=" is the where clause
What exactly does "still doesn't work" mean?
The following code should work just fine:
public Cursor fetchTimetable() {
String[] columnNames = new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME};
String whereClause = "TIMETABLE_MODULECODE=123";
return mDb.query(DATABASE_TABLE_TIMETABLE, columnNames, whereClause, null, null, null, null);
}
the right syntax is
String[] projection= {column names};
String[] where={"values for where clause"}; //this must be array
public Cursor fetchTimetable() {
return mDb.query(TABLE_NAME, projrction, "columname"+"=?", where, null, null, null);
}
The simple way:
return mDb.rawQuery("SELECT * FROM myTable WHERE column1 = "+ someValue, null);
You can use Use this query:
SQLiteDatabase db = this.getReadableDatabase();
//Cursor cursorr = db.rawQuery(Query, null);
Cursor cursorr = db.rawQuery("select * from " + DATABASE_TABLE_EHS + " where " + TASK_ID + "='" + taskid + "'" , null);
if (cursorr.moveToFirst())
{
do {
// your code like get columns
}
while (cursorr.moveToNext());
}
}
If you're checking against a string value you'll want to wrap the string in quotes like below:
dbHelper.COL_EMAIL +"='"+givenEmail+"'",

Return a specified data at row "index" from an SQLite query

I have this query:
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
And i want it to return the data of column_descri at row "index". "Index" is a paramater in my function:
public void showOverlay (OverlayItem overlay, int index)
{
db = openHelper.getWritableDatabase();
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
if (cur.moveToPosition(index)) {
//show an AlertDialog with description of row index
How can i acheive that ?
Thank you for helping.
You can try this:
String res = cur.getString(cur.getColumnIndex(COL_DESCRI));
cur.close();

Categories

Resources