平時用到database的地方很少,這裏記錄一下shell終端下直接對db的基本操做!
html
撰寫不易,轉載請註明出處:http://blog.csdn.net/jscese/article/details/40016701
java
sqlite3 爲android所使用的輕量級數據庫,小巧方便,用於管理android系統中的各類db文件,在ubuntu中可以安裝sqliteman 來查看android系統中的db文件,Framework中的接口位置:/frameworks/base/core/java/android/database/sqlite/SQLiteDatabase.javaandroid
我使用的是ubuntu的minicom下的shell終端。以系統setting的database爲例,文件夾爲:/data/data/com.android.providers.settings/databases/setting.dbsql
cd 到databases文件夾下。打開數據庫文件:sqlite3 setting.dbshell
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 打開數據庫,假設存在就打開操做,假設不存在就建立,改動以後能夠保存建立。ubuntu
使用.help 查看幫助:ide
.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?ui
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?this
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 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 sqlite> 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|15 2|volume_ring|5 3|volume_system|7 4|volume_voice|4 5|volume_alarm|6 6|volume_notification|5 7|volume_bluetooth_sco|7 ...
使用 .mode culumn 以後:
sqlite> .mode column sqlite> 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
sqlite> .schema system CREATE 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數據,怎樣輸入就怎樣存儲,不改變格式.
sqlite> .mode insert sqlite> 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');
insert into system values('45','sqlite','jscese');
這裏插入數據要跟建立的時候數量要相應,否則會報:
Error: table table_name has * columns but * values were supplied
sqlite> select * from system where name='sqlite'; INSERT INTO table VALUES(45,'sqlite','jscese');
delete from system where value='jscese';
45|sqlite|jscese sqlite> update system set name='sqlite3' where value='jscese'; sqlite> select * from system where value='jscese'; 45|sqlite3|jscese
在使用SQL指令以後沒有加 分號就 enter,會進入輸入模式,這個時候再補上 一個 。 分號就能夠:
sqlite> select * from system ...> ;
我直接在minicom下使用 sqlite 沒法識別 上下左右方向鍵,以及 回退鍵!
出現 ^[[A ^H 這種亂碼。
網上別人給出的。我沒試過~http://ljhzzyx.blog.163.com/blog/static/3838031220102595026789/
本機終端adb shell 是可以識別 回退鍵的
通常退出sqlite3 使用 .quit
實在退不出 ...> 模式 就使用 ctrl+D 強退!