sqlite的入门基础

本文为在windows xp下的演示。
http://www.sqlite3.org.cn/down/sqlite-3_6_6_2.zip 下载目前最新的SQLite 3.6.6.2的版本。

(最新版本是3.6.14了)


为了方便,我把它解压了在J:/sqlite-3_6_6_2,就一个SQLite3.exe。
开始-->运行-->输入Cmd 进入命令行,输入cd J:/sqlite-3_6_6_2进入J:/sqlite-3_6_6_2目录下。
如下:
C:/Documents and Settings/x.tsai>cd J:/sqlite-3_6_6_2
1)创建数据库文件:
J:/sqlite-3_6_6_2>SQLite3 test.db
SQLite version 3.6.6.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> ;
sqlite>
就生成了一个test.db在J:/sqlite-3_6_6_2目录下。
这样同时也在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,
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,'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
将在J:/sqlite-3_6_6_2/ 目录下生成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,
name varchar(10) UNIQUE
);
INSERT INTO "mylog" VALUES(1,1,'xtsai');
CREATE INDEX mylog_idx on mylog (id asc);
COMMIT;
sqlite>
6)退出
.exit 或者.quit

by SQLite3 中国组织的 小菜(x.tsai) 20081215转载请保留此信息,谢谢! http://sqlite3.org.cn

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能