SQLite .query() method, WHERE clause is only taking double quotes strings - android

I have variable:
String owner="Mike";
String[] columns ={"quantity", "price","owner"}
My cursor is trying to get
Cursor findEntry = db.query("sku_table", columns, "owner="+owner, null, null, null, null);
I got an error no such column error
android.database.sqlite.SQLiteException: no such column: owner: , while compiling: SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike
But if I take this query:
SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike
and add "" to Mike, and tested in sqlite browsers to execute the query, I do get back the row.
The working query looks like this:
SELECT quantity, price, owner, FROM sku_table WHERE owner="Mike"
Can somebody drop some insights about how do I incorporate double quotes? Other than use \"
Thanks!

Sorry, but that is exactly the reason why you should work with what the method offers! #Leandros and #Jake are helping in the totally wrong direction! Sorry to say that...
The only solution you should use is this:
Cursor findEntry = db.query("sku_table", columns, "owner=?", new String[] { owner }, null, null, null);
ps: Yes I down voted both answers as they may work but providing a solution that shouldn't be used.
Update:
If you need more than one where condition, just add it like you would do in a normal query
Cursor findEntry = db.query("sku_table", columns, "owner=? and price=?", new String[] { owner, price }, null, null, null);
The order of the ? and the new String[] {...} elements must be the same!

Cursor findEntry = db.query("sku_table", columns, "owner='"+owner+"'", null, null, null, null);

SELECT quantity, price, owner, FROM sku_table WHERE owner='Mike' this is the correct SELECT. You forget the ' ' (single quotes)

public Cursor show_vol(String vol,String bk,String hadnu)
{
SQLiteDatabase db = this.getReadableDatabase();
String[] columns ={"hadith"};//colums name that you select
Cursor res = db.query("volume2", columns, "hadithno=?", new String[] { hadnu }, null, null, null);
//volume2 is table name and hadithno is colume name l
//select hadith from volume2 where hadithno=hadnu //working like s
1. List item
return res;
}

I know this is an old question, but you can also do it like this:
Cursor findEntry = db.query("sku_table", columns, "owner=\'"+owner+"\'", null, null, null, null);
I just did it in my app and it worked as expected.
Jake's answer was similar, but probably wouldn't work without the \ before the '

the simplest way is to use SELECT col1,col2 FROM table_name WHERE col =' something' ; just like Leandros said , my problem was the single quotes , thnx

Related

syntax error in "SELECT DISTINCT _id near "SELECT"

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);

How to fix sqlite query error where the ? are not replaced by the selection arguments

My app is a dictionary and now I am working on making it available offline. The database is downloaded and now we have the situation that there is no internet. I need to use the local database. I get my readable database, and then set up a query. On my own testing phone everything works fine. But now before publishing the update I was trying it on two other phones, just to see that the ? in the selection are not replaced by the values in the selectionArgs.
The debugger just shows that the sql statement was built without replacing anything in the selection.
It works on my phone on Android 8.1, but with the two phone that are lower, it stops working and I am out of ideas.
I also tried changing it to db.rawQuery() but that ended with the same result.
String selection = "eng LIKE ? OR oky LIKE ? OR engpl LIKE ? OR okypl LIKE ? OR engcom LIKE ? OR okycom LIKE ? OR Alternative LIKE ?";
String[] selectionArgs = {term, term, term, term, com, com, term};
String orderBy = "eng, oky ASC";
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null, orderBy);
Now I expected the result to give me the terms that fit to the word (term) that was searched for by the user, but instead the cursor is empty, because there is no term containing a question mark.
Especially confusing is that it works on one phone and not on others.
The debugger won't show the resolved ?'s as the replacement of the ?'s is done by the underlying SDK (sqlite implementation) which is in C rather than java.
The best that you can see are the bindArgs e.g. for the following code :-
csr = mDBhlpr.getWritableDatabase().query("table1",null,"id=?",new String[]{"1"},null,null,null);
while (csr.moveToNext()) {
long id = csr.getLong(csr.getColumnIndex(DBHelper.COL_TABLE1_ID));
}
with a breakpoint at the 1st line then you could get :-
I don't believe that the issue is with the binding of the arguments, rather that it is elsewhere. You could test this by changing :-
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null, orderBy);
To
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, null, null, null, null, null);
DatabaseUtils.dumpCursor(cursor);
i.e. select all rows, and them write all the rows in the Cursor to the Log.
If still none then the tables on the other devices are empty.
If an Exception occurs then you would have to investigate that.
If this works as expected, then progressively (one by one) add the selection criteria and args.
e.g. first would be
String selection = "eng LIKE ?";
String[] selectionArgs = {term};
String orderBy = "eng, oky ASC";
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null, null);
then :-
String selection = "eng LIKE ? OR oky LIKE ?";
String[] selectionArgs = {term, term};
String orderBy = "eng, oky ASC";
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null,null);
and so on (note finally re-add the order by).
If none of the above works, then your issue (as expected) is elsewhere.

paging in android sqlite

How to use paging in android sqlite? any example code directly in java?
I am using
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS,
KEY_WEIGHT}, null, null, null, null, KEY_DATE + "DESC", ???);
Here there is a similar question/answer Limit Records fetched in Android (Sqlite database)
Some of the suggestions there are to use LIMIT ... OFFSET in your query, or to create your custom Cursor Adaptor. I guess it depends in which level you want to do the paging.
You can use the LIMIT keyword with index and no. of rows to fetch certain records, it goes something like this:
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS,
KEY_WEIGHT}, null, null, null, null, KEY_DATE + "DESC LIMIT index, No. of records to fetch", ???);
Hope this helps.

Android sql query using WHERE clause for greater than

I have a question regarding how to use a WHERE clause when querying a sql database in Android. I need to return specific records from my database where the value of DURATION is greater than 3.
It works fine when I have the WHERE clause for checking equals.
Example
Cursor resultOfFilterQuery = db.query(myTable, new String[] {call_cost, call_type,
date,DURATION , phone_number }, phone_number= , new String[]{"9456788909"}, null, null, null);
Please let me know how to check for greater than
How should the query statement look?
Cursor resultOfFilterQuery = db.query(myTable, new String[] {call_cost, call_type,
date,DURATION , phone_number }, DURATION> , new String[]{3}, null, null, null);
Don't know how your first code snippet work with syntax errors, but this can helps:
Cursor resultOfFilterQuery = db.query(myTable,
new String[] {call_cost, call_type, date, DURATION, phone_number },
DURATION + "> ?", new String[]{"3"}, null, null, null);

Android SQLiteDatabase: Reading one column

I have a one row database just for saving app data. My goal is to read one column (one value) from it.
This query returns all the columns in a Cursor:
public Cursor readAll() {
return getReadableDatabase().query(tableName, null, null, null, null, null, null);
}
It returns a Cursor with one row in it, just perfect. However, I don't want to read all columns at once, because it's slow as I have blob's in db too.
Instead, I'd like to read just one column at a time, separately. For example, for a column called "TEXT" it would be this:
public Cursor readText() {
String[] projection = new String[]{"TEXT"};
return getReadableDatabase().query(tableName, projection, null, null, null, null, null);
}
However, this won't work, as I get back a Cursor with zero rows.
So, how to read a specific column from SQLiteBatabase in Android?
public Cursor readText() {
return getReadableDatabase().rawQuery("SELECT colName FROM myTable", new String[] {});
}
Syntax seems to be correct. Check please that you use right name of the column. Showing the table generation code and actual query code could help.
You can use this one also
public Cursor readText() {
return getReadableDatabase().rawQuery("SELECT column_name FROM table_name", null);
}

Categories

Resources