Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Android Contacts – [MUST HAVE ROOT]

Posted on January 10, 2014October 26, 2015 By David Kittell

If you have rooted your phone, there are various ways to run these queries but I choose two ways that work for me.

These code snippets will assist you in cleaning up your Android contacts.

DISCLAIMER: Backup your contacts before you run any of these queries as I take no responsibility for anything that happens to your contacts or phone.

If you do not already know how the Android contact database works it’d be a good idea to download SQLite Editor free or paid version and simply view the database before you run any of these queries as the tables are not as easy as doing “SELECT Last_Name, First_Name, Organization FROM contacts”

If you are in SQLite Editor select “Contacts Storage” from the Apps list and then choose “contacts2.db”, from there you’ll see a few tables.

Only Using Phone/Tablet

  1. Download Google Keep and SQLite Database Editor

Using Phone/Tablet Along With A Computer

  1. Download SQLite Database Editor, AndroPHP, and phpliteadmin

If you are in SQLite Database Editor, on most Android phones the contacts database is located at “/data/data/com.android.providers.contacts/databases/contacts.db” or around there.

SELECT data1
FROM data
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%note%"
	AND length(data1) > 0;
SELECT data1
FROM data
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%postal%"
	AND length(data1) > 0;
SELECT REPLACE(replace(REPLACE(data1, x '0D0A', x '0A'), x '0D', x '0A'), x '0A', ' ')
FROM data
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%postal%"
	AND length(data1) > 0;

This query below will display all company/organization names in your contacts. Respectfully if you want only the distinct (no duplicates) you can remove the — in front of DISTINCT

SELECT
--DISTINCT
data1
FROM data
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%organization%"
	AND length(data1) > 0;
SELECT data1 AS Company
	,(
		SELECT data1
		FROM data dt2
		INNER JOIN mimetypes ON mimetype_id = mimetypes._id
		WHERE mimetype LIKE "%name%"
			AND length(data1) > 0
			AND dt1.raw_contact_id = dt2.raw_contact_id
		) AS NAME
FROM data dt1
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%organization%"
	AND length(data1) > 0
ORDER BY Company
SELECT data1 AS NAME
	,(
		SELECT data1
		FROM data dt2
		INNER JOIN mimetypes ON mimetype_id = mimetypes._id
		WHERE mimetype LIKE "%organization%"
			AND length(data1) > 0
			AND dt1.raw_contact_id = dt2.raw_contact_id
		) AS Company
FROM data dt1
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%name%"
	AND length(data1) > 0
ORDER BY NAME

If you run into a situation like what I often do this query below will help as it looks at the name of the contact and the company name and lists where they appear to match

SELECT data1 AS NAME
	,data2 AS FirstName
	,data3 AS Lastname
	,(
		SELECT data1
		FROM data dt2
		INNER JOIN mimetypes ON mimetype_id = mimetypes._id
		WHERE mimetype LIKE "%organization%"
			AND length(data1) > 0
			AND dt1.raw_contact_id = dt2.raw_contact_id
		) AS Company
FROM data dt1
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%name%"
	AND length(data1) > 0
	AND NAME LIKE Company
ORDER BY NAME

If you want to correct the problem in your contacts from the query above you can run this query

UPDATE data
SET data1 = NULL, data2 = NULL, data3 = NULL
WHERE mimetype_id IN (
		SELECT _id
		FROM mimetypes
		WHERE mimetype LIKE "%name%"
		)
AND raw_contact_id IN

(
SELECT
raw_contact_id

FROM data dt1
INNER JOIN mimetypes ON mimetype_id = mimetypes._id
WHERE mimetype LIKE "%name%"
    AND length(data1) > 0
    AND data1 LIKE (
        SELECT data1
        FROM data dt2
        INNER JOIN mimetypes ON mimetype_id = mimetypes._id
        WHERE mimetype LIKE "%organization%"
            AND length(data1) > 0
            AND dt1.raw_contact_id = dt2.raw_contact_id
        ));
Originally Posted on January 10, 2014
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code SQL SQLite

Post navigation

Previous post
Next post

Related Posts

PowerShell Download File and Extract File

Posted on September 17, 2015

Grabbing code from Powershell Download Script and Powershell Extract Zip Files this script will download with the option of extracting the zip file. In PowerShell if you type the following you will get two examples of how to use the code. get-help DownloadFileExtractFile.ps1 -examples <# .SYNOPSIS Downloads one file from…

Read More

PowerShell – Windows 7 – Windows Service Cleanup

Posted on November 9, 2015

There are many services on Windows 7 that do not need to run, while some suggest disabling the service I tend to go on the side of caution and simply stop and set them to manual startup. # Stop Services and set to Manual # Home Group Sharing – start…

Read More

Ektron Find Replace In UrlAliasManual

Posted on October 23, 2013October 26, 2015

This script will rename manual aliases set xact_abort on declare @currval nvarchar(500) declare @newval nvarchar(500) declare @search nvarchar(500) declare @replace varchar(500) set @search = ‘/Index’ –string to find set @replace = ‘/Default’ –replacement string declare @pos int declare @id bigint begin tran declare curs cursor local fast_forward for select AliasId,AliasName…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes