SQLite入门之二常用命令(四)

2014-11-24 10:16:05 · 作者: · 浏览: 3
2012-07-17 17:46:21 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table txt_data_table(id char(10),name char(10),age char(10),address varchar(15),hobby varchar (15)); sqlite> .separator "," sqlite> .import data.txt txt_data_table sqlite> SELECT * FROM txt_data_table; id,name,age,address,hobby 1,bxp,28,hz,ktv 2,zxh,30,hz,play 3,mx,25,kf,ktv 4,ml,26,jx,play 5,cc,25,qdh,ktv

2.2.5 列出所有数据表: .tables

help:

.tables  TABLE         List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.

example:

sqlite> .tables
Books         Customers     Names         Reservations
Cars          Friends       Orders

2.2.6 显示数据库或表结构:.schema

help:

.schema  TABLE         Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.

example:

1. 显示数据库结构

sqlite> .schema
CREATE TABLE Books(Id integer PRIMARY KEY, Title text, Author text, Isbn text default 'not available');
CREATE TABLE Cars(Id integer PRIMARY KEY, Name text, Cost integer);
CREATE TABLE Customers(CustomerId integer PRIMARY KEY, Name text);
CREATE TABLE Friends(Id integer PRIMARY KEY, Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN ('M', 'F')));
CREATE TABLE Names(Id integer, Name text);
CREATE TABLE Orders(Id integer PRIMARY KEY, OrderPrice integer CHECK(OrderPrice>0), Customer text);
CREATE TABLE Reservations(Id integer PRIMARY KEY, CustomerId integer, Day text);

2. 显示表结构

sqlite> .schema Books
CREATE TABLE Books(Id integer PRIMARY KEY, Title text, Author text, Isbn text default 'not available');

2.2.7 显示当前所有的数据库: .databases

help:

.databases             List names and files of attached databases

example:

bixiaopeng@bixiaopeng db$ sqlite3 wirelessqa.db ".databases"
seq  name             file
---  ---------------  ----------------------------------------------------------
0    main             /Users/bixiaopeng/workspace/source-github/wirelessqa/SQLIT

2.2.7 导出SQL脚本

将SQLite中指定的数据表以SQL创建的形式导出

help:

.output FILENAME       Send output to FILENAME
.dump  TABLE  ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.

example:

1. 导出所有的SQL脚本
sqlite> .output outputdata.sql
sqlite> .dump
sqlite>
2. 导出指定表的的SQL脚本
sqlite> .output carsdata.sql
sqlite> .dump Cars
sqlite>
3. 导出数据库
bixiaopeng@bixiaopeng db$ sqlite3 wirelessqa.db ".dump" | sqlite3 bxp.db
bixiaopeng@bixiaopeng db$ ls -al
total 168
drwxr-xr-x  7 bixiaopeng  staff    238  2 24 17:37 .
drwxr-xr-x  7 bixiaopeng  staff    238  2 24 17:37 ..
-rw-r--r--  1 bixiaopeng  staff  36864  2 24 17:37 bxp.db
-rw-r--r--  1 bixiaopeng  staff    484  2 24 17:31 carsdata.sql
-rw-r--r--  1 bixiaopeng  staff   2727  2 24 17:24 outputdata.sql
-rw-r--r--  1 bixiaopeng  staff   3174  2 24 17:15 testdata.sql
-rw-r--r--  1 bixiaopeng  staff  36864  2 24 17:15 wirelessqa.db
bixiaopeng@bixiaopeng db$ sqlite3 bxp.db "SELECT * FROM Cars;"
1|Audi|52642
2|Mercedes|57127
3|Skoda|9000
4|Volvo|29000
5|Bentley|350000
6|Citroen|21000
7|Hummer|41400
8|Volkswagen|21600
4. 导出其它格式
默认list,其它任选
                         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
导出html格式
bixiaopeng@bixiaopeng db$ sqlite3 -htm