I am writing an SQL query like this:
Cursor data = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, WhereWolfOpenHelper.FRIEND_GROUP_COLUMN+"="+params[0], null, null, null, null);
And it's producing the following error:
ERROR/AndroidRuntime(620): Caused by: android.database.sqlite.SQLiteException: no such column: Friends: , while compiling: SELECT * FROM ww_friend WHERE friend_group=Friends
There is no column called Friends, I want to retrieve the rows where the friend_group column has the value Friends. What am I doing wrong?
you need to surround Friend value with quote :
Cursor data = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, WhereWolfOpenHelper.FRIEND_GROUP_COLUMN+"='"+params[0]+"'", null, null, null, null);
PS: be sure to escape dangerous characters from param[0] when you do like this to avoid SQL Injection
I'm guessing that "Friends" is a value, so you must enclose it within quotes.
Try this:
Cursor data = db.query(WhereWolfOpenHelper.FRIEND_TABLE_NAME, null, WhereWolfOpenHelper.FRIEND_GROUP_COLUMN+"=\""+params[0]+"\"", null, null, null, null);
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);
I got an sqlite exception while executing the query given below.
Cursor c=db.query("patient_tbl", new String[] {" first_name"},
null, null, null, null,"DESC");
Exception is :
08-19 16:55:35.748: E/AndroidRuntime(2808):android.database.sqlite.SQLiteException:
no such column: DESC: , while compiling: SELECT first_name FROM patient_tbl
ORDER BY DESC
The last argument in db.query is orderBy which says how to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Hence you have to mention the column name also..
You just mentioned DESC only so you will get no such column SQLiteException
So Use
Cursor c=db.query("patient_tbl", null,
null, null, null, null,"first_name ");
When i insert one record then duplicate entry is generated that means total two same entries are display. My query for select statement is following:
How to make distinct record???
Cursor cur = db.query("timer", null, null, null, null, null, null);
To make a distinct selection define a boolean Variable like :
private boolean Distinct = true;
then change your query to :
c = db.query(Distinct,DATABASE_TABLE,cols,null,null, null, null,
null,null);
I am trying to get column info from a cursor, and getting a frustrating error. I want to select all columns containing $SEARCH. Here is the code:
Bundle b = getIntent().getExtras();
SEARCH = b.getString("searchtext");
Cursor c = mDBHelper.getReadableDatabase().query("table", null, "name="+ SEARCH, null, null, null, null);
For some reason, the cursor is throwing a runtime exception. here is the error:
12-30 03:55:00.357: E/AndroidRuntime(1302): Caused by: android.database.sqlite.SQLiteException: near "CAP": syntax error: , while compiling: SELECT * FROM kroger WHERE name=john
Not sure why this is happening, there's probably a very simple error in my code, but I'm not sure what it is. THanks for your help!
"name="+SEARCH will become name=CAP, which isn't a valid SQL expression. The naïve solution is to wrap the term in single-quotes:
"name='" + SEARCH + "'"
But this is subject to SQL injection attacks. Use the argument-passing facilities to pass the search term in an injection-free manner:
query("table", null, "name=?", new String[] {SEARCH}, null, null, null);
I'm getting an annoying error when trying to query some data in SQLite.
Here is my code:
Cursor cursor= db.query(TABLE_IMAGES, new String[]{"_id"}, "name" +" = "+compareToThis, null, null, null, null);
I'm just returning the cursor as a string.
The error is saying:
no such column: compareToThis: while compiling.....the statement
My question is: why is SQLite setting the compareToThis attribute as a column when it's just a value?
How can I fix this?
Thanks in advance.
Cursor cursor= db.query(TABLE_IMAGES, new String[]{"_id"}, "name" +" = ?", new String[]{compareToThis}, null, null, null);
The selection must include placeholder for parameter, and the next argument should be the array of parameters.
The solution by Vladimir works, however if you are like me and wonder why your approach did not work initially when it should have, here is why:
It is because it expects an integer unless you used (single or double) quotation marks to indicate that it is a string.
For example, in MySql this would return no results:
SELECT * FROM clients WHERE firstName = Bob; -- This will not work.
However when you surround it with quotations, it will return a result because it identifies Bob as a String literal.
Select * FROM clients WHERE firstName = 'Bob'; -- Single quotes work.
Select * FROM clients WHERE firstName = "Bob"; -- Double quotes as well.
Therefore for it to work, you would have to surround your compareToString with single quotes, as Muhhammad mentioned within the comments.
Cursor cursor= db.query(TABLE_IMAGES, new String[]{"_id"}, "name" +'" = "+compareToThis+"'", null, null, null, null);