sqlite version 3.3.6
Enter ".help" for instructions
sqlite> .help // 命令不需要用分号结束.
.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
.mode MODE ?table? Set output mode where MODE is one of:
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 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> .databases;
unkNown command or invalID arguments: "databases;". Enter ".help" for help
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /root/test.db
sqlite> drop table test; // sql 语句用分号结束
sqlite> create table test ( ID varchar(20) PRIMARY KEY,name nvarchar(20) UNIQUE);
sqlite> insert into test values ( '1234','zhankunlin');
sqlite> insert into test values ( '1234','zhankunlin');
sql error: column name is not unique // 违背了主键约束
sqlite> insert into test values ( '123','zhankunlin');
sql error: column name is not unique
sqlite> insert into test values ( '123','zhaoyanhong');
sqlite> select * from test;
1234|zhankunlin
123|zhaoyanhong
sqlite> .schema test // 查看表的定义
CREATE table test ( ID varchar(20) PRIMARY KEY,name nvarchar(20) UNIQUE);
sqlite> .tables
test
sqlite> .databases //查看当前数据库
seq name file
--- --------------- ----------------------------------------------------------
0 main /root/test.db
1 temp /var/tmp/sqlite_QrNXVqClzKC6LWO
更多使用,参考:http://www.cnblogs.com/hnrainll/archive/2011/04/22/2024627.html
SQlite命令行工具的使用
最近使用sqlite较多,这东西现在是越用越着迷,不管是工作或者个人自娱自乐写东西,只要有数据存储首先就想到了sqlite,我一直对C/S模式的数据存储很反感,记得在知道sqlite之前的很长一段时间里一直用的是xml来存数我的数据,我喜欢将数据存储在单一文件里,我可以随时把数据带在身上,而且它们兼容性够好,拿着数据文件到那里都能用,哪怕是在嵌入式系统里照样玩儿的转…
sqlite特性
@H_301_87@ ACID事务 零配置 – 无需安装和管理配置 储存在单一磁盘文件中的一个完整的数据库 数据库文件可以在不同字节顺序的机器间自由的共享 支持数据库大小至2TB 足够小,大致3万行C代码,250K 比一些流行的数据库在大部分普通数据库 *** 作要快 简单,轻松的API 包含TCL绑定,同时通过Wrapper支持其他语言的绑定 良好注释的源代码,并且有着90%以上的测试覆盖率 独立: 没有额外依赖 Source完全的Open,你可以用于任何用途,包括出售它 支持多种开发语言,C,PHP,Perl,Java,ASP.NET,Python @H_419_114@ sqlite安装
现在各大linux发型版均有现成的软件包可供安装,而且大部份系统都是自带有的,想确认系统里有没有运行下
$sqlite3
非linux系统到:http://www.sqlite.org/download.html下载安装
接下来就是sqlite命令行工具的使用
打开或创建数据库
$sqlite3 test.db3
这样就能打开或者创建一个新的数据库文件
$sqlite3 test.db3
sqlite version 3.6.23
Enter ".help" for instructions
Enter sql statements terminated with a ";"
sqlite>
它看起来就是这个样子
sqlite内置命令
它除了能执行SQL语句以外还提供一组内置的命令,它们是以点.开始,比如说查看帮助信息就是.help退出是.exit跟.quit
创建表
在命令行里大部份的SQL语句它都是支持的,现在来新建两个表
sqlite>create table Artists (
--->ArtistID INTEGER PRIMARY KEY,
--->Artistname TEXT);
sqlite 对SQL语句大小写不敏感,所以大写小写随便
sqlite>create table CDs (
--->CDID INTEGER PRIMARY KEY,
--->ArtistID INTEGER NOT NulL,
--->Title TEXT NOT NulL,
--->Date TEXT);
这里注意,除了INTEGER PRIMARY KEY其它字段是都可以是无类型的,也就是不关声明什么或者不声明,这个字段是可以存储任何数据的。
插入数据
sqlite>insert into Artists (ArtistID,Artistname) values (NulL,'Peter GabrIEl');
sqlite>insert into Artists (ArtistID,'Bruce Hornsby');
sqlite>insert into Artists (ArtistID,'Lyle lovett');
sqlite>insert into Artists (ArtistID,'Beach Boys');
sqlite>insert into CDs (CDID,ArtistID,Title,Date) values (NulL,1,'So','1984');
sqlite>insert into CDs (CDID,'Us','1992');
sqlite>insert into CDs (CDID,2,'The Way It Is','1986');
sqlite>insert into CDs (CDID,'Scenes from the SouthsIDe','1990');
sqlite>insert into CDs (CDID,'Security',3,'Joshua Judges Ruth',4,'Pet Sounds','1966');
sqlite是支持导入sql文件的,只要使用内置命令.read即可,比如说我们将以上的命令建成一个sql文件,命名为insert_table.sql
insert into Artists (ArtistID,’Peter GabrIEl’);
insert into Artists (ArtistID,’Bruce Hornsby’);
insert into Artists (ArtistID,’Lyle lovett’);
insert into Artists (ArtistID,’Beach Boys’);
insert into CDs (CDID,’So’,’1984′);
insert into CDs (CDID,’Us’,’1992′);
insert into CDs (CDID,’The Way It Is’,’1986′);
insert into CDs (CDID,’Scenes from the SouthsIDe’,’1990′);
insert into CDs (CDID,’Security’,’Joshua Judges Ruth’,’Pet Sounds’,’1966′);
接着在命令行里运行
sqlite>.read insert_table.sql
表查询
来看看现在这两张表里都有那些内容,执行
sqlite>select * from Artists;
和
sqlite>select * from CDs;
如果要同时看表头,请在运行查询语句前打开headers选项
sqlite>.headers ON
输出结果看起来应该是这样子
ArtisID|Artistname
1 |Peter GabrIEl
2 |Bruce Hornsby
3 |Lyle lovett
4 |Beach Boys
和
CDID|ArtisID|Title |Date
1 |1 |So |1984
2 |1 |Us |1992
3 |2 |The Way It Is |1986
4 |2 |Scenes from the SouthsIDe|1990
5 |1 |Security |1990
6 |3 |Joshua Judges Ruth |1992
7 |4 |Pet Sounds |1966
其它的一些查询语句
sqlite>SELECT Title AS Albumname FROM CDs;
sqlite>SELECT Title FROM CDs WHERE Date>=1990 ORDER BY Title;
sqlite>SELECT Date FROM CDs;
sqlite>SELECT disTINCT Date FROM CDs;
sqlite>SELECT Title FROM CDs GROUP BY ArtistID;
多表查询
执行
sqlite>SELECT t1.Artistname,CDs.Title FROM Artists t1,CDs WHERE t1.ArtistID=CDs.ArtistID
得到的结果
Artistname |Title
Peter GabrIEl|So
Peter GabrIEl|Us
Peter GabrIEl|Security
Bruce Hornsby|The Way It Is
Bruce Hornsby|Scenes from the SouthsIDe
Lyle lovett |Joshua Judge Ruth
Beach Boys |Pet Sounds
更新字段
插入一条数据
sqlite>UPDATE Artists SET Artistname ='Santana' WHERE ArtistID=5;
即可
删除字段
首先执行
sqlite>select * FROM CDs WHERE Title liKE 'Super%';
看看是不是想要删除的数据,是的话执行
sqlite>DELETE FROM CDs WHERE Title liKE 'Super%';
再运行
sqlite>select * FROM CDs WHERE Title liKE 'Super%';
看看是不是已经删除了?
如果嫌上面的命令行不够直观高效,而你又非常喜欢多用鼠标,那么推荐你安装sqlite Manager这个firefox扩展程序,它真的非常方便。
总结以上是内存溢出为你收集整理的SQLITE简单使用全部内容,希望文章能够帮你解决SQLITE简单使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)