# read_sms_db.py
import sqlite3
import sys
from pathlib import Path
def main(db_path):
db_path = Path(db_path)
if not db_path.exists():
print(f"[ERROR] File not found: {db_path}")
return
try:
conn = sqlite3.connect(db_path)
except sqlite3.Error as e:
print(f"[ERROR] Could not connect to database: {e}")
return
cursor = conn.cursor()
query = """
SELECT
datetime(date + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') AS message_date,
is_from_me,
text
FROM message
ORDER BY date ASC
LIMIT 100;
"""
try:
cursor.execute(query)
except sqlite3.Error as e:
print(f"[ERROR] Query failed: {e}")
conn.close()
return
rows = cursor.fetchall()
conn.close()
if not rows:
print("[INFO] No messages found.")
return
for msg_date, is_from_me, text in rows:
direction = "OUTGOING" if is_from_me == 1 else "INCOMING"
text = text if text is not None else ""
print(f"[{msg_date}] {direction}: {text}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python read_sms_db.py path/to/sms.db")
else:
main(sys.argv[1])