设为首页 收藏本站
查看: 720|回复: 0

[经验分享] (转)Android SQLite Shell

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-29 09:34:55 | 显示全部楼层 |阅读模式
  SQLite is available on the Android device itself. The executable is in the /system/bin directory of the device. You can see that this directory contains the shell commands like ls, ps, etc., as well as sqlite3, dalvikvm, and dexdump utilities.
  Code Listing 1. Contents of system/bin
  
# pwd
pwd
/system/bin
  # ls -l
ls -l
-rwxr-xr-x root root 196 2008-02-29 01:09 am
-rwxr-xr-x root root 2342 2008-02-29 01:09 dumpstate
-rwxr-xr-x root root 208 2008-02-29 01:09 input
-rwxr-xr-x root root 212 2008-02-29 01:09 monkey
-rwxr-xr-x root root 196 2008-02-29 01:09 pm
  …
  lrwxr-xr-x root root 2008-02-29 01:16 mount -> toolbox
lrwxr-xr-x root root 2008-02-29 01:16 notify -> toolbox
-rwxr-xr-x root root 28032 2008-02-29 01:16 dexdump
-rwxr-xr-x root root 7100 2008-02-29 01:16 dumpsys
-rwxr-xr-x root root 7904 2008-02-29 01:16 mem_profiler
-rwxr-xr-x root root 6480 2008-02-29 01:16 service
-rwxr-xr-x root root 4320 2008-02-29 01:16 rild
-rwxr-xr-x root root 4928 2008-02-29 01:16 sdutil
-rwxr-xr-x root root 26488 2008-02-29 01:16 sqlite3
-rwxr-xr-x root root 4148 2008-02-29 01:16 dexopt
-rwxr-xr-x root root 4908 2008-02-29 01:16 dalvikvm
-rwsr-sr-x root root 3200 2008-02-29 01:16 surfaceflinger
-rwxr-xr-x root root 5420 2008-02-29 01:16 app_process
-rwxr-xr-x root root 39408 2008-02-29 01:16 runtime
-rwxr-xr-x root root 2920 2008-02-29 01:16 system_server
-rwxr-xr-x root root 9168 2008-02-29 01:17 pv
  Where are the SQLite databases on an Android Device?
  
By default, the SQLite databases have an extension .db. For example, a NotePad application may use note_pad.db database. This might be created from code using Android SDK’s SQLite classes/methods. By default, the SQLite database for an application is in the /data/data/<app>/databases directory. As you can see from the listing below, the notepad database is in the /data/data/com.google.android.notepad/databases directory.
  Code Listing 2. The location of the notepad database
  
# pwd
pwd
/data/data/com.google.android.notepad/databases
  # ls -l
ls -l
-rw-rw---- app_8 app_8 3072 2008-03-18 18:23 note_pad.db
  You can see a whole bunch of these sub-directories under the /data/data directory.
  Code Listing 3. The /data/data directory
  
# pwd
pwd
/data/data
  # ls
ls
com.google.android.notepad
com.google.android.samples
com.google.android.gtalkservice
com.google.android.phone
com.google.android.maps
com.google.android.providers.im
com.google.android.home
com.google.android.googleapps
com.google.android.gtalksettings
com.google.android.fallback
com.google.android.development
com.google.android.contacts
com.google.android.browser
com.google.android.providers.telephony
com.google.android.providers.settings
com.google.android.providers.media
com.google.android.masfproxyservice
com.google.android.providers.googleapps
com.google.android.providers.contacts
android
  Starting the SQLite command line program
  
Starting generically
  
Typing sqlite3 in the Android Shell will open the SQLite command line program (since sqlite3 is in the /system/bin directory). Typing .help gives the summarized set of commands available from here.
  Code Listing 4. The SQLite commands available from the command line utility
  
# sqlite3
sqlite3
SQLite version 3.5.0
Enter ".help" for instructions
  sqlite> .help
.help
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices TABLE Show names of all indices on TABLE
.load FILE ?ENTRY? Load an extension library
.mode MODE ?TABLE? Set output mode where MODE is one of:
                         csv Comma-separated values
                         column Left-aligned columns. (See .width)
                         html HTML <table> code
                         insert SQL insert statements for TABLE
                         line One value per line
                         list Values delimited by .separator string
                         tabs Tab-separated values
                         tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.schema ?TABLE? Show the CREATE statements
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.tables ?PATTERN? List names of tables matching a LIKE pattern
.timeout MS Try opening locked tables for MS milliseconds
.width NUM NUM ... Set column widths for "column" mode
sqlite>
  Opening a database
  
You would, of course, want to see the data and structure of a particular database. In the listing below, the note_pad.db is opened for further querying. Here, I went to the directory where the database is; however, you can also specify the full path.
  Code Listing 5. Opening a specific database from SQLite command line
  
# pwd
pwd
/data/data/com.google.android.notepad/databases
  # ls
ls
note_pad.db
  # sqlite3 note_pad.db
sqlite3 note_pad.db
SQLite version 3.5.0
Enter ".help" for instructions
sqlite>
  Example Commands from the SQLite command line program
  
Getting the list of tables
  
You can get the list of all the tables in the current database in two ways: from the .tables command or by querying the sqlite_master table.
  Code Listing 6. List of tables
  
sqlite> .tables
.tables
android_metadata notes
  sqlite> select * from sqlite_master;
select * from sqlite_master;
table|android_metadata|android_metadata|2|CREATE TABLE android_metadata (locale
TEXT)
table|notes|notes|3|CREATE TABLE notes (_id INTEGER PRIMARY KEY,title TEXT,note
TEXT,created INTEGER,modified INTEGER)
  Getting the schema of the tables
  
By using the .schema command, you can get the schema of either all of the tables or a single table.
  Code Listing 7. Schema of tables
  
sqlite> .schema
.schema
CREATE TABLE android_metadata (locale TEXT);
CREATE TABLE notes (_id INTEGER PRIMARY KEY,title TEXT,note TEXT,created INTEGER
,modified INTEGER);
  sqlite> .schema notes
.schema notes
CREATE TABLE notes (_id INTEGER PRIMARY KEY,title TEXT,note TEXT,created INTEGER
,modified INTEGER);
  SQL Queries
  
You can issue queries like ‘select * from notes’ to get the data from the notes table. As you can see from the figure below, I have entered a series of notes in the notepad application.
  Figure 1. Notes in the Note pad application
  
After entering the notes, if you query the notes table, you will get the data in the _id, title, note, created, and modified columns.
  Code Listing 8. Data from notes
  
sqlite> .schema notes
.schema notes
CREATE TABLE notes (_id INTEGER PRIMARY KEY,title TEXT,note TEXT,created INTEGER
,modified INTEGER);
  sqlite> select * from notes;
select * from notes;
1|Title of Note
This is a test note.|This is a test note.|1205871448827|1205873944838
3|Another note
Some text.|A third note
Some text.|1205873990394|1205874145996
4|One more
Body here|One more
Body here|1205874074992|1205874088072
  And, finally, we can take a look at how this table was created.
  Code Listing 9. Creation of the notes table
  
public class NotePadProvider extends ContentProvider {
  …
  private static class DatabaseHelper extends SQLiteOpenHelper {
  @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE notes (_id INTEGER PRIMARY KEY,"
                    + "title TEXT," + "note TEXT," + "created INTEGER,"
                    + "modified INTEGER" + ");");
        }

  }
  …
  @Override
    public boolean onCreate() {
        DatabaseHelper dbHelper = new DatabaseHelper();
        mDB = dbHelper.openDatabase(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
        return (mDB == null) ? false : true;
    }
  …
  }
  As you can see above, CREATE TABLE sql has been used to create the notes table. Classes like SQLiteOpenHelper and ContentProvider from Android SDK are put to use here.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307029-1-1.html 上篇帖子: SQLite的基本优化方法 下篇帖子: SQLite外键(Foreign Key)支持
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表