Android——sqlite3 基本命令操作,androidsqlite3

來源:互聯網
上載者:User

Android——sqlite3 基本命令操作,androidsqlite3

             平時用到database的地方不多,這裡記錄一下shell終端下直接對db的基本操作!



                                                 撰寫不易,轉載請註明出處:http://blog.csdn.net/jscese/article/details/40016701


一.概念:

            sqlite3 為android所使用的輕量級資料庫,小巧方便,用於管理android系統中的各種db檔案,在ubuntu中可以安裝sqliteman 來查看android系統中的db檔案,Framework中的介面位置:/frameworks/base/core/java/android/database/sqlite/SQLiteDatabase.java




二.shell使用:

      我使用的是ubuntu的minicom下的shell終端,以系統setting的database為例,目錄為:/data/data/com.android.providers.settings/databases/setting.db

cd 到databases目錄下,開啟資料庫檔案:sqlite3 setting.db

SQLite version 3.7.11 2012-03-20 11:35:50                                                                                                Enter ".help" for instructions                                                                                                           Enter SQL statements terminated with a ";"                                                                                               sqlite> 

可以看到SQL版本,以及簡單提示,使用SQL語句時需要以 “分號結尾!

sqlite3  *.db 開啟資料庫,如果存在就開啟操作,如果不存在就建立,修改之後能夠儲存建立。


使用.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..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.vfsname ?AUX?         Print the name of the VFS stack.width NUM1 NUM2 ...   Set column widths for "column" mode.timer ON|OFF          Turn the CPU timer measurement on or offsqlite> .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 databasessqlite> sqlite> 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..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.vfsname ?AUX?         Print the name of the VFS stack.width NUM1 NUM2 ...   Set column widths for "column" mode.timer ON|OFF          Turn the CPU timer measurement on or off


可以看到所支援的命令,其中常用的:

.database  顯示資料庫資訊;包含當前資料庫的位置
.tables  或者 .table 顯示表名稱 沒有表則不顯示
.schema  命令可以查看建立資料對象時的SQL命令;

.mode csv|column|insert|line|list|tabs|tcl   改變輸出格式

預設情況,使用 select * from system 查看system表的全部資料:

sqlite> select * from system;1|volume_music|152|volume_ring|53|volume_system|74|volume_voice|45|volume_alarm|66|volume_notification|57|volume_bluetooth_sco|7... 

可以看到是列表的形式顯示出來的,而且ID name value 都是以 “ | ” 分隔開來,分割符號由 .separator "X" 來定義,X即為分割符!

使用 .mode culumn 之後:

sqlite> .mode columnsqlite> select * from system;1           volume_music  15        2           volume_ring   5         3           volume_syste  7         4           volume_voice  4         5           volume_alarm  6         6           volume_notif  5         7           volume_bluet  7  

其它類似,都只是為了 改變輸出的格式而已。


1.建立指令:

sqlite> .schema systemCREATE TABLE system (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON CONFLICT REPLACE,value TEXT);CREATE INDEX systemIndex1 ON system (name);

建立的時候指定參數和屬性,這裡有三個,並且自增,關於資料類型:

NULL: 這個值為空白值
INTEGER: 值被標識為整數,依據值的大小可以依次被儲存為1,2,3,4,5,6,7,8個位元組
REAL: 所有值都是浮動的數值,被儲存為8位元組的IEEE浮動標記序號.
TEXT: 文本. 值為文本字串,使用資料庫編碼儲存(TUTF-8, UTF-16BE or UTF-16-LE).
BLOB: 值是BLOB資料,如何輸入就如何儲存,不改變格式.

2.插入資料:

sqlite> .mode insertsqlite> select * from system;INSERT INTO table VALUES(1,'volume_music','15');INSERT INTO table VALUES(2,'volume_ring','5');INSERT INTO table VALUES(3,'volume_system','7');INSERT INTO table VALUES(4,'volume_voice','4');INSERT INTO table VALUES(5,'volume_alarm','6');INSERT INTO table VALUES(6,'volume_notification','5');INSERT INTO table VALUES(7,'volume_bluetooth_sco','7');

如我要在system表裡面插入一條資料:

insert into system values('45','sqlite','jscese');

這裡插入資料要跟建立的時候數量要對應,不然會報:

Error: table table_name has * columns but * values were supplied


3.查詢指定資料:

sqlite> select * from system where name='sqlite';INSERT INTO table VALUES(45,'sqlite','jscese');

根據表類型值來篩選查詢,這裡表的屬性有 _id ,name,value ,可在建立命令中看到!


4.刪除資料:

delete from system where value='jscese';

刪除整個表: drop table table_name


5.更新表資料:

45|sqlite|jscesesqlite> update system set name='sqlite3' where value='jscese';sqlite> select * from system where value='jscese';45|sqlite3|jscese


6.操作問題:

在使用SQL指令之後沒有加 分號就 enter,會進入輸入模式,這個時候再補上 一個 ; 分號即可:

sqlite> select * from system   ...> ;

我直接在minicom下使用 sqlite 無法識別 上下左右方向鍵,以及 回退鍵!

出現 ^[[A  ^H 這樣的亂碼,

網上別人給出的,我沒試過~http://ljhzzyx.blog.163.com/blog/static/3838031220102595026789/

本機終端adb shell 是可以識別 回退鍵的

一般退出sqlite3 使用 .quit

實在退不出   ...> 模式 就使用   ctrl+D  強退!






android 基本檔案操作命令

ADB (Android Debug Bridge)
說明:下面一些命令需要有root許可權才能執行成功
快速啟動dos視窗執行adb:
1. adb.exe所在路徑添加到系統內容變數中
2. 配置快速鍵啟動dos
進入C:\WINDOWS\system32目錄下,找到cmd.exe.
右擊菜單 "發送到" -> 案頭捷徑。
在案頭上右擊"捷徑 到 cmd.exe" -> "屬性" -> "捷徑"頁
-> 游標高亮"快速鍵" -> 按下自訂快速鍵 (如:Ctrl + Alt + Z)

任何情況下,按下Ctrl + Alt + Z啟動dos視窗就可以執行adb命令了

-----------查看裝置串連狀態 系列-----------
adb get-serialno 擷取裝置的ID和序號serialNumber
adb devices 查詢當前電腦上串連那些裝置(包括模擬器和手機),輸出格式: [serialNumber] [state]
adb get-state 查看模擬器/設施的目前狀態.

說明:
序號[serialNumber]——由adb建立的一個字串,這個字串通過自己的控制連接埠<type>-<consolePort>
唯一地識別一個模擬器/裝置執行個體。一個序號的例子: emulator-5554

-----------發送命令到裝置 系列-----------
adb [-d|-e|-s <serialNumber>] <command>
-d 發送命令給usb串連的裝置
-e 發送命令到模擬器裝置
-s <serialNumber> 發送命令到指定裝置

如啟動手機裝置shell: adb -d shell

adb forward <local> <remote>發布連接埠,可以設定任意的連接埠號碼,
做為主機向模擬器或裝置的請求連接埠。如:adb forward tcp:5555 tcp:8000

adb reboot 重啟手機
adb remount 將system分區重新掛載為可讀寫分區
adb kill-server 終止adb服務進程
adb start-server 重啟adb服務進程
adb root 已root許可權重啟adb服務
adb wait-for-device 在模擬器/裝置串連之前把命令轉載在adb的命令器中
adb jdwp 查看指定的設施的可用的JDWP資訊.
可以用 forward jdwp:<pid> 連接埠映射資訊來串連指定的JDWP進程.例如:
adb forward tcp:8000 jdwp:472
jdb -attach localhost:8000

adb shell am 命令可以啟動應用程式

adb shell input text <string> 向裝置輸入文本(游標所在的文字框)
adb shell input keyevent <event_code> 向裝置發送按鍵事件
如:
在編輯簡訊時,往文字框輸入文本:adb shell input text "hello"
向手機發送索引值回Home:adb sh......餘下全文>>
 
android sqlite3 怎通過dos 命令進入控制台進行查詢

sqlite3 xxxxx.db
.dump
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.