SQLite 建表语句

-- 获取当前日期时间(本地时间)
CREATE TABLE Logs (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Message TEXT NOT NULL,
    LogTime DATETIME DEFAULT (datetime('now', 'localtime')),
    LogDate DATE DEFAULT (strftime('%Y-%m-%d', 'now', 'localtime'))/*当前本地时间不带时分秒*/
    ,CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP /*当前时间戳(注意不是本地时间而是UTC 时间)*/
    ,Amount REAL    
    ,ImageData BLOB/*二进制大对象*/
);

-- 只获取日期部分
CREATE TABLE DailyRecords (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    RecordDate DATE DEFAULT (date('now')),
    Description TEXT
);

-- 只获取时间部分
CREATE TABLE TimeRecords (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    RecordTime TIME DEFAULT (time('now', 'localtime')),
    Activity TEXT
);

 

-- 比较不同的日期时间计算方式
SELECT 
    datetime('now') as 当前时间,
    
    -- 方法1:使用 julianday() 计算精确时间差
    julianday('now') - julianday('2024-12-28 12:00:00') as 精确天数差,
    
    -- 方法2:使用 strftime 计算整数天数差
    strftime('%s', 'now') - strftime('%s', '2024-12-28 12:00:00') as 秒数差,
    (strftime('%s', 'now') - strftime('%s', '2024-12-28 12:00:00')) / 86400.0 as 由秒数转换的天数差
    
    select 
    julianday('now') - julianday('2024-12-28 12:00:00') as 总天数差,
    (julianday('now') - julianday('2024-12-28 12:00:00')) * 24 as 小时差,
    (julianday('now') - julianday('2024-12-28 12:00:00')) * 24 * 60 as 分钟差,
    (julianday('now') - julianday('2024-12-28 12:00:00')) * 24 * 60 * 60 as 秒数差

 

posted on 2025-11-11 14:04  wakaka_wka  阅读(2)  评论(0)    收藏  举报