Error executing select query in sqlite by android - 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

Related

SQLite rawQuery with multiple WHERE clauses

I have retrieved 2 values through the putExtra and getStringExtra method in another class. BOth these values are correct. I need to use these 2 values in my SQL query:
final Cursor cursor = sdb.rawQuery("SELECT match_player_name FROM match_response WHERE match_date=? AND " +
"match_response =?"+new String [] {String.valueOf(date),String.valueOf(response)}, null);
I am unfamiliar using rawQuery with more than 1 condition. Have I the correct syntax? I have a record in my SQLite database satisfying this condition.
This is an example:
Cursor cur = database.rawQuery("select name from Table where ID=? and SubCategoryID=?",
new String [] {String1,String2});
public Cursor getMachineServices(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select row1, from maquina where something = x AND somethingelse = y", new String[]{});
return c;
}
Then on your activity where you want to display it you must do:
Database db;
db = new Database(yourActivity);
Cursor c = db.getMachineServices);
if (c.moveToFirst()) {
do {
String machines
c.getString(c.getColumnIndex("row1"));
//you can use the variable machines where you want
} while (c.moveToNext());
}

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

How to insert a text with single quots to sqlite db in android?

I am facing an issue in inserting an issue in inserting a text with single quotes in sqlite db in android. it is returning the following error.
06-21 15:21:55.644: E/AndroidRuntime(16328): android.database.sqlite.SQLiteException: near
"re": syntax error (code 1): , while compiling: select * from tbl_chatHistory where
chat_date = '1.403344315631E12' and chat_time = '1.403344315635E12' and chat_text =
'you're'
Please help.
**************** EDITED *************************
public boolean checkChatExists(String date, String time, String chat) {
boolean result = false;
String selectQuery = "select * from tbl_chatHistory where chat_date = '"+date+"'
and chat_time = '"+time+"' and chat_text = '"+chat+"'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
result = true;
} else {
result = false;
}
cursor.close();
return result;
}
In SQL string literals you can escape ' as ''.
It however better to use parametrized queries. Replace the string literals with ? and provide the values in the bindArgs array. For example:
String selectQuery = "select * from tbl_chatHistory where chat_date = ? and chat_time = ? and chat_text = ?";
Cursor cursor = database.rawQuery(selectQuery, new String[] { date, time, chat });
Use ` instead of '. The problem is that the second ' is recognised as the end of the text.

Android Sqlite query runs in 4.0 but not in 2.3

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)

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