-1

I have a database which contains multiple phone numbers. I need to check if the database contains a specific number. How do I get the database values in an if statement? Should i use Array List?

This is what i have been doing so far..

@Override
public void onReceive(Context context, Intent intent) {
Bundle bund=intent.getExtras();
String space="";

if(bund!=null) {  
Object[] smsExtra = (Object[]) bund.get(SMS_EXTRA_NAME);

for (int i = 0; i < smsExtra.length; i++) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
body = sms.getMessageBody().toString();
address = sms.getOriginatingAddress();

db = openOrCreateDatabase("Admindb", MODE_PRIVATE, null);
Cursor crs = db.rawQuery("select * from adminstbl", null);
String[] str= new String[crs.getCount()];
crs.moveToFirst();
for(int k=0;k<str.length;k++)
{
str[k] = crs.getString(crs.getColumnIndex("Number"));
crs.moveToNext();
}

if (address.equals(str)){
 //do my task here
}
shinilms
  • 1,494
  • 3
  • 22
  • 35
  • 1
    you want to use SQL to do a `query` on your database. http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow . You'll want to make a `selection` based on the specific number – Blundell Nov 28 '15 at 17:42
  • what im trying to do is that the user enters some phone number at the start of the app and when a sms is received i want to check if the sms is from any of the number which the user entered earlier and do some tasks based on that. how can i do it?? – shinilms Nov 28 '15 at 17:49
  • You can do it by reading the url I linked above. Then your next question will probably be like this: http://stackoverflow.com/questions/1243199/how-to-perform-an-sqlite-query-within-an-android-application – Blundell Nov 28 '15 at 17:50

1 Answers1

1

First, in your activity code, you can assess your database with a cursor.

 Cursor numberCursor = mDbHelper.fetchRecord(mPhoneNumber);

Here is a sample of code that you would have in your database handler.

public Cursor fetchRecord(long mPhoneNumber) throws SQLException {

    Cursor mCursor = mDb.query(true, ACCOUNTS_TABLE, new String[]{
                    ACCOUNTS_ROWID, ACCOUNTS_NAME, ACCOUNTS_ADDRESS,
                    ACCOUNTS_PHONENUMBER}, ACCOUNTS_PHONENUMBER+ "=" + mPhoneNumber, null, null,
            null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

The part that reads ACCOUNTS_PHONENUMBER+ "=" + mPhoneNumber in the cursor query is looking for any records that match the number that you passed in from the activity. The cursor will only return records with that number.

Finally, back in your activity, after the first line of code shown above you can put an if statement to check for a returned value.

  mReturnedNumber = numberCursor.getLong(3);
    if (mReturnedNumber != null){
// You may now use the number here since it is not null
}

For more information on Sqlite queries, follow this link.

Here is an example of the code for one of the comments below.

 Cursor mCursor = mDb.query(true, ACCOUNTS_TABLE, new String[]{
                ACCOUNTS_ROWID, ACCOUNTS_NAME, ACCOUNTS_ADDRESS,
                ACCOUNTS_PHONENUMBER}, ACCOUNTS_PHONENUMBER+ "=" + mPhoneNumber + " OR " + ACCOUNTS_PHONENUMBER_2 + "=" + mPhoneNumber, null, null,
        null, null, null);

Notice the query now has an OR statement.

Eric Engel
  • 305
  • 4
  • 15
  • thank you for the answers. i will try it and let you know if it works – shinilms Nov 28 '15 at 18:07
  • 1
    @Shinil - The way I have used the query statement, only a matching value will be returned. There is no need to write code that sifts through all records to find a match. Study the information at the link I put in my answer and I think you will find that the Sqlite framework will make your life easier. – Eric Engel Nov 28 '15 at 18:10
  • but can i check multiple numbers with this? like more than one phone number? – shinilms Nov 29 '15 at 01:56
  • @shinil - can you give me an example? Do you mean multiple numbers in one query? – Eric Engel Nov 29 '15 at 03:38
  • lets assume the user adds 4 different phone numbers at the start of the app. and when an sms is received, i need to check if the sms is from any of the number the user added earlier. that is, if the originating address of the sms matches any of my numbers in the database, then i need to perform some actions,. or else do nothing. – shinilms Nov 29 '15 at 06:19
  • I would assume that the 4 numbers were entered into the database for that user. Then you just need to search the database for a match on the number of the incoming sms. Depending on how you structure the database tables, you may have multiple entries for users with more than one number or you could allow up to a fixed amount of numbers per user and keep each user on one row. If the user has multiple numbers on one row (more than one column of numbers) then you just need to have the query look at each number column for that number. I am including an example of the syntax to the latter case. – Eric Engel Nov 29 '15 at 15:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96472/discussion-between-tequilaman-and-shinil). – Eric Engel Nov 29 '15 at 15:39