Sqlite select email column from content provider - android

I'm trying to select some data from sqlite, where the column represents an email address.
My query is like this:
Cursor countCursor = mContentResolver.query(
SubscribeContract.SubscribeEntry.CONTENT_URI,
new String[]{"count(*) AS count"},
"user = ? ",
new String[]{ userChatID },
null);
But it's returning nothing.
Querys on sqlite3 terminal is returning ok.
sqlite> select count(*) as count from subscribe where user = 'brunox17_7a84e0c5e635fb571b32f5be9d55dd0b734c2f57#boo-app.com';
count
21
I think the problem is because the column type is text, and I cant put quotes surrounding the selection arguments.

What API version are you testing on? The parametrized queries are supported from API11.
You can try the getCount() method on the Cursor object without specifying any where clause.

Related

Android SQLite rawquery not working for a query that works in sqlite3 command line & dbeaver

I am writing an android app for a condo complex to aid locating utilities for residents. I have multiple tables and am having trouble retrieving data from 1 of them, but only for 1 query out of 3. Note that I am using SQLiteDatabase.rawquery. Also, the query works in sqlite3 command line and dbeaver. The first usage of the app should have the column 'yours' all 0's (boolean false) and let the user later set theirs to 1 (true). So, first call should return no rows. Later calls 1 row.
Table definition is as follows:
CREATE TABLE units (
building int4 NOT NULL,
unit int4 NULL,
stack int4 NULL,
unit_id int4 NOT NULL,
stack_id int4 NOT NULL,
yours int4 NULL,
CONSTRAINT units_pk PRIMARY KEY (unit_id));
Query that fails in android but works in command line & dbeaver is as follows. I copied the non-args version straight into command line and it works aside from adding the semicolon. The args variation did not work either.
String query = "SELECT * FROM units WHERE yours = 1";
//String query = "SELECT * FROM units WHERE yours = ?";
//String[] args = new String[]{"1"};
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery(query, null);
//cursor = db.rawQuery(query, args);
The following 2 queries work, one with the asterisk (*) wildcard.
List<String> units = new ArrayList<>();
String query = "SELECT unit FROM units WHERE building = " + buildingSelected
+ " ORDER BY unit";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor;
try {
cursor = db.rawQuery(query, null);
cursor = db.rawQuery("SELECT * FROM units ORDER BY unit_id", null);
I am at a loss as to what is wrong and can't find an answer on the internet based upon my questions.
10/2/2019 Added the following to explain "not working".
From sqlite3 command line, ran the SQL statement to select all rows where yours = 1. No rows come back as expected since all rows have yours = 0 (false). Then update 1 row to set yours to 1. Ran same SQL statement and get 1 row as expected.
SQLite version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
sqlite> .tables
electric_meters gas_meters units water_valves
sqlite> SELECT * FROM units WHERE yours = 1;
sqlite>
sqlite> UPDATE units SET yours = 1 WHERE unit_id = 4561205;
sqlite> SELECT * FROM units WHERE yours = 1;
4561|205|5|4561205|45615|1
sqlite>
Run UPDATE again to put row back to yours = 0 to run app in studio. Updated code after rawQuery call to getCount() in variable rows.
cursor = db.rawQuery(query, null);
//cursor = db.rawQuery(query, args);
int rows = cursor.getCount();
And while I am gathering this information, I found a code error that was causing issues with the debug output. While the data looks valid with the right formats, I am not expecting rows to return but the 2 rows returned do have yours = 1. Note the code error was after the rawQuery call and during the extraction of the values in cursor into a Unit class instance.
This is probably bad form to change my question in the middle. The database I am looking at through sqlite3 command line is in the assets directory of the project. The app is using the one on the AVD under /data/user/0/.... They might be different. I have done a clean project a few times. I thought that would reset everything, including updating the database the app uses in the AVD. Can someone tell me how to make sure the database in the project gets repopulated into the AVD data structure?
I found a solution to my issue. Turns out the database in the AVD file structure was not getting updated when I thought it would: clean project, synchronise files, etc. The AVD database had values for yours = 1, so it was responding correctly. Ended up deleting the databases and then uploading from the assets project folder. Probably a better way, but I could not find it. Thanks.

Unable to delete row from SQLite DB via delete() - Android

I have a sqlite db with 3 tables. TABLE1, TABLE2 and TABLE3.
I am using the following API to delete row(s) from the DB:
int rowsAffected = db.delete(tableName, "columnName=?", new String[] {String.valueOf(shoeId)});
Now, when I run this API for TABLE1 and TABLE2, the rows get deleted fine and I get the correct number in rowsAffected. But when i used this API for TABLE3, I always get rowsAffected as 0 and no entries are deleted.
int rowsAffected = db.delete(tableName, "columnName=?", new String[] {String.valueOf(shoeId)});
All 3 tables have the shoeId column.
Could someone please help?
Thanks.
The second parameter is the where clause, so you query should look like this :
int rowsAffected = db.delete(tableName, "nameOfColumn=?", new String[] {String.valueOf(shoeId)});
This means : "DELETE FROM tableName WHERE nameOfColumn = String.valueOf(shoeId)"
Don't remove the =?, it is used for prepared statements
The above query was not working due to the following reasons:
Lets say the columnName here is busTicketId and its data type is Int.
When the DB was created, the busTicketId was erroneously created as default String type due to an error in the Create table query.
As a result when the time came to delete busTicketId, it was trying to search for busTicketId, which was of Int type and since it could not find any, the rows affected count always turned out to be 0.
Once the data type issue for this column was fixed while creating the table, this issue also got fixed.

Selecting int values using a cursor query

I am new to Android, and having some basic problems. One of them is the use of queries.
I store a boolean value as either 1 or 0 in the table (INTEGER field). However, when I select either on 1 or 0 using the query below I get no results. What am I doing wrong?
Cursor cursor = _db.query(_objectName, _fields.keySet().toArray(new String[0]), "parentId=? AND published=?", new String[] {String.valueOf(menuItem), String.valueOf(1)}, null, null, "level");
There is nothing wrong with your query. The problem must be elsewhere. Check your code and table structure. Maybe you are not sending the right values for parentId and published columns or the data in the table is not in the format you expected.
Use raw query
"Select * from "+TABLE_NAME+" where published = '"+String.valueOf(1)+"'";
You can put your integer value insted of 1

Android Sqlite subquery does not return any value

I am using the following query to get the list of item
Query="Select * from Item_List where idSubcategory = (Select _id from SubCategory_List where Name = ?)";
c = db.rawQuery(Query, new String[] { Search });
But it is returning no result.
I ran this query on the database which I pulled from the device as well as emulator. There I am getting proper result, But when I run it on device no data is returned.
Can you try a different way? Use execSQL() as explained in this link. It is exactly the same problem.
EDIT
Unfortunately i didn't notice that execSQL() return no data (the work day is ending, finally!). The only solution i have is to do a first query and the result of this one will be the parameter of second query.
EDIT 2
Try to add "IN" to your query, like this:
Query = "Select * from Item_List where idSubcategory in (Select _id from SubCategory_List where Name = ?)"

SQLIte Query, select everything that equals - Android

// get friends
public Cursor getFriends(){
Cursor c = dataBase.query(SqConstants.FRIENDS_TABLE_LOCAL, null, null, null, null, null, null);
return c;
}
I have multiple tables in a SQLite database in my application for android.
I want to select everything from a column in a Friends Table (which holds integers) and select everything that equals that integer from a User Table (in the primary column which also is integer) and the get all the users which equals the numbers stored in the friends column.
It was some while I managed SQL databases but I think the query for a MySQL database would be something like:
SELECT * UsersID FROM TABLE Users EQUALS LOCAL_FRIENDS_ID FROM TABLE Friends
I might be wrong that it would look like this, but something like it.
What I want is to do this in android code, from a cursor get all the Users from the Users table which ID is equal to the FriendsID.
How do I change the above method to make it so?
You could just use the rawQuery
Cursor c = dataBase.rawQuery(SELECT * UsersID FROM TABLE Users EQUALS LOCAL_FRIENDS_ID FROM TABLE Friends, null)
If you need to use a actual parameter for the LOCAL_FRIENDS_ID
string[1] args = new String[LOCAL_FRIENDS_ID.ToString()];
Cursor c = dataBase.rawQuery(SELECT * UsersID FROM TABLE Users EQUALS ? FROM TABLE Friends, args)

Categories

Resources