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);
Related
I am selecting row based on user id but non is returning
select all is working and i am sure of the id in the where
can somone help me what is wrong with the query
String sql = "SELECT * FROM " + TABLE_Txns
+ " WHERE " + COLUMN_USERID + " = ?";
SQLiteDatabase db = this.getReadableDatabase();
List<TransactionModel> savedTransactions = new ArrayList<>();
Cursor cursor = db.rawQuery(sql, null
, new String[]{getLogin_id()});
Use rawQuery() this way:
Cursor cursor = db.rawQuery(sql, new String[]{getLogin_id()});
The 2nd parameter is the selection arguments.
The signature you used with 3 parameters is this:
rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal)
and you don't need that.
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());
}
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 });
I am using query function in the SQLiteDatabase class in android for fetching contents from the sqlite database. It works fine. But when I am trying to use date function of sqlite in my query as follows, it does not return any rows.
SQLiteDatabase mDb = mDbHelper.getWritableDatabase();
String [] selectionArgs = new String[] {"date('2016-02-10 00:00:00')"};
String selection = "date(column_name) = ?";
String limit = 4;
String[] ALL_COLUMNS = new String[] { "id", "column_name","column_name2"};
cursor = mDb.query("table_name", ALL_COLUMNS,selection , selectionArgs, "column_name" + " DESC", limit);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
//code to read each row
}
cursor.close();
}
Can anyone tell me what is wrong with the above code?
Also, is there a way to compare the date (and not the time) from the sqlite database using SQLiteDatabase class query function?
In SQLite dates are strings. Reference: https://www.sqlite.org/lang_datefunc.html
Therefore, simply remove the date() function from the date value and from the column name:
i.e.: change this
String [] selectionArgs = new String[] {"date('2016-02-10 00:00:00')"};
String selection = "date(column_name) = ?";
to this
String [] selectionArgs = new String[] {"2016-02-10"};
String selection = "column_name = ?";
If you need the time, too:
String [] selectionArgs = new String[] {"2016-02-10 00:00:00"};
String selection = "column_name = ?";
See the SQLite supported datetime strings in the link above
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.