Chris Jünger
Chris Jünger

Reputation: 3

android database delete row by content

Hey Guys ive got a problem with my database. iam displaying my database in a textview looking like:

 hh:mm dd:MM:yyyy text
 12:14 12.12.2014 awdawdawd
 13:12 13:12:2015 awdaw awdw

onclick iam getting the text by:

StringBuilder ortsplit = new StringBuilder();
String item = ((TextView) view).getText().toString();
            String[] itemsplit = item.split("\\s+");
            String uhrsplit = itemsplit[0].toString();
            String datumsplit = itemsplit[1].toString();
            ortsplit.setLength(0);
            for (int i = 2; i < itemsplit.length; i++) {
                ortsplit.append(" " + itemsplit[i].toString());
            }
            String sortsplit = String.valueOf(ortsplit);

then iam opening my database:

datasource.open();
                datasource.findedel(uhrsplit,datumsplit,sortsplit);
                datasource.close();

my datasource.findedel: public void findedel(String pZeit, String pDatum, String pOrt) {

    database.delete("TABELLE", "UHRZEIT="+Zeit +"AND DATUM="+Datum+"AND ORT="+Ort,null);

}

ive got no "id" displayed in the rows, earlier it looked like:

 1    hh:mm dd:MM:yyyy text
 2    12:14 12.12.2014 awdawdawd
 3    13:12 13:12:2015 awdaw awdw

and ive just took the "id" and searched my entries for that id = id and deleted the row, but since i deleted the first row i want to search the row by the content.

any1 got a solution for my problem?

Upvotes: 0

Views: 65

Answers (1)

You have multiple errors and also you are prone to SQL injection.

You must use prepared statements or you must add quotes to your strings and escaping the quotes the string has, for example, in your code:

database.delete("TABELLE", "UHRZEIT="+Zeit +"AND DATUM="+Datum+"AND ORT="+Ort,null);

this: DATUM="+Datum+"AND is bad coded, there is not space between Datum and AND so, if datum is equal to test, then you string will be like this: DATUM=testAND. That will return syntax errors in mysql, and also string must be quoted like this: DATUM='test' AND.

The main problem of quoting this way is that if Datum has quotes by itself, you will have errors too. For example, if Datum equals to te'st then your string is going to be like this: DATUM='te'st' AND. As you see, you will have 3 quotes and then will return syntax error.

You must read and understand this before going further, because you will end up with a really messy code plenty of errors and vulnerabilities: http://wangling.me/2009/08/whats-good-about-selectionargs-in-sqlite-queries.html

Good luck ;)

And also, in Java all variable names must start in lowercase (Instead of String Datum use String datum)

Upvotes: 1

Related Questions