记忆力减退之sqlite操作

下载与安装

SQLite Download Page

 

直接解压

 

设置环境变量

 

 

 

创建数据库

 

 

 简单基础命令

创建数据库
	sqlite3 test.db
退出数据库
	.quit或者.exit

查看表sql结构
	.schema
查看表
	.tables

查看数据库
	.database

创建表
	create table human(id integer,name text,age int,sex text);

	create table test(studentID int, name text, class text, chinese float, math float, english float, physics float, sports float);
重命表名
	alter table human rename to humans;

增加表字段
	alter table humans add id integer;
	alter table humans add weight float;  
	alter table humans add love text;    

重命名表字段	
	 

删除表
	drop table human;
删除表字段
	sqlite不支持,
	先新建一个临时表,将原表中需要的字段存入新表
	create table temp as select name,age,sex from humans where 1=2;	
	再删除原表
	drop table humans;
	重命名新建的表
	alter table temp rename to humans;

/
插入数据
	insert into humans (name,sex) values('xxc','female');
	insert into humans (name,age,sex) values('ccm',30,'female');
	
    insert into student(studentID, name , class, chinese, math, english, physics, sports) values(960001,'xxc2','1-2',95.6,91,81.3,91.7,92);

/
查询所有字段
	select * from humans;
查询指定字段
	select name from humans;

查询指定的数据
	select * from humans where name = 'xxc';
	select * from humans where name = 'xxc' and sex='female';
	select * from humans where name = 'xxc' or name='ccm';

修改数据
	update humans set age = 50,sex='female' where name='xxc';
	update humans set id = 1 where name='xxc';
	update humans set id = 2 where name='ccm';
	update humans set love = 'Basketball' where name='ccm';

删除数据
	delete from humans where name='xxc' or name='ccm';

升序排序
	select * from humans order by id asc;
降序排序
	select * from humans order by id desc;
组合排序
	select * from humans order by id desc, name asc;


sqlite函数与聚合
	length()	返回字符串长度	
	lower()	将字符串转换为小写	
	upper()	将字符串转换为大写	

	select length(sex) as 性别字符长度 from humans;
	
	avg()	返回平均值
	count()	返回某列的行数
	max()	返回某列最大值
	min()	返回某列最小值
	sum()	返回某列值之和

	select avg(age) as 平均年龄 from humans;
	select count(name) as 当前人数 from humans;

数据分组group by
	select sex count(name) as 人数 from human group by sex; 

过滤分组having 
	select age from humans group by age having age > 20;

*********as 是将前面的输出作为什么的意思*********


主键约束 primary key
	唯一地标识一行,一张表中只能有一个主键
	主键对用户没有意义,常用于索引
	永远不要更新主键,否则违反对用户没有意义原则
	主键不应包含动态变化的数据,如时间戳
	主键应当由计算机自动生成,保证唯一性
	create table human(id int primary key,name text,age int);
		
唯一约束 unique
	用来保证一列或者一组列中数据唯一性,类似于主键,但与主键有区别
	表可包含多个唯一约束,但只允许一个主键
	唯一约束列可以修改或更新
	创建表时通过 unique设置
	create table human(id int primary key,studentID int unique,name text);	

检查约束
	用来保证一列或者一组列中数据满足一组指定的条件
	指定范围,检查最大或最小范围,通过 check设置
	create table human(id int primary key,name text, age int check(age) > 0;)

事务
	默认情况下,每条SQL语句自成事务
	begin: 开始一个事务,之后的所有操作都可以取消
	commit:使begin后的所有操作得到确认
	rollback:取消begin后的所有操作

分表
	将学生信息与成绩分开存储
		节省空间,处理简单,效率高效,在处理大量数据时尤为明显
	使用关系型数据库存储数据
		各表的设计非常重要,良好的表设计能够简化数据的处理,提高效率,提高数据库的健壮性
	在联结两个表时,实际上是将第一个表中的每一行与第二个表中的每一行配对
	从表persons和表grade获取信息
	select name,addr,score,year from persons, grade where persons.id = grade.id;

视图
	create view humanview as select name,addr,year from persons;
	创建视图后humanview 可以作为一张表来用
	select name form humanview;


QSqlDatabase()
	addDatabase()
	setDatabaseName()
	open()


QSqlQuery
	exec()
	next()
	value()
	prepare()
	bindValue()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值