您的位置:首页 > 运维架构 > Linux

【转载】sqlite的入门基础(Linux下)(SQLite3 中国组织原创)

2010-12-01 16:44 916 查看
【转载】sqlite的入门基础(Linux下)(SQLite3 中国组织原创)
2009-01-09 16:32
本文为在ubuntu 8.04 Linux 发行版下的演示。

从http://www.sqlite3.org.cn/down/sqlite3-3.6.7.bin.gz 下载目前最新的SQLite
3.6.7的版本。

为了方便,我把它解压了在/home/xtsai/sqlite3,就一个sqlite3-3.6.7.bin。

sqlite3文件夹上按右键点击“在终端中打开”进入/home/xtsai/sqlite3目录下。

如下:

tsai@xtsai:~/sqlite3$

1)创建数据库文件:

xtsai@xtsai:~/sqlite3$ sqlite3 test.db

SQLite version 3.4.2

Enter ".help" for instructions

sqlite> ;

sqlite>

( 注意:命令要用小写字母,否则会出现像

xtsai@xtsai:~/sqlite3$ SQLite3 test.db

bash: SQLite3:找不到命令 )

就生成了一个test.db在/home/xtsai/sqlite3目录下。

这样同时也在SQLite3挂上了这个test.db。

创建一个数据库mylog

打入:

create table mylog(

id integer primary key,

cid integer,

name varchar(10) UNIQUE

);

如下:

sqlite> create table mylog(

...> id integer primary key,

...> cid integer,

...> name varchar(10) UNIQUE

...> );

sqlite>

# 如果表存在就会提示:

SQL error: table mylog already exists

我们创建索引信息

打入:

create index mylog_idx on mylog (id asc);

如下:

sqlite> create index mylog_idx on mylog (id asc);

sqlite>

我们查看表的信息,看有多少个表

.table

如下:

sqlite> .table

mylog

查看表的结构:

.schema mylog

如下

sqlite> .schema mylog

CREATE TABLE mylog(

id integer primary key,

cid integer,

name varchar(10) UNIQUE

);

CREATE INDEX mylog_idx on mylog (id asc);

给数据表插入一条记录

insert into mylog (cid,name) values ('001','xtsai');

如下:

sqlite> insert into mylog (cid,name) values ('001','xtsai');

sqlite>

检索有多少条记录

select count(*) from mylog;

如下:

sqlite> select count(*) from mylog;

1

检索搜索记录

select * from mylog;

如下:

sqlite> select * from mylog;

1|1|xtsai

2)

用.help可以看看有什么命令。

.help 回车即可。

如下:

sqlite> .help

.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

.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.

.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 on TABLE

.load FILE ?ENTRY? Load an extension library

.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

.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

.schema ?TABLE? Show the CREATE statements

.separator STRING Change separator used by output mode and .import

.show Show the current values for various settings

.tables ?PATTERN? List names of tables matching a LIKE pattern

.timeout MS Try opening locked tables for MS milliseconds

.width NUM NUM ... Set column widths for "column" mode

sqlite>

3)看看目前挂的数据库。

.database

如下:

sqlite> .database

seq name file

--- ---------------
---------------------------------------------------------

0 main J:/sqlite-3_6_6_2/test.db

1 temp

sqlite>

4)如果要把查询输出到文件。

格式:

.output 文件名

查询语句;

例如:

sqlite> .out query.txt

将在/home/xtsai/sqlite3 目录下生成query.txt

sqlite> select * from mylog;

把查询的结果保存在query.txt中。

如果要把查询结果用屏幕输出,则重新打入

.output stdout

例如:

sqlite> .output stdout

sqlite> select * from mylog;

1|1|xtsai

5)把表结构输出,同时索引也会输出

格式:

.dump 表名

例如:

sqlite> .dump mylog

BEGIN TRANSACTION;

CREATE TABLE mylog(

id integer primary key,

cid integer,

name varchar(10) UNIQUE

);

INSERT INTO "mylog" VALUES(1,1,'xtsai');

CREATE INDEX mylog_idx on mylog (id asc);

COMMIT;

sqlite>

6)退出

.exit 或者.quit

windows下sqlite的入门基础方法类似请查看
http://www.sqlite3.org.cn/asp2html/newsfile/2008-12-15/20081215132926.html
by SQLite3 中国组织的 小菜(x.tsai) 20090108转载请保留此信息,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: