Android开发之SQLite基本语句

之前接触过一点MySQL数据库方面的知识,现在在学习Android软件开发过程中发现SQLite语句与MySQL的语句还是有部分不同,SQLite中的命令不算太多,可以在adb shell中进入App的data文件,之后执行sqite3 数据库名,进入到sql命令行界面

Android开发之SQLite基本语句_第1张图片

查询SQLite的基本命令,输入".help"得到命令与句列表:

sqlite> .help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail on|off           Stop after hitting an error.  Default OFF
.binary on|off         Turn binary output on or off.  Default OFF
.clone NEWDB           Clone data into NEWDB from the existing database
.databases             List names and files of attached databases
.dbinfo ?DB?           Show status information about the database
.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
.eqp on|off            Enable or disable automatic EXPLAIN QUERY PLAN
.exit                  Exit this program
.explain ?on|off?      Turn output mode suitable for EXPLAIN on or off.
                         With no args, it turns EXPLAIN on.
.fullschema            Show schema and the content of sqlite_stat tables
.headers on|off        Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indexes ?TABLE?       Show names of all indexes
                         If TABLE specified, only show indexes for tables
                         matching LIKE pattern TABLE.
.limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         ascii    Columns/rows delimited by 0x1F and 0x1E
                         csv      Comma-separated values
                         column   Left-aligned columns.  (See .width)
                         html     HTML  code
                         insert   SQL insert statements for TABLE
                         line     One value per line
                         list     Values delimited by .separator strings
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Use STRING in place of NULL values
.once FILENAME         Output for the next SQL command only to FILENAME
.open ?FILENAME?       Close existing database and reopen FILENAME
.output ?FILENAME?     Send output to FILENAME or stdout
.print STRING...       Print literal STRING
.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
.save FILE             Write in-memory database into FILE
.scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator COL ?ROW?   Change the column separator and optionally the row
                         separator for both the output mode and .import
.shell CMD ARGS...     Run CMD ARGS... in a system shell
.show                  Show the current values for various settings
.stats on|off          Turn stats on or off
.system CMD ARGS...    Run CMD ARGS... in a system shell
.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
.timer on|off          Turn SQL timer on or off
.trace FILE|off        Output each SQL statement as it is run
.vfsname ?AUX?         Print the name of the VFS stack
.width NUM1 NUM2 ...   Set column widths for "column" mode
                         Negative values right-justify

譬如语句“.table”查看当前数据库下面的表单

语句“select * from MPD;”则与MySQL中检索所有列的所有内容语句相同,查看某列则是“select id from MPD;”将通配符修改为列名

Android开发之SQLite基本语句_第2张图片

查看某些行,则加入limit参数,如“select * from MPD limit 3;”显示第三行以后的所有列数据,“select id from MPD limit 3,3;”显示第三行以后的三行(4-6行)id列数据

Android开发之SQLite基本语句_第3张图片

查看当前SQLite的基本配置,输入命令".show"

Android开发之SQLite基本语句_第4张图片

之前打印出来的表单没有显示列名,因此执行语句“.headers on”,再打印

Android开发之SQLite基本语句_第5张图片

在MySQL中可以利用“\G”代替“;”,从而使表单以列形式显示,但是SQLite中不能使用“\G”,修改mode即可

Android开发之SQLite基本语句_第6张图片

和MySQL相同地,可以使用where子句检索数据

Android开发之SQLite基本语句_第7张图片

使用聚集函数AVG()求均值

Android开发之SQLite基本语句_第8张图片

你可能感兴趣的:(Andriod开发)