SQLite官网下载页面下载SQLite,其中windows需要下拉到Precompiled Binaries for Windows中需要下载dll与tools两个zip:
统一解压在一个文件夹中(例如:D:\tools\cpp_lib\SQLite)
在系统设置的path中添加这一路径,之后可在cmd命令提示符输入sqlite3验证是否可用
在某一路径下输入:
创建数据文件:Sqlite3 文件名.后缀
例:sqlite3 data.db
显示所有表名:.table
创建表:create table 表名(键名1,键名2,…);
例:create table Person(pid, name, score);
如果表不存在则创建:create table if not exists 表名(键名1,键名2,…);
例:create table if not exists Person(pid, name, score);
create table if not exists Kungfu(kid integer primary key autoincrement, n
ame text unique, pid); //integer:整型,primary key:主键,autoincrement:自增(必须是整型),text:字符串,uinque:唯一
删除表:drop table 表名;
例:drop table Person;
增加一条数据:insert into 表名 values(值1,值2,…);
例:insert into Person values(1,‘令狐冲’,98);
Insert into Person(pid,name,score) values(2,’岳不群’,94);
Insert into Person(pid) values(3);
删除一条数据:delete from 表名 where 键名=值;
例:delete from Person where pid=6;
修改一条数据:update 表名 set 键名=值 where 键名=值;
例:update Person set name=”任我行” where pid=4;
查询:select * from 表名;
例:select * from Person;
select pid,name,score from Person;
select name from Person;
select * from Person limit 3; //前三条
select * from Person where score > 93;
select * from Person where score > 93 and score < 98;
select * from Person order by score; //排序
select * from Person order by score desc; //由大到小
select * from Person order by score desc limit 3;
select count(*) from Person; //数量
select sum(score) from Person; //字段总数
select avg(score) from Person; //字段平均
多表查询:select 表名.键名,表名.键名,表名.键名 from 表名,表名 where 表名.键名=表名.键名
例:select Person.name,Kungfu.name,Person.score from Person,Kungfu where Perso
n.pid=Kungfu.pid;