How to use sqlite?
2016-01-13 19:53
344 查看
I tried to write something down, but I was not sure about something. So I searched it in the Internet, then I find that there are some good articles. Here is one: https://www.sqlite.org/cli.html
I will write down some useful commands.
1. How to open a sqlite database file?
$ sqlite3 NAME.db
2. How to list the help info?
sqlite> .helpAnd it will display like below:
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 <table> 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
sqlite>
3. How to quit (from sqlite command line mode)?
sqlite> .exit
4. How to show all tables in the database?
sqlite> .table
5. How to show all columns in a table?
sqlite> select * from tbl_TimeSetting;You'd better to execute the following sentence to change the mode to display the column names.
.mode lineThe default mode is list, it seems like this:
sqlite> .mode list
sqlite> select * from tbl_TimeSetting;
0|1|0|0|0|0|0|12|0|0|0|0|30|50|0|0|1|0|0|0|16200|0|1|1452664121|2858
and the line mode is:
sqlite> .mode line
sqlite> select * from tbl_TimeSetting;
_id = 0
bOnTimeFlag = 1
bOffTimeFlag = 0
enOffTimeState = 0
...
6. How to modify a value in the table?
update TABLE_NAME where CONDITION set COLUMN=VALUE;
For example:
update tbl_ThreeDVideoMode where InputSrcType=42 set eThreeDVideo3DTo2D=7;
I will write down some useful commands.
1. How to open a sqlite database file?
$ sqlite3 NAME.db
2. How to list the help info?
sqlite> .helpAnd it will display like below:
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 <table> 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
sqlite>
3. How to quit (from sqlite command line mode)?
sqlite> .exit
sqlite> .quitand [ctrl] + d are all OK.
4. How to show all tables in the database?
sqlite> .table
5. How to show all columns in a table?
sqlite> select * from tbl_TimeSetting;You'd better to execute the following sentence to change the mode to display the column names.
.mode lineThe default mode is list, it seems like this:
sqlite> .mode list
sqlite> select * from tbl_TimeSetting;
0|1|0|0|0|0|0|12|0|0|0|0|30|50|0|0|1|0|0|0|16200|0|1|1452664121|2858
and the line mode is:
sqlite> .mode line
sqlite> select * from tbl_TimeSetting;
_id = 0
bOnTimeFlag = 1
bOffTimeFlag = 0
enOffTimeState = 0
...
6. How to modify a value in the table?
update TABLE_NAME where CONDITION set COLUMN=VALUE;
For example:
update tbl_ThreeDVideoMode where InputSrcType=42 set eThreeDVideo3DTo2D=7;
相关文章推荐
- oracle----sql----查询---为例---计算
- oracle----笛卡尔集---多表查询---内链--外链---子查询
- hibernate核心API和级联的增删改查(CRUD)
- oracle--sql---约束--视图--索引
- oracle-----sql--建表
- Oracle中的PL/SQL的简单用法
- 常用sql总结
- Oracle中的自定义函数
- DbVisualizer报错You have an error in your SQL syntax; check the manual that corresponds to your MySQL
- symfony2中mysql和mongodb的增删改查总结
- MySQL函数
- 【翻译自mos文章】Oracle Cluster failed to start with ASM instance getting ORA-00443 ---改动/etc/resolv.conf所致
- Oracle中的触发器
- SQL Server 2008更新操作基本格式
- spring 整合 redis,以及spring的RedisTemplate如何使用
- Amoeba实现mysql读写分离
- 数据库事务及锁机制介绍
- Code First Migrations更新数据库结构的具体步骤
- MySQL limit分页查询优化写法
- unbutu+mysql的root密码重置方法