![]() Note: Since you want to delete multiple records (sometimes you need to delete 10 000 and more rows) try to think about TRANSACTIONS which rapidly increase performance and security. SQLiteStatement stm = db.compileStatement(b.toString()) Same as above but you can use SQLiteStatement: SQLiteDatabase db // instantiated SQLiteDatabase StringBuilder b = new StringBuilder("delete from table where _id IN(") Second approach: List ids = new ArrayList() Here you can create "dynamic" statement due to size of ids (stored in some dynamic array).įirst approach: List ids = new ArrayList() ĭb.delete("table", "_id = ?", new String ) Since Android API 11, there is method that allows delete multiple records.Create dynamically query with IN clause.delete records in loop (API is less than 11).Finally, you have row's ids you want to delete.īut, right now you don't know exactly how many rows you want to delete so you need to: I prefer at first query a table and place simple if condition if between actual date and date stored in row is difference 2 days then save his rowId. Then there are more approaches how you can do it. Since you want to delete rows due to specified timestamp i suggest you to insert each row with specified date in certain date format (you can simply use SimpleDateFormat. And as Rathod pointed out, your delete statement is not valid statement. Second, for delete rows from table use API build-in function delete(). This is very "huge" question and can have more working approaches depend on your personal requirements e.q security, performance etc.Īt first i recommend to you use parametrized statements instead of your "harcoded" which are unsafe and not much human readable, so use placeholders e.q. PS i also tried doing by execSQL statement but was not able to write the complete queryīy database.execSQL("Delete * from "+TABLE_NAME+" WHERE = "+currentdate - Long.ValueOf(?) >2*24*60*60 ") I would be really grateful if someone can tell me a even better and simple approach than this. You can tell you’ve selected it because it should be highlighted in blue. Go back to the Database Structure tab, and click on the adinfo table. I am not sure what should i write in whereClause and whereArgs. Since SQLite Browser automatically imports all columns in all tables as TEXT, we need to manually change the data type of the non-text columns. So i am planning to do something like this long currentdate =new date().getTime() Īnd than check the difference between currenttime-Long.Valueof(TIMESTAMP_DB) field for each rows of the table and if it is more than 2*24*60*60 than delete that rowĬan someone please tell me how can i use the below function to achieve the above public int delete (String table, String whereClause, String whereArgs) Now when i start my app i want that all rows that were added more than 2 days ago should be deleted Private final String createDb ="create table if not exists "+TABLE_NAME +" (" Private static final String DATABASE_NAME ="database.db" Private static final String USERRATING_DB="userrating" Private static final String TIMESTAMP_DB="time" Private static final String JSONOBJECT_DB="json" Private static final String Name_DB ="name" ![]() ![]() Private static final String POSITION_DB ="_id" I am new to android and i am having trouble understanding the delete function in sqlite database.I have created my database using the string private static final String TABLE_NAME="NameInfo" ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |