`
fonter
  • 浏览: 870939 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

(转)Android SQLite Shell

阅读更多

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.

 

分享到:
评论

相关推荐

    Android SQLite学习工具

    这篇博客“Android SQLite学习工具”可能详细介绍了如何在Android环境中使用SQLite进行数据操作,并提供了一个实用的SQLite管理工具——sqlite3.exe。 SQLite数据库在Android中的应用主要包括创建数据库、创建表、...

    sqlite3以及所依赖的文件 Android

    5. **使用ADB shell**:通过ADB连接到设备,然后在shell环境下运行sqlite3命令,这样可以避免因环境配置不全导致的问题。 6. **第三方工具**:有些第三方工具,如SQLiteManager,提供了图形化的界面来管理SQLite...

    相当简单Android SQLite数据库增删改查【自己写的】

    相当简单Android SQLite数据库增删改查,绝对适合新手,包您一看就会, 1.这是我照着网上的资料和帮助手册写的; 2.数据库的增删改查都涉及到了; 3.导入项目,运行,然后 在终端进入数据库,命令为 adb shell cd ...

    Android-shell创建数据库

    本文将详细介绍如何使用Android shell(通常简称adb shell)命令来创建一个SQLite数据库,并进行基本的操作。 #### 二、环境准备与配置 在开始之前,确保已经安装了Android SDK Platform Tools,并将其添加到系统...

    android adb shell 命令大全

    ### Android ADB Shell 命令详解 #### 1. 显示系统中全部 Android 平台 - **命令**: `android list targets` - **描述**: 此命令用于列出所有可用的 Android 平台版本,这对于选择合适的开发环境非常重要。 - **...

    Android平台sqlite3数据库

    此外,Android SDK提供了一个叫做`adb shell sqlite3`的命令,可以直接在adb命令行中与设备上的SQLite数据库交互,但该版本可能不适用于所有Android版本,因此,从特定版本的AVD中提取并使用的SQLite3可执行文件可能...

    Android中使用SQLite示例

    Android提供了`adb shell`命令,可以导出和导入SQLite数据库,方便在开发过程中备份和恢复数据。 9. **LiveData与Room库** Google推出的Room库是对SQLite的现代封装,提供了更高级别的抽象,如`LiveData`,使得...

    Android例子源码解决多线程读写sqlite数据库锁定问题

    SQLite实质上是将数据写入一个文件,通常情况下,在应用的包名下面都能找到xxx.db的文件,拥有root权限的手机,可以通过adb shell,看到data/data/packagename/databases/xxx.db这样的文件。我们可以得知SQLite是...

    sqlite3和libncurses.so

    标题中的“sqlite3和libncurses.so”涉及到的是在Android环境中使用SQLite数据库管理工具和一个必要的库文件。SQLite是一个轻量级的关系型数据库管理系统,广泛应用于移动设备,如Android手机和平板,因为它不需要...

    Android 真机安装 sqlite3 & libncurses.so

    在Android平台上,SQLite3是一个非常重要的数据库管理系统,用于存储应用程序中的结构化数据。SQLite3作为一个轻量级的数据库,不需要独立的服务器进程,且能够直接嵌入到Android应用中,使得开发者可以方便地进行...

    android-SQLite3命令详解教程

    在Android系统上使用SQLite3时,需通过`adb shell`命令进入设备的shell环境,再调用SQLite3命令。若要操作特定的应用数据库,如`settings.db`,可以直接指定数据库文件的完整路径。此外,通过组合使用`su`命令获取...

    sqlite3等文件

    在Android系统中,SQLite是一个轻量级的数据库引擎,它被广泛用于移动应用来存储数据。当我们在Android设备上进行应用程序开发或者进行调试时,有时需要直接与SQLite数据库交互,例如查询、修改或分析数据。在这样的...

    android数据操作,sqlite

    在Android系统中,SQLite是一个内置的关系型数据库管理系统,用于存储应用程序的数据。SQLite支持SQL标准,具有轻量级、高效、可靠的特点,使得它成为移动应用数据管理的理想选择。本主题将深入探讨如何在Android中...

    SQLite(创建数据库)

    10. **与其他应用集成**:许多应用程序,如Android、iOS和桌面应用,都内置了SQLite支持,使得在这些平台上集成SQLite变得非常容易。 以上就是SQLite创建数据库的基本流程和主要操作,通过熟悉这些知识,开发者可以...

    sqlite最新版3.7.9-android手机开发数据库

    - 管理SQLite数据库:通过`.open &lt;database&gt;`命令打开数据库,`.databases`查看已连接的数据库,`.tables`查看数据库中的表,`.schema &lt;table&gt;`查看表结构,`.quit`退出SQLite shell。 在提供的压缩包子文件的文件...

    android中shell命令浅析

    通过组合使用`adb shell`和`sqlite3`命令可以访问Android设备上的SQLite数据库。例如: ```bash adb shell sqlite3 ``` ##### 13. 进入Shell命令行 使用`adb shell`命令可以直接进入Android设备的Shell命令行界面,...

    sqlite3 android6.0 armV7

    SQLite3 是一个轻量级的关系型数据库管理系统,广泛应用于移动设备,尤其是Android系统。在Android 6.0(Marshmallow)版本中,SQLite3被用作内置的数据库引擎,用于应用程序存储结构化数据。这个“sqlite3 android...

    AndroidSQLite详解

    在 Windows 环境下,通常下载 `sqlite-shell-win32-x86` 和 `sqlite-analyzer-win32-x86` 的 zip 包。前者是 SQLite 数据库引擎,后者是 SQLite 数据库分析器,主要用于分析数据库的状态等信息。 1. **创建数据库**...

    android设备4.4和5.0系统安装 sqlite3方法

    在Android系统中,SQLite是一个内置的轻量级数据库引擎,用于存储应用程序的数据。SQLite3是其更新版本,提供了更高效和稳定的数据管理功能。对于Android 4.4(KitKat)和5.0(Lollipop)这两个较早的系统版本,安装...

Global site tag (gtag.js) - Google Analytics