2011/06/09

android database management -1

參考資訊: Android sqlite3 簡介, sqlite3 datatype, sqlite 語法

Android 用 Sqlite3 的格式來儲存資料,甚至系統的 setting, preference 都是如此,在進一步研究 sqlite3 之前,先列一下所有目前模擬機上的 database file(android 3.1):
/data/data/com.android.providers.contacts/databases/contacts2.db
/data/data/com.android.email/databases/EmailProvider.db
/data/data/com.android.email/databases/EmailProviderBody.db
/data/data/com.android.browser/app_databases/Databases.db
/data/data/com.android.browser/app_databases/https_mail.google.com_0/0000000000000001.db
/data/data/com.android.browser/app_appcache/ApplicationCache.db
/data/data/com.android.browser/databases/autofill.db
/data/data/com.android.browser/databases/webview.db
/data/data/com.android.browser/databases/browser2.db
/data/data/com.android.browser/databases/webviewCache.db
/data/data/com.android.browser/app_geolocation/CachedGeoposition.db
/data/data/com.android.browser/app_icons/WebpageIcons.db
/data/data/com.android.providers.downloads/databases/downloads.db
/data/data/com.android.quicksearchbox/databases/qsb-log.db
/data/data/com.android.inputmethod.latin/databases/userbigram_dict.db
/data/data/com.android.inputmethod.latin/databases/auto_dict.db
/data/data/com.android.providers.media/databases/internal.db
/data/data/com.android.providers.media/databases/external-80b2b02.db
/data/data/com.android.providers.settings/databases/settings.db
/data/data/com.android.providers.userdictionary/databases/user_dict.db
/data/data/com.android.launcher/databases/launcher.db
/data/data/com.android.providers.telephony/databases/mmssms.db
/data/data/com.android.providers.telephony/databases/telephony.db
/data/data/com.google.android.gsf/databases/gservices.db
/data/data/com.google.android.gsf/databases/googlesettings.db
/data/data/com.google.android.gsf/databases/subscribedfeeds.db
/data/data/com.google.android.gsf/databases/talk.db
/data/data/com.android.deskclock/databases/alarms.db
/data/system/accounts.db

sqlite3 是一個 android device 上的內建命令,因此要使用的話就得進 device shell, 譬如 adb shell。執行 sqlite3 的畫面如下:
# which sqlite3
/system/xbin/sqlite3
# sqlite3                                                       
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

上面提供了幾點資訊:
1. sqlite3 放在 /system/xbin, 這是系統路徑
2. 版本是 3.7.4
3. sqlite3 的提示符號是 sqlite>
4. 每個命令要用分號 ; 來結束
5. 要取得說明請下 .help 命令

有些隱含的訊息像,命令都是以 . 開頭,這一點下過 .help 命令後就知道了,另外要離開 sqlite3 的話,可以下 .quit 命令,或是 ctrl-D

這篇先列所有命令先,也就是 .help 畫面:

sqlite> .help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.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
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.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.
                         With no args, it turns EXPLAIN on.
.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
                         If TABLE specified, only show indices for tables
                         matching LIKE pattern TABLE.
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.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
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off

0 意見: