标签:database sqlite3 shell table sql
平时用到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
我使用的是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 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 强退!
标签:database sqlite3 shell table sql
原文地址:http://blog.csdn.net/jscese/article/details/40016701