adrian
adrian

Reputation: 4594

Store numbers that start with 0 in sqlite android

Lets say I have stored in a sqlite database the following records as Strings:

09575788
08902149
08902134
47889789
47889776
47889797
48250496

My task is to check if these values exist in the database. So i have build one method that checks for the existence of these values.

So the user insert one of these value in the editText and I have to check if the value exists.

The problem that appears is that when I try to search for a number that start with "0" the query always returns null, but when I search for any other number - that doesn't start with "0" the query finds the value in db.

This is my code:

 Cursor cursor = this.db.query(TABLE_NAME, new String[] {KEY1, KEY2, KEY3},
                KEY_CONDITION1 + " LIKE " + prefix1 + " AND " + KEY_CONDITION2 + "=" + prefix2, null, null, null, null);
        return cursor;

Any idea how to solve? thanks for your answer

IMPORTANT All the values are stored as String in database

Upvotes: 2

Views: 1681

Answers (1)

Tim
Tim

Reputation: 8919

@Adrian, the correct answer was given to you by Alex K, but you have misunderstood him, I think. If the leading zero is meaningful (e.g. as it would be for a US zip code that can begin with a zero) then you do not have a number, rather you have a string. The data affiliation of such a column should be text (aka varchar). You do not want to cast the value with a leading zero to an integer; you simply want to look for the string value by wrapping the search term in single quotes; for example:

          ...  where zipcode = '01287'

Upvotes: 3

Related Questions