Monday, April 29, 2013

How to get contact phone number via contact name?

in some specific situations, if we got a bunch of contacts names, how can we get the matched phone number from the database?

the answer is yes, we may get it done by python sl4a script or sqlite3 command.

for sqlite3 command requirement, please check my previous entry for sure.

hereinafter it is the specific command to get the matches mobile phone number by name.

## type represents the phone number type, 2 means mobile phone number.

## LIMIT 1, cause there might be more than 1 mobile phone number for this contact person.

### %Contact_name is the variable set from tasker

sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db 'SELECT number FROM view_v1_phones WHERE name="%Contact_name" AND type=2 LIMIT 1';



via python sl4a script, it is also simple that way. if you wanna sepcify the search result for mobile phone number only, you may use for statement combined with find function with regex match.

### %Contact_name is the variable set from tasker
#### script content ###########

import android
droid = android.Android()
contacts = droid.queryContent('content://com.android.contacts/data/phones',['data1'],'display_name = "%Contact_name"',None,None).result
a = contacts[0]['data1']
print a
## returns specific phone number in interger

29 comments:

  1. I am trying to solve the same problem, but solution you described does not work to me and I do not know why. I am already installed SL4A and Python 2.6.2, but that script returned error when I used non-english characters (like á, ř, etc.). When I used only english characters, script returned IndexError: list index out of range. And i have another one problem. Any script which I try to run by Tasker do not variables returning back to Tasker. Please, have you any idea how to solve it?

    btw: This is very useful website. Thank you for it :)

    ReplyDelete
    Replies
    1. Hi,

      sorry for the late reply. regarding your questions.

      1st, non-english characters for sl4a script, by default, it is in ASCI coding format for english chracters. for non-english characters, i guess you need to define the coding in the first line of the script.

      2nd, IndexError: list index out of range, this erros denotes that the arguments you set doesn't exit, or out of range for search duo to the condition you set for the arguments. please check this out.

      3rd, run by Tasker do not variables returning back to Tasker, i already posted the details python sl4a script in other posts, please check it out.

      let me know if any other questions.

      Cathy

      Delete
  2. I use contact backup tool to export contacts to .csv file to sd card. after that I put file in to variable and use variable split to find correct number.

    ReplyDelete
    Replies
    1. Hi,

      sounds good, do anything as you like as long as it works.

      Cahty

      Delete
  3. Hi there,

    Could you please tell me how do I retrieve the thumbnail photo of a contact based on display name and then display it on a particular scene (using pyhton script or otherwise)?

    /cm

    ReplyDelete
    Replies
    1. Hi,

      tasker 4.0+ already supports this functionality.

      you may refer to my another post regarding popup sms notifier to see the details.

      Cheers!

      Cathy

      Delete
    2. Hi Cathy,

      I looked at the post named 'Another version of my Popup SMS notifier' where you are showing the picture but in the post I could not see how are you achieving the same. Probably you are using the 'test' action with profile pic being pulled based on phone number?

      However, in my use case I won't be able to use that action as I am dynamically generating a list of contacts based on certain input criteria (like partial last name) through DB query. If that is the case then how do I dynamically show the thumbnails on a particular scene element like menu element or something?

      /cm

      Delete
    3. Hi,

      please take some time to read again on my entries, you may set up contact name as a variable with regular express match. that could solve your puzzle.

      Cathy

      Delete
  4. If anyone is still searching of a way to achieve this, I found a simple way that doesn't involve any scripting. I made a text file with no spaces. It looked similar to this....

    Dave5554443333DaveMom5558881111MomDad5552229999DadJohn5556668888Johnetc.

    In tasker if you want to look up a contact that you have saved as a variable...say %name, you simmply read the file to a variable %number. Then do a variable split by %name. Now the correct number is saved as the variable %number2. The downside is that you have to set up your most popular contacts into the text file. Could take 10-20 minutes but works very well and doesn't require any additional scripting software.

    ReplyDelete
    Replies
    1. Hi,

      thanks so much for your comment.

      however there is one simple problem if using your method.

      what if i have 200 or even 500 or more contacts?

      will you suggest people to follow your method?

      that could be sorta annoying to make that file, and most importantly, what if i add more contact in the future? you will have to update that file as well.

      you know, people always would prefer some other easy way if available.

      anyway, this method is good, and i have already been using it. you may refer to my another entry regarding popup sms notfier via tasker scene to see how it is.

      Cahty

      Delete
    2. Can you please describe your method a bit more detailed? How to do var split and write to number2?

      Delete
  5. I tried the sqlite method and I got nothing so I ran it in a terminal and this is the error message I get: "no such function: _PHONE_NUMBER_STRIPPED_REVERSED."

    Any idea about this?

    P.S.
    I've on sqlite 3.7.16

    ReplyDelete
    Replies
    1. Hi,

      this error sounds weird to me, since i didn't experience before.

      but from the error description, i guess probably your ROM doesn't allow you extract data from the database?

      allright, firstly, you may confirm if the database path is exactly the same as mine, and the requested table also exists, open the table to check if the parameter you defined is there.

      after done these check, let me know the result, i will look into it if any luck.

      Cahty

      Delete
    2. Hi Mate,

      Your tutorial inspired me and I tried this myself, but I also get _PHONE_NUMBER_STRIPPED_REVERSD error. The path to sqlite and to contacts2.db is correct as I'm able to get data from other tables within contacts2.db (e.g. phone_lookup, view_groups) just fine. It seems like some part of tables work and some don't for unknown reason. At the same time I can use e.g. SQL Debbuger and it reads each table in that database without any issue.. I'm really counting on your help as I've been stuck at this for a month or so and it seems I can't resolve it myslelf.

      Regards,
      Robert

      Delete
    3. HI Robert, The same is happening to me. Did you find a way to solve this _PHONE_NUMBER_STRIPPED_REVERSD error? I´m running Androir 4.4kitkat. Thanks

      Delete
  6. Hello

    Like the first guy here say, when i try to put the sl4a script you gave i get: IndexError: list index out of range

    I didn't understand your solution for that problem
    What i do in tasker is new task:
    1. I set the %Contact_name to John
    2. I run sl4a script:
    import android
    droid = android.Android()
    contacts = droid.queryContent('content://com.android.contacts/data/phones',['data1'],'display_name = "%Contact_name"',None,None).result
    a = contacts[0]['data1']
    print a

    when i run this take it say IndexError: list index out of range.
    What should i do?
    Thanks !

    ReplyDelete
    Replies
    1. Hi,

      list index out of range, that means python script could not find the argument parameter you defined in the phone database.

      1, you need to confirm if your database directory path is exactly the same as mine? cause some other custom ROM, they revise it to other path.

      2, open the database table to see if the parameter you defined exists. as i said, in some custom ROM, it might be revised somehow in another different table.

      okay, let me know if any other issue.

      Cathy

      Delete
  7. How to get The mobile num er by sl4a Way?
    I want to get The first cellphone number. Tks

    ReplyDelete
  8. Hi,
    This looks like something that I am trying to do right now, this "may" help me out.
    What I am trying to do is run sl4a or something else to look for a contact group lets say called location. I want to extract all the mobile phone numbers from all the contacts in that contact group and pass it to tasker.

    I have not found a way to accomplish that yet.

    ReplyDelete
  9. Hi,

    I have a db of Names, Addresses and personal ID numbers. I would like to find a way using a script to search the internet with that information and locate contact phone numbers. Is this possible? I have no idea how to do something like this I'm just wondering if it is possible and how difficult it would be for someone to write the script. Any insight would be great.

    ReplyDelete
  10. Hey there,
    i dun have a fix for the discussed problem
    !BUT!
    This error "_PHONE_NUMBER_STRIPPED_REVERSD" only appears for me if i try to open/read the database on an local PC... If i open/read it on the device (via shell) directly this error is not shown, also i can see all "contact data".

    So u can open/read it via shell commands on the device but not on an local machine...

    Hope this helps someone...

    ReplyDelete
    Replies
    1. i am also trying to get contact data using python but is always give error sqlite3.opertaionalerror: unable to open database file.i am trying to access it through database program.

      Delete
  11. I wanted to know if we use a variable in place of %contact_name, why does it not work?

    ReplyDelete
  12. Solution for _PHONE_NUMBER_STRIPPED_REVERSD problem!!

    Because there aren't any VIEW in my contact2 db, sqlite3 throw this silly error mesage. (takes a day to realize this :) )

    But there is workaround: :)

    Run this shell command from an android terminal:
    NameSearchFor='Viktoria'; sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db
    "SELECT p.normalized_number FROM raw_contacts c, phone_lookup p WHERE p.raw_contact_id=c._id and c.display_name LIKE '%$NameSearchFor' ";

    I have Samsung Note 3 and Lolipop and there was no sqlite3 at all in Path

    Get latest SQLITE3 from XDA in my case armv7-pie.
    Copy to /system/xbin/sqlite3
    Change permissions to 755
    then run this shell command from terminal.
    Next step is runing it from Tasker.
    :)

    ReplyDelete
  13. Nice blog thanks for sharing this article. to get more details about email database visit us at consumer email list usa free download

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. It is full of informative stuff thanks for sharing. Keep sharing information like this. extract phone numbers from outlook

    ReplyDelete