Selecting int values using a cursor query - android

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

Related

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.

Sqlite select email column from content provider

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.

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 = ?)"

Queue SUM of SQLite column - android

I have a listview populated from an SQLite database. I have several items that I successfully populate into the listview, however I'm having trouble with one last thing.
I'm trying to queue the sum total of the column KEY_CONTENT6 which is a string type, however it only contains numbers. I'd like to keep it as a string, so to add it up I'm using Double.valueOf(). The problem is this code force closes on queue and I cant figure out whats wrong:
public Cursor queueAll(){
String[] columns =
new String[]{KEY_ID, "sum("+ Double.valueOf(KEY_CONTENT6) +")",
KEY_CONTENT9, KEY_CONTENT10 };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null , null, KEY_CONTENT10, null, KEY_CONTENT9+ " DESC");
return cursor;
}
simply use SUM, no need to use anything else..
String[] columns =
new String[]{KEY_ID, "sum(KEY_CONTENT6)",
KEY_CONTENT9, KEY_CONTENT10 };
It is valid for SQLite. Because, no matter what you set data type in SQLite, it stores values as string. So, type conversion is somewhat built-in in SQLite.
You can't use java in a SQL statement, either stick to strait sql or iterate over the cursor and use java to do your calculation.
You can find everything there is to know about sqlite here http://www.sqlite.org/docs.html
SQLite is basically typeless, so you might be able to use SUM on your column even though it is a string. However, if it's meant to be a numeric column, why not give it a number type??

Android Sqlite get last insert row id [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get generated id after insert
I want to get the last inserted row id in my android application using this code :
String query = "SELECT * from SQLITE_SEQUENCE";
int createdUserId = Integer.parseInt(dbHelper.executeSQLQuery(query).toString());
but the problem is that it's throws an exception that cannot convert dbHelper.executeSQLQuery(query).toString() to integer. I'm not really good at sqlite ,but i think that this should return the last row id which was inserted...which will definitely will be int (at least I think this way). So if this is not the right way, can someone please guide me how to get the last row id in android application.
Thanks!!!
Your SQL statrment will return all the row ids, not just the latest. Try something like this...
SELECT ROWID from SQL_LITE_SEQUENCE order by ROWID DESC limit 1
Also note that I believe selecting from SQL_LITE_SEQUENCE will get the latest ID from ANY table, you can also access the SQL_LITE_SEQUENCE by selecting ROWID on any table, and getting just the IDs for that table. IE
SELECT ROWID from MYTABLE order by ROWID DESC limit 1
And thanks to MisterSquonk for pointing out the next step in the comments, adding it here for ease of reference later...
The query statement will then return a Cursor object containing the results, so to access the integer value you would do something like this (I'll substitute more common methods for your helper method, just for others sake)
String query = "SELECT ROWID from MYTABLE order by ROWID DESC limit 1";
Cursor c = db.rawQuery(query);
if (c != null && c.moveToFirst()) {
lastId = c.getLong(0); //The 0 is the column index, we only have 1 column, so the index is 0
}
(Note that although the SQL Lite docs call ROWID and Integer, it is a 64 bit integer, so in Java it should be retrieved as a long.)
Care to use "selectlast_insert_rowid()"? :)

Categories

Resources