SQlite数据库操作

SQLite是一个进程内的库,实现了SQL数据库引擎,其重要的特性是零配置的,这意味着不需要复杂的安装或管理

数据库及SQL作用:网站经由 web 来更新大量的信息,需要数据库来存储信息

可用于网站主机的数据库系统有很多种类型,最常见的是MySQL、SQL Server和Oracle

SQL 是一门用于访问数据库的语言,如果要在数据库存储或检索数据,web 服务器就需要使用 SQL 语言对数据库系统进行访问的权限

检验与安装: Linux 操作系统一般都附带 SQLite,终端键入 sqlite3 即可检验是否安装SQLite

若未安装则键入

 sudo apt-get install sqlite3  //安装SQlite

SQLite数据存储类型:interger整型 real浮点型 text文本字符串 blob二进制数据(比如文件) 

先来个小例子:我在点击按钮时ajax发送请求,然后views里执行清空购物车操作

if request.method == 'POST':
      CartModel.objects.all().delete()//注意:CartModel.objects.all()表示CartModel所有数据

SQL语句类型:(1)数据定义DDL(2)数据操作DML(3)数据查询DQL

(1)数据定义语句:

①创建表create table 表名(字段名 字段类型) 例:

create table t_student(id interger,name text,age integer,score real)

 ②删除表drop table 表名 例:

drop table t_student

(2)数据操作语句:

①插入数据insert into 表名(字段)value (字段值) 例:

insert into t_student(name,age) values('haizeiwang',10) 

 (注意:数据库中的字符串内容都用单引号' '引住)

②更新数据 update 表名 set 字段 = 字段值  例:

update t_student set name = 'json',age = 20;

(条件更新①:update t_student set age=5 where age>10 and name!='haizei';

      将表中年纪大于10且名字为haizei的数据中年纪改为5

     ②:updete t_student set score=age where name='haizei';

                      将表中名字为haizei的记录,score字段值都改为age字段的值) 

③删除数据 delete from 表名 例:

delete from t_student;

 (注意:会将t_student表中所有记录删除)

(条件删除:delete from t_student where age<=10 or age>30

                        将年纪大于30或小于10的数据删除)

(3)数据查询:关键字 select,where,having,groupby

①select 字段 from 表名;select*from表名      //查询所有字段 例:

elect name,age from t_student;

 条件查询:(年龄大于10)

select*from t_student where age>10 

 ②分页查询 select*from 表名 limit 数值1,数值2 例:

select*from t_student limit 4,8 //跳过最前面4句,而后取8条记录

 (每页固定显示5条数据)

limit 0,5  //第一页

limit 5,5  //第二页

limit 5*(n-1),5  //第三页

条件语句:配合使用后,可以将删除,更新等具体到某一条或多条

(等于):where 字段=某值(注意:1个=) 等价 where 字段 is 某值

(不等于):where 字段!=某值 等价 where 字段 is not 某值

(大于):where 字段 > 某值

(多重条件):where 字段1 = 某值 and 字段2 > 某值

where 字段1 = 某值 or 字段2 = 某值 

猜你喜欢

转载自570109268.iteye.com/blog/2368816