Android sqlite query for string - android

Cursor cursor = db.rawQuery("SELECT id,lastname FROM people WHERE lastname='John Kenedy'; ",null);
Is that correct usage of comparing String ? if no, How can I compare String values in the database?

For better performance, you should use rawQuery method with selectionArgs which is faster and more secure against adding directly data to statement. So try it like this
Cursor cursor = db.rawQuery("SELECT id,lastname FROM people WHERE lastname = ?; ", new String[] {"John Kenedy"});

Related

Android SQLite query cursor moveToFirst

I'm making an Android App with a sqlite db, which has a query method like this:
String[] projection = {COLUMN_NAME_ID};
String selection = COLUMN_NAME_TEAM + " = ?";
String[] selectionArgs = {teamId.toString()};
String sortOrder = COLUMN_NAME_NAME + " ASC";
Cursor c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
if (c.moveToFirst()){
//...
}
as suggested here. The point is to implement this query:
SELECT PLAYER_ID FROM PLAYERS WHERE TEAM_ID = ? ORDER BY NAME ASC;
According to the SQLiteDatabase.query documentation, this is a valid call to query(), and so far I've been doing it successfully with all my other queries in this App.
For some reason, in this query (and only in this) the App freezes in c.moveToFirst().
Some answers to similar questions suggest that it might be a performance issue. I don't think this is the case, as my table has only 6 rows and the query is quite simple.
Any ideas?
Thanks

Android: Getting row by id

I'm trying to get database row by it's ID, but somehow query doesn't return that row. The sql query SELECT * From Table1 Where _id = 100 works good, so I don't know what's the reason. Here is the code of the query:
String [] selectedArgs = new String [] {String.valueOf(selectedItemId)};
String selection = Tables.Table1.COLUMN_ID + "=?";
String [] columns = {Tables.Table1.COLUMN_ID, Tables.Table1.COLUMN_NAME};
Cursor c = foodDB.query(Tables.Food.TABLE_NAME, columns, selection, selectedArgs, null, null, null);
Does anybody have any suggestions?
The problem is that your ID field is a number, while the parameter is a string; the query function does not allow other parameter types for some strange reason.
Try using an expression like COLUMN_ID + " = CAST(? AS INTEGER)".
If your query had only one result column, you could use a separate SQLiteStatement object where you'd be able to use bindLong().
Just in case somebody need it, i have used rawQuery() method for such type of query to work, because query() doesn't work.

Android SQLite wildcard to select character pattern

I am using an SQLite database in Android. I want to query the database using a query like
SELECT * FROM Persons WHERE City LIKE '%nes%'
which would return the city Sandnes based on the character pattern 'nes' matching, as in: http://www.w3schools.com/sql/sql_wildcards.asp.
However, when I try to put this into android's
query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
method, using
selection = KEY_CODE + " LIKE ?"; and selectionArgs = new String[] {"'%" + query + "%'"};,
I get no results returned.
Can I do this in the android query? If so, how should I write it? And if not, is there an alternative way?
Thanks in advance.
You're building a "likeable" parameter that looks like this:
'%do you like me%'
The apostrophes should not be there: they belong to literal syntax, not to a pattern itself:
%now you like me%
Use
selectionArgs = new String[] {"%" + query + "%"};

What is the correct syntax for SELECT statement and WHERE clause?

I am trying to explore android and I just started using SQLite database. I'm wondering on what is the right syntax for selecting a single row from a table, where the row I want to select is from the value entered from a user using editText. Thanks in advance.
I'm going to disagree with both of the answers above. What if the user enters this query:
Bobby Tables'; drop table yourTable;
See: http://xkcd.com/327/
I believe you should do this instead:
String query = "select * from TABLE_NAME WHERE column_name=?";
String[] selection = new String[1];
selection[0] = users_entered_value;
Cursor c = db.rawQuery(query, selection);
ETA: Actually, the more I think about it, the more I think you're going in the wrong direction. If your app depends on a database query returning exactly one unique match to an arbitrary string entered by the user, it's probably going to be broken a great deal of the time.
What you should probably do is something like this:
String query = "select * from TABLE_NAME WHERE column_name LIKE ?";
String[] selection = new String[1];
selection[0] = "%" + users_entered_value + "%";
Cursor c = db.rawQuery(query, selection);
and then iterate through the results and pick a "best" match according to your own criteria.
Also, you should create the table with case-insensitive matching for the column(s) you're going to be searching.
SQLiteDatabase db;
db.rawQuery("select * from yourTable where your_column_name = 'users_entered_value' limit 1", null);
SQLiteDatabase db;
// make connection to your database ;
Cursor c = null ;
String SQL = "select * from TABLE_NAME where column_name='VALUE'";
c = db.rawQuery(SQL);
c contains your result array of query you fired.
You can retrieve values using loop.

Join in sqlite Android

I have two tables in my database table1, table2. I have a join query like this:
select a.*,b.* from table1 a,table2 b where a.field1=b.field1;
Its working fine while i test it in sqlite manager(sqlite browser). when i try to make it through java like this:
database.execSQL("select a.*,b.* from table1 a,table2 b where a.field1=b.field1;");
its saying return type is not cursor. How can i get it to cursor with join. I have to get that data and show in a listview
use like this:
String qry = "select a.*,b.* from table1 a,table2 b where a.field1=b.field1";
Cursor cur = db.rawQuery(qry,null);
String query = "select a.*,b.* from table1 a,table2 b where a.field1=b.field1;";
Cursor c = database.rawQuery(query, null);
/* do whatever you want with 'c' */
Following is an Example of rawQuery, you can add your Query Respectively.
String getRT = "SELECT count(*) from "+ DATABASE_PROFILE+";";
Cursor mCur = sqlitedb.rawQuery(getRT, null);
return mCur;
SQLiteDatabase.rawQuery(query, selectionArgs)
Use rawQuery method to design any sort of complex query you might need. In the reference there are many useful methods you might need.

Categories

Resources