Search code examples
sqlitebatch-fileadb

sql select statement over adb


I would like to have a batch file that runs a select statement over adb and returns the found rows. This is the command that I use in my batch file:

adb shell "su -c sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db 'select * from sms;'"

The problem I have is, that my command produces an error:

Error: incomplete input

What I don’t understand is that the fallowing command works just fine:

adb shell "su -c sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db '.tables'"

This returns the tables from the database, as expected.

My guess is, that I need to escape some characters, the “;” maybe, but escaping it with \ or with ^ both did not work.

Any help would be greatly appreciated.


Solution

  • The correct answer would be to use `"`" to encode ".

    adb shell "su -c 'sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db `"`"SELECT * FROM sms;`"`"'"
    

    I only found out about this thanks to chatGPT.

    An alternative would be to use `\`". this makes a lot more sense. you escape the " with the \ on the adb side, and then you escape both of these characters with a backtick on the powershell side.

    adb shell "su -c 'sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db `\`"SELECT * FROM sms;`\`"'"
    

    If someone want’s to loose all sanity one could also add a where clause to the select statement:

    adb shell "su -c 'sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db `\`"SELECT body FROM sms WHERE address=`\`\`\`"+41798072042`\`\`\`";`\`"'"
    

    Works like a charm.