How can pass two parameters to rawQuery in android? [duplicate] - android

PREF_NAME, PREF_STATUS - text
It gives me this error:
07-02 14:08:07.457: E/AndroidRuntime(17295): android.database.sqlite.SQLiteException: near "?": syntax error: , while compiling: SELECT ? FROM ? WHERE ? = ?
The code I'm using is:
Cursor c = db.rawQuery("SELECT ? FROM ? WHERE ? = ?", new String[]{ PREF_STATUS,
PREF_TABLE, PREF_NAME, assigned_pref_name });

According with the documentation, you only can put "?" on where clause:
public Cursor rawQuery (String sql, String[] selectionArgs)
Parameters
sql the SQL query. The SQL string must not be ; terminated
selectionArgs You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.

After the SELECT keyword you have a ? instead of something like *.

I guess you wanted to pass assigned_pref_name as a string and not variable name so :
Cursor c = db.rawQuery(
"SELECT ? FROM ? WHERE ? = ?",
new String[]{
PREF_STATUS,
PREF_TABLE,
PREF_NAME,
"assigned_pref_name" //as a string
});

Related

Sqlite syntax error near "?"

Simple enough; my code looks like this:
int idhash = getIdHash();
String[] args = {"runways", ""+idhash};
cursor = sqlite.rawQuery("SELECT * FROM ? WHERE idhash = ?", args);
And I see this line in logcat:
E/SQLiteLog( 9570): (1) near "?": syntax error
Where did I go wrong?
You can use ? only for binding literals. You cannot use it for binding identifiers such as table names. Identifiers must be in the SQL itself.
Maybe you can try
Cursor cursor = sqlite.rawQuery("SELECT * FROM runways WHERE idhash = ?", new String[]{idhash.toString()});
and it

syntax error near "," while compiling select

Anyone con find the error in these query?
String[] column = {"titolo","regista","cast","descrizione"};
String selection = "_id_film = ? ";
String[] selection_args = {Integer.toString(selectedfilm+1)};
Cursor cursor = db.query("T_FILM", column, selection,selection_args, null, null, null);
logcat says:
android.database.sqlite.SQLiteException: syntax error near"," (code 1) :, while compling SELECT titolo,regista,cast,descrizione FROM T_FILM WHERE _id_film=?
"Cast" is a reserved operation in SQLLite. So it's trying to interpret the column name "cast" as an operation. Your easiest answer is to change the column name to something else.

SQLiteDatabase.query() syntax in Android

This query keeps returning errors like error near ?.
public Cursor getRow2( String st, String dr) throws SQLException {
String whereClause = "(adate ?) AND (station ?)";
String[] whereArgs = new String[] { dr, st };
String orderBy = "adate";
Cursor mCursor = db.query(DATABASE_TABLE, columns, whereClause, whereArgs,
null, null, orderBy);
String dr is for "data-range".
If the user does specify the two dates, then dr gets a value like BETWEEN 2004-03-01 AND 2004-06-01.
Othewise dr gets NOT NULL so that the query finds ALL dates.
String st is for "gas station".
If the user provides the station name, st gets a string like 'Shell'.
Otherwise st gets NOT NULL so as to find ALL stations.
Thank you very much.
You can use ? to bind only literals, not partial expressions.
(adate ?) AND (station ?) is not a valid expression. Binding the first arg to BETWEEN 2004-03-01 AND 2004-06-01 and the second Shell makes it essentially
(adate 'BETWEEN 2004-03-01 AND 2004-06-01') AND (station 'Shell')
which is syntactically incorrect.
To use bind args with a query like this, make the query like
(adate BETWEEN ? AND ?) AND (station = ?)
binding args as "2004-03-01", "2004-06-01", "Shell".
For the "not specified" cases it's probably best to use different queries altogether.

Why is my query crashing?

I've written the following DB query.
However, the app crashes when I access the list activity that displays the results.
I've traced the error to the method below (other simpler query methods work just fine):
public Cursor fetchInterface_HSE_Entries(String string) throws SQLException{
String[] columns = new String[] {KEY_ROW_ID_INTERFACE, KEY_TEXTVIEW_VALUE, KEY_CATEGORY_OPTIONS, KEY_WORKSCREEN};
String whereClause = KEY_WORKSCREEN+"=" + string;
Cursor cursor = db.query(TEXTVIEWS_TABLE, columns, whereClause, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
return cursor;
}
And this is part of my error log:
12-31 16:13:38.851: E/AndroidRuntime(480): Caused by: android.database.sqlite.SQLiteException: no such column: testInterface1: , while compiling: SELECT _id, textviewvalue, categoryoptions, workscreen FROM interfacetable WHERE workscreen=testInterface1
Try with:
String whereClause = KEY_WORKSCREEN+"='" + string + "'";
You need to quote text values in your queries, otherwise they will be interpreted as column names (or functions, or whatever).
Note that this is not safe against SQL Injection attacks, you should be using bind variables.
String whereClause = KEY_WORKSCREEN+" = ?";
Cursor cursor = db.query(TEXTVIEWS_TABLE, columns, whereClause,
new String[]{string}, null, null, null);
Because you do not put quotes around your whereClause.
Try:
String whereClause = KEY_WORKSCREEN+"='" + string + "'";
You can clearly see it in your error log:
SELECT _id, textviewvalue, categoryoptions, workscreen FROM interfacetable WHERE workscreen=testInterface1
should be:
SELECT _id, textviewvalue, categoryoptions, workscreen FROM interfacetable WHERE workscreen='testInterface1'
SELECT _id, textviewvalue, categoryoptions, workscreen FROM interfacetable WHERE workscreen=testInterface1
As error says you don't have testInterface1 collumn in interfacetable table, i think you should have value instead of testInterface1 in your sql statement. Run your query in db, and you will see the same error.
You need to put your string value in the where clause into quotes.
Workscren is string edit your query and add single quotes. Key-workscreen+"="'+ string +'" like that

erroring out when executing rawquery in sqlite

i am trying to execute rawquery in sqlite in android but its erroring out .
My SQL code is :
String sql = "select vaccine_name,vaccine_duration from table_vaccinelists where vaccine_type=";
String[] selectionArgs = new String[10];
selectionArgs[0] = "'C'";
Cursor cur = database.rawQuery(sql, selectionArgs);
Error :
Its giving "Null pointer exception" .i am new to android and i feel there is something wrong the way i have written the where clause in the query .
Yes, there is something wrong with your query: selectionArgs are there to replace question marks in your query.
String sql = "select vaccine_name,vaccine_duration from table_vaccinelists where vaccine_type=?";
String[] selectionArgs = new String[]{"C"};
Cursor cur = database.rawQuery(sql, selectionArgs);
However, it shouldn't cause NPE, so something else might be wrong. I think your database variable is null, check the way you initialise it.

Categories

Resources