我打算学习SQLite了 荣

接触SQLite已经很久了,没有怎么用过。最近在做一个东西,用到SQLite,打算自现在开始,把自己当成一个新手来接触SQLite(也的确是新手)。
先找个地方下个管理数据库的工具吧。
当前遇到的第一个问题:[六子文章]
1:如何用SQL语句,修改数据库表中列的数据类型?
2:如何应用参数?
3:删除数据库表中的记录。
Delete FROM Recorder
4:删除数据库表
DROP TABLE  Recorder
5:取得前9条记录
SELECT  *  FROM Recorder limit 9
6:建立测试数据库:
1)RecType
花销类型表:
CREATE TABLE RecType (RecTypeId integer NOT NULL, RecTypeName [varchar](20) NOT NULL, PRIMARY KEY(RecTypeId))
表描述:
字段名称 数据类型 数据长度 字段描述 默认值 标识 可为空
==============================
RecTypeId, int, 4, 花销类型编号, , 是, 不可
----------------------------------------
RecTypeName, varchar, 20, 花销类型名称, , 不是, 不可
----------------------------------------
==============================
主键为
RecTypeId
2)Recorder
CREATE TABLE [Recorder] (
 [RedId] integer NOT NULL ,
 [CreateDate] [datetime] NOT NULL default CURRENT_TIMESTAMP,
 [RecDate] [date] NOT NULL ,
 [RecType] [int] NOT NULL ,
 [RecValue] [decimal](18, 2) NOT NULL ,
 [Remark] [varchar] (500) NULL ,
PRIMARY KEY(RedId)
)
表描述:
字段名称 数据类型 数据长度 字段描述 默认值 标识 可为空
[六子文档:url:admin11.cnblogs.com]
==============================
RedId, int, 4, 记录ID, , 是, 不可
----------------------------------------
CreateDate, datetime, 8, 记录时间, (getdate()), 不是, 不可
----------------------------------------
RecDate, smalldatetime, 4, 花销日期, , 不是, 不可
----------------------------------------
RecType, int, 4, 花销类型, , 不是, 不可
----------------------------------------
RecValue, decimal, 9, 花销金额, , 不是, 不可
----------------------------------------
Remark, varchar, 500, 备注, , 不是, 可
----------------------------------------
==============================
主键为
RedId

六子文档:url:admin11.cnblogs.com

7:T_SQL语句转换成SQLite语句
// 取得数据库表中的月份数量
T_SQL:
select count(distinct DATEPART(month, RecDate)) AS CounRecs from recorder
SQLite:
SELECT COUNT(1) FROM (SELECT distinct substr(RecDate, 0, 6) FROM recorder) AS Month

问题:
substr(RecDate, 0, 6 ) 适用于类似2006-5-10而不能适用于2006-05-10这样的日期,具体如何处理,暂时还不知道。
[六子文档:url:admin11.cnblogs.com]

// 取得数据库中某列的值总合。
T_SQL:
select sum(recvalue) as recvalue from recorder
SQLite:
select sum(recvalue) as recvalue from recorder

两个语句是一样的

7:复杂的句子:
取得数据库中的月份总数和月平均值
T_SQL:
select cast(recvalue/CounRecs AS Money) AS PercentValue,recvalue, CounRecs AS Days 
from (select sum(recvalue) as recvalue from recorder )SumValue,(select count(distinct DATEPART(month, RecDate)) AS CounRecs from recorder  ) CountRec

SQLite:
select recvalue/CounRecs AS PercentValue,recvalue, CounRecs AS Days  from (select sum(recvalue) as recvalue from recorder) AS SumValue,    (SELECT COUNT(1) as CounRecs FROM (SELECT distinct substr(RecDate, 0, 6) FROM recorder)) As CountRec

注:本语句中的Days  列其实为月份数

// 取得当前记录的记录日期,金额总数,平均化小。(每日)
T_SQL:
select Cast(recvalue/CounRecs AS Money) AS PercentValue,recvalue, CounRecs AS Days  from (select sum(recvalue) as recvalue from recorder)SumValue,(select count(distinct convert(varchar(10),recdate,120)) AS CounRecs from recorder ) CountRec

SQLite:
SELECT recvalue/CounRecs AS PercentValue,recvalue, CounRecs AS Days  from (select sum(recvalue) as recvalue from recorder ) as SumValue,  (SELECT COUNT(1) as CounRecs FROM (SELECT distinct substr(RecDate, 0, 9) FROM recorder) daynum) as CounRec

[六子文档:url:admin11.cnblogs.com]

// 简单的视图操作:
//  分类花销记录
// T_SQL  语句:
SELECT Sum(RecValue) AS SumVAalue,  RecTypeName, COUNT(1)  AS SaleTime FROM V_Recorder  Group By RecTypeName

//  SQLite语句:
SELECT Sum(RecValue) AS SumVAalue,  RecTypeName, COUNT(1)  AS SaleTime FROM V_Recorder  Group By RecTypeName

两个语句是一样的

原文地址:https://www.cnblogs.com/admin11/p/445445.html