My Android app is crashing with this error:
E/AndroidRuntime( 315): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x3470a0
...
E/AndroidRuntime( 315): at lee.medical.icu.dataentry.db.PatientInfoDbHelper.getTests(PatientInfoDbHelper.java:163)
It's stumbling on this bit of code:
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {C_LOCATION, C_TESTS};
String selection = "? = '?' and ? = '?'";
String[] selectionArgs = {C_LAST, lastName, C_FIRST, firstName};
Cursor cursor = db.query(TABLE, columns, selection, selectionArgs,
null, null, null); // line 163
What could this error mean?
(A couple of notes: C_LOCATION, C_TESTS, C_LAST, and C_FIRST are columns in my database. lastName and firstName are method arguments that are self-explanatory.)
Edit: I had to play around with antlersoft's solution a bit to make it work. In case anyone else having the same problem stumbles upon this question, the solution in my case was to change the selection to:
String selection = C_LAST + " = ? and " + C_FIRST + " = ?"
Don't put your ? in single-quotes for string arguments; in single quotes the ? is taken as a literal string and not as an argument placeholder-- SQLite will know the type of the argument passed as ? and so you don't have to quote it.
The way you have written it there are four arguments and only two argument placeholders in your query and so the exception.
Related
If someone knows a better way to get a rowId from text in the row, please let me know.
I've been running around in circles with this and I know it's probably something simple, but I can't figure it out. Hoping someone can tell me what I'm doing wrong. I'm getting an error running this SQLite code:
String where = "SELECT rowid, * FROM masterRecord WHERE masNameCol=" + name;
Cursor c = db.query(true, masterName, ALL_KEYS_MASTER, where, null, null, null, null, null);
The error points to the second line.
"name" is a string variable (in this case it's "Mary"). The exact error I'm getting is:
SQLiteLog: (1) near "SELECT": syntax error in "SELECT DISTINCT _id, masNameCol, masTotalTimeCol FROM masterRecord WHERE SELECT rowid, * FROM masterRecord WHERE masNameCol=Mary"
I've tried every syntax change I could find and think of, and it never changes the error. I'm just trying to get the rowId of the row so I can change a value in another column.
Use rawQuery(), not query().
You are trying to specify the entire SQL statement, which is what rawQuery() is for. query() assembles the SQL statement from pieces, and your one piece (where) is not just the WHERE clause.
Use placeholders for queries:
where = "masNameCol = ?";
whereArgs = new String[] { name };
columns = new String[] { "rowId" , /* all other column names you are interested in */ };
Cursor c = db.query("mytable", columns, where, whereArgs, null, null, null);
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.
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.
I'm writing a method to update default settings in a table. The table is very simple: two columns, the first containing labels to indicate the type of setting, the second to store the value of the setting.
At this point in the execution, the table is empty. I'm just setting up the initial value. So, I expect that this cursor will come back empty. But instead, I'm getting an error (shown below). The setting that I am working with is called "lastPlayer" and is supposed to get stored in the "SETTING_COLUMN" in the "SETTINGS_TABLE". Here's the code:
public static void updateSetting(String setting, String newVal) {
String table = "SETTINGS_TABLE";
String[] resultColumn = new String[] {VALUE_COLUMN};
String where = SETTING_COLUMN + "=" + setting;
System.err.println(where);
SQLiteDatabase db = godSimDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(table, resultColumn, where, null, null, null, null);
System.err.println("cursor returned"); //I never see this ouput
\\more
}
sqlite returned: error code = 1, msg = no such column: lastPlayer
Why is it saying that there is no such column lastPlayer? I thought that I was telling the query to look at the column "SETTING_COLUMN" and return the record where that column has a value "lastPlayer". I'm confused. Can somebody straighten me out? I've been looking a this for an hour and I just don't see what I am doing wrong.
Thanks!
You're not properly building/escaping your query. Since the value lastPlayer is not in quotes, your statement is checking for equality of two columns, which is what that error message is saying.
To properly build your query, it's best to not do this manually with String concatenation. Instead, the parameter selectionArgs of SQLiteDatabase.query() is meant to do this.
The parameters in your query should be defined as ? and then filled in based on the selectionArgs. From the docs:
You may include ?s in selection, which will be replaced by the values
from selectionArgs, in order that they appear in the selection. The
values will be bound as Strings.
So, your code would look like this:
String where = SETTING_COLUMN + " = ?";
Cursor cursor = db.query(table, resultColumn, where, new String[] { setting }, null, null, null);
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.