Android: SqlLiteDatabase delete does not work for certain ID - android

I have a very strange bug in my code. Add/Remove to SqlLiteDb works well. But there is one entry I can't remove. I really have no idea how this is possible.
I add a value like this:
ContentValues values = new ContentValues();
values.put(COLUMN_ID, id);
getWritableDatabase().insert(TABLE_SUBSCRIPTIONS, null, values);
And remove a value like this:
getWritableDatabase().delete(TABLE_SUBSCRIPTIONS, COLUMN_ID + " = " + id, null);
If I call
addEntry("43.05071");
removeEntry("43.05071");
the db is empty.
But after
addEntry("43.06600");
removeEntry("43.06600");
43.06600 is still in the db.
When I execute
getWritableDatabase().execSQL("delete from "+ TABLE_SUBSCRIPTIONS)
the table is empty as expected even if it contained 43.06600.
I doubt that the ID per se causes the problem. But do you have any ideas what I could have overlooked?
Thanks in advance.
Update: I updated my remote database and changed the id from 43.06600 to 43.05072. No it works for the object which had the 43.06600 id. How is that possible?

Related

SQLite in Android can't delete row with multiple where clauses

Ok, so I'm trying to make my database delete an entire row if two conditions are fulfilled. I've tried using the delete(String tableName, String whereClause, String[] whereArgs) method as provided from the SDK
However, a row is deleted only if I put in ONE where clause, such as this:
mDatabase.delete(ItemTable.NAME, ItemTable.cols.NAME + " = ?",
new String[] {latestItem.getName()});
Wherein attempting to put in multiple WHERE clauses breaks the whole thing
mDatabase.delete(ItemTable.NAME,
ItemTable.cols.NAME + " = ? AND " +
ItemTable.cols.PRICE + " = ?",
new String[] {latestItem.getName(),
String.valueOf(latestItem.getPrice())});
I am completely out of ideas on how to tackle this. Any and all help is appreciated.
Ok guys, I figured out what was wrong.
Turns out the condition for checking the item name was returning TRUE but the item price condition wasn't. Apparently there was a mismatch with the sqlite data affinity of the itemprice() data.
I fixed this by forcing all column data affinities to be TEXT and storing my item price as a String through String.valueOf(getItemprice()). Everything works this way.
Thanks though for all your help!

Column columnname is not unique(code 19)

this is the code where I am trying to insert into my table and getting an exception that column ShopName(COL_SN) is not unique though I am giving a name that is not already existing in the database.That particular column is the primary key of the table
public void insert(String sn,String skn,String sa,String un,String pwd) throws SQLiteConstraintException
{
sdb=this.getWritableDatabase();
System.out.println("in insert method");
//sdb.execSQL("insert into " + TABLE_ShopDetails + " values(" +sn+ "," +skn+ "," +sa+ "," +un+ "," +pwd+ ")");
ContentValues cv=new ContentValues();
cv.put(COL_SN,sn);
cv.put(COL_SKN,skn);
cv.put(COL_SA,sa);
cv.put(COL_UN,un);
cv.put(COL_PWD,pwd);
sdb.insert(TABLE_ShopDetails,COL_SN,cv);
sdb.insert(TABLE_ShopDetails,COL_SKN,cv);
sdb.insert(TABLE_ShopDetails,COL_SA,cv);
sdb.insert(TABLE_ShopDetails,COL_UN,cv);
sdb.insert(TABLE_ShopDetails,COL_PWD,cv);
}
just call insert only once
sdb.insert(TABLE_ShopDetails,null,cv);
You should call insert() only once.
The ContentValues object already contains the values for all columns. By inserting multiple times, you're trying to create duplicate records, which results in a primary key violation.
The second parameter can be null, it's only for special cases (when values is empty).
You definitely only need to call insert once as others have said. The second optional parameter should most likely be null, it is for the following ...
optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
Also you might want to look into setting up a content provider. The link below would serve as a great tutorial.
Android SQLite database and content provider - Tutorial

How to update android database when fresh JSON content arieves?

Could someone suggest me a way on how to update my apps SQLite database when the content of the parsed JSON file is changed?
I am having a hard time finding the right solution, now I have to clear the cache before the app starts, but the end user obviously won't do that.
I asume changing the table version is not suitable for frequent updates (hourly).
Thanks!
As #KenWolf mentioned this documentation in the comments.
Update a Database:
When you need to modify a subset of your database values, use the update() method.
Updating the table combines the content values syntax of insert() with the where syntax of delete().
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
// Which row to update, based on the title
String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";
String[] selectionArgs = { "MyTitle" };
int count = db.update(
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);
Change your database version by incrementing by 1 from current value.
It will make possible of updation.

How to delete row from sqlite at listview Android

I have a listview and i am getting the data from sqlite database. My problem is to delete a row which user selected it from listview. I can delete all table by
dbConn.delete("Restobj", null,null);
But i cant delete a single row which is selected from listview.
Please Help
You essentially need to get the row id from the selected ListView item. Using the row id you can easily delete that row:
String where = "_id = " + _id;
db.delete(TABLE_NAME, where, null);
After deleting the row item make sure you get the ListView adapter cursor and do a requery. If you don't you will not see the updated table, hence updated ListView.
Make use of those other two parameters to the delete method. Take a look at the API documentation for more information.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#delete%28java.lang.String,%20java.lang.String,%20java.lang.String[]%29
Pass in something other than null.
Also, try searching on stackoverflow and/or Google for this topic. The answers are plentiful.
You need to supply the appropriate values to the database object. I'm assuming that dbConn is an instance of a database object. If that is the case, you can pass in dbConn.delete() with 3 arguments. The first argument is the table name. The second is the whereClause. This is something similar to:
"id = " + objectDatabaseId
The final variable in this case you can leave blank. The end result is something like:
String whereClause = "id = " + objectDatabaseId;
dbConn.delete("Restobj", whereClause, null);
As a side note, it's better to use constants when referring to table names and table columns as apposed to "Restobj" you should have something like RestObject.TABLE_NAME where the constant is defined as a static final String inside of the RestObject.
-Randall

slight misunderstanding with sqlite database in android

i created a database with 6 columns and i have a create and update method in my class that takes 6 parameters/arguments which represent these columns. my problem is that, anytime i try to update or create the database without using all 6 arguments (setting some to null), i get an error "constraint failed". this is most particular with the update method.
any ideas how i can get around this? because sometimes i don't want to fill all columns. I have removed the "text not null" constraint when creating the database. Thank you.
You're going to want to use the ContentValues to achieve this. Heres a quick demo.
My function
public boolean updateStuff(int id,ContentValues args) {
return mDb.update(TableName, args, _id_col + "=" + id, null) > 0;
}
And to call it. Note you can put as many ContentValues as you need
ContentValues initValues = new ContentValues();
initValues.put(col_key,col_value);
Edit:
mDB is a SQLiteDatabase

Categories

Resources