Android Sqlite query runs in 4.0 but not in 2.3 - android

I have the following query that I run on the database in Android
SQLiteDatabase db = this.getWritableDatabase();
Cursor c;
String Query = "Select * from SubCategory_List where CatId = (Select _id from Category_List where Name = ?) order by Name ASC";
c = db.rawQuery(Query, new String[] { CatName });
I use this cursor to populate the listview. In android 4.0 i get the desired output, But in 2.3 the vales are not displayed.

Change this :
String Query = "Select * from SubCategory_List where CatId = (Select _id from Category_List where Name = ?) order by Name ASC";
by this :
String Query = "Select * from SubCategory_List where CatId IN (Select _id from Category_List where Name = ?) order by Name ASC";
(Changing = into IN)

Related

Android SQLiteException parameter

I'm working very well with the SQLite database, but now I have a problem while using a query.
For example, if use a query like this:
SQLiteDatabase db = mHelper.getReadableDatabase();
String parameter = mypar;
String sql = SELECT color, name FROM table WHERE name = '" + parameter + "';
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()) {
String color = c.getString(0);
String name = c.getString(1);
}
c.close();
db.close();
Everything works fine
But if the parameter has an apex
String parameter = mypar';
I get an exception
Caused by: android.database.sqlite.SQLiteException: near "r": syntax error (code 1):
How can I solve this problem? Thank you
this is my solution, works fine!! try it
String parameter = mypar';
String sql = SELECT color, name FROM table WHERE name = ?;
Cursor c = db.rawQuery(sql, new String[] { parameter });

Android SQLite select with like

I'm working sqlite. I wrote some code which can to select some datas with like in table
this is a part my source
String[] args = new String[1];
args[0] = "%" + personalid + "%";
Cursor friendLike = db
.rawQuery(
"select * from LoanList WHERE PersonID like ?",
args);
SQLiteDatabase db = this.getWritableDatabase();
//Cursor cursor = db.rawQuery(selectQuery, null);
System.out.println("Cursor Count = " + friendLike.getCount());
I can select with PersonID from loan list, but now I want to select(with like) with two parameters. with PersonID and for example personage
just, I want to add second parameter in select.
How?
Add a second ? and a second argument to the array:
SQLiteDatabase db = this.getWritableDatabase();
String[] args = new String[]{"Some Value", "%" + personalid + "%"};
Cursor friendLike =
db.rawQuery("SELECT * FROM LoanList WHERE SomeField = ? AND PersonID Like ?", args);
SomeField is your other search field.
The placeholders (?) respect the bound parameter list (your args array) order.

Error executing select query in sqlite by android

I have write this code for select filtered data from sqlite db through android. But it says there are some error. Can you help me to correct my code please.
category = data[0];
district = data[1];
SQLiteDatabase db = openOrCreateDatabase("dbumbers", 1, null);
Cursor c = db.rawQuery("SELECT * FROM numbers where [category = '"+category+"'] and [district = '"+district+"']", null);
You should use this:
Cursor c = db.rawQuery("SELECT * FROM numbers where category = '"+category+"' and district = '"+district+"'", null);
You must eliminate [] from your query

rawQuery in android

I have a database in my application. I want to select specific rows.
here is my query string :
SQLiteDatabase db = this.getReadableDatabase();
String C_PRODUCT_CATEGORY_ID = category_id;
String TABLE_PRODUCT = tblProduct;
String categoryID = 1;
String select = String.format("SELECT %s FROM %s WHERE %s=%s",
C_PRODUCT_CATEGORY_ID, TABLE_PRODUCT, C_PRODUCT_CATEGORY_ID,
categoryID);
Cursor c = db.rawQuery(select,new String[]{});
the cursor c is empty! what when i use the .db file on device with sqlite editor and run
SELECT category_id FROM tblProduct WHERE category_id=1
it returns 2 rows.
whats wrong? please help me!
Use database's query method as,
Cursor c = db.query(TABLE_PRODUCT, null, C_PRODUCT_CATEGORY_ID+"=?", new String[]{categoryID}, null, null, null);
Hope this works well for you !!
SQLiteDatabase db = this.getReadableDatabase();
String categoryID = 1;
String select = "SELECT category_id FROM tblProduct WHERE category_id="+categoryID;
Cursor c = db.rawQuery(select,new String[]{});
Just use this
It could be that you need to put the ' and ' after "="; I mean:
String select = String.format("SELECT %s FROM %s WHERE %s='%s'",
C_PRODUCT_CATEGORY_ID, TABLE_PRODUCT, C_PRODUCT_CATEGORY_ID,
categoryID);
Cursor c = db.rawQuery(select,null);
try this
Cursor c = dB. rawQuery (select, null);

select one cell from sql database

I'm trying a simple SQL Command wihtin my Android-App, to get the age of a selected Person:
public int getAge(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM persons WHERE name =? " + MainActivity.selectedPerson.getText().toString(), null);
int age = cursor.getInt(3); // column with ages
cursor.close();
db.close();
return age;
}
But when I run my app, it crashes when I call the function getAge(). I get the following Error:
SQLiteException: no such column: Max: , while compiling: SELECT * FROM persons WHERE name = Max
I don't get it. There is the name "Max" in the table. What am I doing wrong? Thanks in advance.
Edit 2:
With this one:
Cursor cursor = db.rawQuery("SELECT name FROM persons WHERE name = '" + MainActivity.selectedPerson.getText().toString() + "'", null);
I get a different error:
08-27 19:43:47.573: E/AndroidRuntime(6161): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
what does this mean?
You should consider using the selectionArgs parameter of rawQuery() to prevent SQL Injection Attacks:
Cursor cursor = db.rawQuery("SELECT * FROM persons WHERE name = ?", new String[] { MainActivity.selectedPerson.getText().toString() });
Also you only need one column so rather than wasting resources by selecting them all with *, you should just select the one column:
Cursor cursor = db.rawQuery("SELECT age FROM persons WHERE name = ?", new String[] { MainActivity.selectedPerson.getText().toString() });
Hope that helps!
All together it should look like:
public int getAge(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT age FROM persons WHERE name = ?", new String[] { MainActivity.selectedPerson.getText().toString() });
int age;
// Check if you have a valid result and move to the first row to read it
if(cursor.moveToFirst())
age = cursor.getInt(0);
// Prevent a crash if there is no data for this name
else
age = 0;
cursor.close();
db.close();
return age;
}
Chan ge the 3rd line of your program:
Cursor cursor = db.rawQuery("SELECT * FROM persons WHERE name =? " + MainActivity.selectedPerson.getText().toString(), null);
to this:
Cursor cursor = db.rawQuery("SELECT * FROM persons WHERE name = ?", new String[] { MainActivity.selectedPerson.getText().toString()} );
Try this:
public int getAge(){
int age;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM persons WHERE name = '" + MainActivity.selectedPerson.getText().toString()+"'",null);
if(cursor.moveToFirst())
{
age = cursor.getInt(3); // column with ages
}
cursor.close();
db.close();
return age;
}
You missed the single quotes (' ') in your sql command. That's why MAX was taken as a column and not as a value.

Categories

Resources