Sie sind auf Seite 1von 7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

cmanios
Developer life is a NullPointerException

Read SMS directly from Sqlite database in Android


Posted on October 29, 2013

Today I wanted to extract the SMS content out of the android SMS application SQLite database using ADB.
NOTE: The whole procedure was tested on a rooted phone running Cyanogenmod Android. It may not work
on unrooted phones
So I achieved that , using the following steps:
1. Find SMS database file location
2. Pull SQLite database file with ADB
3. Identify sms tables
4. Identify the thread _ID of the SMS you want to read
5. Find all sms messages for a specific thread id
In detail:
1. Find SMS database file location
After some searching I found that the location of sms database file is in

/data/data/com.android.providers.telephony/databases/mmssms.db

2. Pull SQLite database file with ADB


This step is optional. Open a shell and type:

adb pull /data/data/com.android.providers.telephony/databases/mmssms.db mmssms.db

Now the whole sms database is copied locally in your working directory!
3. Identify sms tables
Open the SQLite using command line or your favourite SQLite editor. The tables in which the android
telephony application stores SMS messages are threads and sms.
Table Threads stores a record as a header for every SMS thread which is started:
https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

1/7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

CREATE TABLE threads (


_id INTEGER PRIMARY KEY AUTOINCREMENT,
date INTEGER DEFAULT 0,message_count INTEGER DEFAULT 0,
recipient_ids TEXT,snippet TEXT,
snippet_cs INTEGER DEFAULT 0,
read INTEGER DEFAULT 1,
type INTEGER DEFAULT 0,
error INTEGER DEFAULT 0,
has_attachment INTEGER DEFAULT 0
)

Table SMS stores all outgoing and incoming messages for every thread:

CREATE TABLE sms (_id INTEGER PRIMARY KEY,


thread_id INTEGER,
Follow
address TEXT,
person INTEGER,
Follow cmanios
date INTEGER,
new post delivered
date_sent INTEGER DEFAULGet
T 0every
,
to
your
Inbox.
protocol INTEGER,
read INTEGER DEFAULT 0,
Enter y our email address
status INTEGER DEFAULT -1,
type INTEGER,
Sign me up
reply_path_present INTEGER,
subject TEXT,
Build a website with WordPress.com
body TEXT,
service_center TEXT,
locked INTEGER DEFAULT 0,
error_code INTEGER DEFAULT 0,
seen INTEGER DEFAULT 0
)

4. Identify the thread _ID of the SMS you want to read


Run a SQL query and read snippet column. This column stores the last sms in your thread and is the
visible text of every SMS list item when you open the SMS application.

SELECT _id, snippet


FROM threads;

5. Find all sms messages for a specific thread id


In my case the thread _id had the value 310. Knowing this id we run another query on sms table in order
to get SMS messages ordered chronologically by date:

SELECT datetime(date/1000, 'unixepoch','localtime') ,datetime(date_sent/1000, 'unixepoch','localtime') ,p


FROM sms
WHERE thread_id = 310
ORDER BY date

https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

2/7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

The results are going to be similar to the following:

+---------------------------------------------------------------------------------------------------------| date
| date_sent
| person
| body
|---------------------------------------------------------------------------------------------------------| 2013-10-20 13:48:18 | 2013-10-20 13:48:16 | 54
| Hello Christos! How are you?
| 2013-10-20 16:34:03 | 1970-01-01 02:00:00 |
| Fine, thanks ! I configure the left MFD of a F| 2013-10-20 16:40:02 | 2013-10-20 16:40:01 | 54
| Awesome! I am throwing a party tomorrow at 21:4
| 2013-10-20 17:15:15 | 1970-01-01 02:00:00 |
| Thanks! I will be there!
+----------------------------------------------------------------------------------------------------------

Note that person with id 54 is my friend Kitsos and person = NULL is me!
Good luck tampering and hacking with your Android device !
About these ads

Share t his:

Twitter

Facebook

Like
Be the first to like this.

Relat ed

Android pre-populated Database


In "Android"

Import a large sql dump file to a MySQL


database from command line
In "Databases"

Extend SQLiteOpenHelper as a singleton


class in Android
In "Android"

About cmanios
programming
View all posts by cmanios

This entry was posted in Android, Databases, Linux, SQLite and tagged adb, android, cyanogenmod, database, file, linux, location, root, shell, sms, sqlite, telephony, thread.
Bookmark the permalink.

https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

3/7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

16 Responses to Read SMS directly from Sqlite database in Android


Tomaz says:
January 12, 2014 at 13:49

hello i need to urgent get back some sms i don know this sql. can u help me i can send u file or something..
Reply

Tomaz says:
January 12, 2014 at 14:02

i can pay also


Reply

cmanios says:
January 12, 2014 at 19:42

@Tomaz You can backup your sms database easily with an application like
https://play.google.com/store/apps/details?id=com.riteshsahu.SMSBackupRestore. However if you want you
may send me your database.
Reply

Tob says:
January 20, 2014 at 11:15

Ive rooted my Galaxy S3 but I cant find data/data/com.android.providers.telephony/databases/mmssms.db anywhere.


The phone is attached to my computer and Im looking for the path above in Explorer. I can only find
com.android.providers.media.
Reply

cmanios says:
January 20, 2014 at 11:47

Tob I assume that you have attached your phone to a Windows operating system. I am not sure if accessing your
files via explorer.exe is done using root user with admin privileges. I suggest that you use ADB or a root file
explorer.
Reply

Tob says:
January 21, 2014 at 11:19

Thank you! I found it with root explorer. The problem now is that the database only have a few records
visible in the sms table. I know that at least 100 sms has been deleted. Is there anyway to get them back or
they just dont exist anymore if not visible in the sms table?

cmanios says:
February 15, 2014 at 12:52

I am afraid that you need digital forensics software to view deleted files in your phone storage.
https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

4/7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

Unfortunately I am not aware of such software and techniques.

kundan says:
May 21, 2015 at 08:41

hello i have made an application that send sms to customer, now the problem is that i am using samsung tablet model
number GT-N8000 when i send sms from application it store the sms in inbox of tablet where we can see the sms
now i did not want to store or want to see that sms in inbox. Can you just help me out how to hide sms from inbox.
and i am using kitkat 4.4.2 version.
Reply

Demetris Hadjigeorgiou says:


September 12, 2014 at 17:08

Hello! Ive been trying to write an app on Android that saves incoming SMS (sender, content and time) in an SQLite database
and replies based on the content and sender of the message with predefined answers. Unfortunately, I havent been able to
make it work as it gets stuck on accessing the database. I have written up my question in more detail with a copy of my code
on stack overflow and I was wondering if you could help me out. Heres the link:
http://stackoverflow.com/questions/25604039/how-to-update-sqlite-database-when-receiving-sms
Thanks!
Reply

cmanios says:
September 17, 2014 at 23:43

Demetris, I am afraid that your question is irrelevant to this blog post. When you want to access Contacts
programmatically in your application you should use Contacts Provider. Also the error you have in yout
StackOverflow post cannot help as there is nothing but a custom log message. You should post the actual
Exceptions or stacktrace in order to be comprehensible.
Reply

Tharun says:
October 27, 2014 at 07:15

I am creating a Kiosk based application on my rooted android tablet.One functionality is to reboot the device remotely by
sending an SMS,which is working fine.However,I observe that the phone reboots exactly after 30 min intervals and
thereafter for a couple of times.(This reboot functionality when triggered using GCM/phone call works fine and doesnt reboot
later,as expected).So i suspect that when I reboot using SMS,the SMS module is firing some notifications after 30 min interval
which is causing the erratic reboots.Could it be due to the messages not being marked as read? Anyhow I think I can solve
this if I can delete messages from the core android message storage.How can I achieve this ? Iv searched online but no good
results.Kindly assist .
Reply

cmanios says:
October 28, 2014 at 14:31

1. I am quite puzzled.. Do you want to reboot your tablet every 30 minutes after the first reboot which was fired by
the SMS BroadcastReceiver?
https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

5/7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

2. Are you sure that the reboots are caused by your application and not from the tablet itself?
3. Can you provide more details about your implementation or code extracts ?
Note that my example describes raw access to SMS Sqlite database via shell (cli) and not from an Android
ApplicationContext.
Reply

George says:
January 19, 2015 at 21:50

Hi, I looked in the location you said but my root data directory contains nothing. I have texts visible in the app so there should
be something but data/data doesnt even exist. Is there any other location they could be found at to your knowledge?
Im on a Nexus 4, Cyanogenmod.
Its driving me crazy, nobody online seems to give an answer that is true for me! Thanks.
Reply

cmanios says:
January 21, 2015 at 22:45

It seems to me that you have a permission issue. You possibly do not have root access. Try to access / directory
with a superuser application or via terminal running

su

command before

ls -l /data/data

.
Reply

kundan says:
May 21, 2015 at 08:46

hello i have made an application that send sms to customer, now the problem is that i am using samsung tablet model number
GT-N8000 when i send sms from application it store the sms in inbox of tablet where we can see the sms now i did not want
to store or want to see that sms in inbox. Can you just help me out how to hide sms from inbox. and i am using kitkat 4.4.2
version.
Reply

cmanios says:
June 3, 2015 at 08:11

There are 2 ways:


https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

6/7

14/07/2015

Read SMS directly from Sqlite database in Android | cmanios

1. Follow my steps in a rooted phone and delete or move messages from sms database.
2. Use SMS Content Provider like in this example and delete or move messages.
I do not know if there is a way to hide them. You may want to move them to another database.
Reply

cmanios
The Twenty Ten Theme.

Blog at WordPress.com.

https://cmanios.wordpress.com/2013/10/29/read-sms-directly-from-sqlite-database-in-android/

7/7

Das könnte Ihnen auch gefallen