Chinaunix首页 | 论坛 | 博客
  • 博客访问: 290286
  • 博文数量: 61
  • 博客积分: 1581
  • 博客等级: 上尉
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-24 13:52
个人简介

幸运儿不是我,因为我选择的路很难走.如果够出色也不能出头,至少要做到没第二个我.

文章分类

全部博文(61)

文章存档

2013年(14)

2012年(47)

分类: LINUX

2012-11-22 10:35:22

  花了两天时间学习sqlite3数据库,觉得sqlite3很简单使用,下面做一个简单的小结:
  常用命令:
  最直接的查看命令方法是,进入sqlite3,用.help查看帮助文档.

SQLite version 3.7.14.1 2012-10-04 19:37:12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
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 //dump ?TABLE? 生成形成数据库表的SQL脚本
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off//显示开关,设置为on后,命令回显
.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 //.headers 设置为 on 时,查询结果显示时带有字段名

.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.
.load FILE ?ENTRY?     Load an extension library
.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 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 //用指定的串代替输出的NULL串
.output FILENAME       Send output to FILENAME //将输出导入到指定的文件中
.output stdout         Send output to the screen //将输出打印到屏幕
.prompt MAIN CONTINUE  Replace the standard prompts //改变 CLP 的 shell 提示符

.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 //设置"list"模式下的分隔符
.show                  Show the current values for various settings //打印所有SQLite环境变量的设置
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables //列出?PATTERN?匹配的表名
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.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
.timer ON|OFF          Turn the CPU timer measurement on or off
 
获得数据库的 Schema 信息

.tables [pattern]
得到所有表和视图的列表,其中[pattern]可以是任何类 SQL 的操作符。执行上述命令会返回符合条
件的所有表和视图,如果没有 pattern 项,返回所有表和视图。

.indices [table name]
显示一个表的索引

.schema [table name]
返回一个表或视图的定义(DDL)语句。如果没提供表名,则返回所有数据库对象(包括 table、indexe、view 和 index)的定义语句

数据导出
.dump
将数据库导出为 SQL 格式的文件,不使用任何参数,.dump 将导出整个数据库。如果提供参数,CLP 把参数理解为表名或视图名
例:
sqlite> .output file.sql
sqlite> .dump
sqlite> .output stdout

数据导入

1 .read
导入由.dump 命令创建的文件(如果要使用前面作为备份文件所导出的
file.sql,需要先移除已经存在的数据库对象(test 表和 schema 视图),然后用下面方法导入)
例:
sqlite> drop table test;
sqlite> drop view schema;
sqlite> .read file.sql

2 sqlite3 test.db < test.sql
  此处假设 test.db 不存在。如果它存在,则或许会因为数据库中有同名的对象而出错。
可以用复制的方法得到一个二进制的数据库文件拷贝

  如果文件是由逗号或其它定界符分隔的值(comma-separated values, CSV)组成,可使用.import [file][table]命令。此命令将解析指定的文件并尝试将数据插入到指定的表中。

格式化

.mode
命令可以设置结果数据的几种输出格式。可选的格式为 csv、column、html、insert、line、list、tabs 和 tcl。默认值是 list,在此模式下显示结果时列间以默认的分隔符分隔。如果你想以 CSV 格式输出一个表的数据,可如下操作:
sqlite3> .output file.csv
sqlite3> .separator ,
sqlite3> select * from test;
sqlite3> .output stdout

数据库备份

1 .dump
可得到 SQL 格式的文件
例:
sqlite3 test.db .dump > test.sql

2 cp test.db test.backup
   一般情况下,二进制的备份如不 SQL 备份兼容性好。尽管 SQLite 有很好的向上兼容性和各
操作系统间文件格式的一致性,但如果想要将备份文件保留很长时间,还是 SQL 格式保险
一些。












阅读(6329) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~