使用“用于SQLite3的简单Delphi包装器”插入日期。 [英] Inserting dates using "A Simple Delphi Wrapper for SQLite3"

查看:119
本文介绍了使用“用于SQLite3的简单Delphi包装器”插入日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用delphi 2010和Tim Anderson的SQLite3包装器- http://www.itwriting .com / blog /?page_id = 659 -但我无法插入日期

I am using delphi 2010, and Tim Anderson's SQLite3 wrapper - http://www.itwriting.com/blog/?page_id=659 - but I am having trouble inserting dates

这是我的数据库创建

DB.ExecSql('CREATE TABLE Tags (No Integer NOT NULL, Title VarChar(25) NOT NULL, Creator VarChar(25) NULL, Born Date NULL, Charter Boolean Default False NULL, Owned Boolean Default False NULL, Image Blob NULL, CONSTRAINT PK_No PRIMARY KEY (No));');

哪个版本可以正常工作。我使用SQLite管理员进行了测试- http://sqliteadmin.orbmu2k.de/
我什至能够使用管理员手动输入日期

Which builds and works fine. I tested it with SQLite administrator - http://sqliteadmin.orbmu2k.de/ I am even able to manually enter dates using adminstrator

这是我的插入内容

DB.ExecSql('Insert into Tags (No, Title, Creator, Born, Charter, Owned) ' +
             'values (' + quotedStr(frmTag.edtTagNo.Text) + ',' + quotedStr(frmTag.edtTitle.Text) + ',' +
                          quotedStr(frmTag.edtCreator.Text) + ',' + quotedStr(frmTag.edtBorn.Text) + ',' +
                          quotedStr(BoolToStr(frmTag.cbxCharter.Checked)) + ',' + quotedStr(BoolToStr(frmTag.cbxOwned.Checked)) + ');');

日期字段由edtBorn控件(TRzDateEdit)提供

The date field is being supplied by the edtBorn control (TRzDateEdit)

我在插入之前检查了edtBorn.Text和edtBorn.date的值,并且日期始终正确。

I have checked the values of edtBorn.Text amd edtBorn.date prior to the insert and the date is always correct.

我尝试插入

frmTag.edtBorn.Text 
FormatDateTime('mm/dd/yyyy',frmTag.edtBorn.Text)
quotedStr(frmTag.edtBorn.Text)
quotedStr(FormatDateTime('mm/dd/yyyy',frmTag.edtBorn.Text))

我什至尝试使用参数

 DB.AddParamText('@ABorn', frmTag.edtBorn.Text);
 DB.AddParamFloat('@ABorn', frmTag.edtBorn.Date);

似乎没有任何效果!我没有例外,但我的字段从未获得过dat值!

Nothing seems to work! I get no exceptions, yet my field never gets a dat value!

推荐答案

日期和时间数据类型

SQLite能够将日期和时间存储为TEXT,REAL或INTEGER值:

SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:


  • TEXT作为ISO8601字符串( YYYY-MM-DD HH:MM:SS.SSS)。

  • REAL为儒略日数,即自公元前4714年11月24日格林威治中午以来的天数。

  • INTEGER as Unix Time,是自1970-01-01 00:00:00 UTC以来的秒数。

此处显示了REAL和INTEGER部分。

修改后的SQLite3包装器测试文件: uTestSqlite

modified SQLite3 wrapper Testfile: uTestSqlite

sSQL := 'CREATE TABLE testtable ([ID] INTEGER PRIMARY KEY,[OtherID] INTEGER NULL,';
sSQL := sSQL + '[Name] VARCHAR (255),[Number] FLOAT,[Date] INTEGER, [notes] BLOB,
       [picture] BLOB COLLATE NOCASE);';
sldb.execsql(sSQL);
sldb.execsql('CREATE INDEX TestTableName ON [testtable]([Name]);');

//begin a transaction
sldb.BeginTransaction;

sSQL := 'INSERT INTO testtable(Name,OtherID,Number,Date) VALUES ("Some Name", 4,
         julianday("now"), strftime("%s","now"));';
sldb.ExecSQL(sSQL);

sSQL := 'INSERT INTO testtable(Name,OtherID,Number,Date,Notes) VALUES ("Another Name",12,
         julianday("2013-03-01"),strftime("%s","2013-03-01"), "More notes");';
sldb.ExecSQL(sSQL);

//end the transaction
sldb.Commit;

[...]

//query the data
sltb := slDb.GetTable('SELECT * FROM testtable');
if sltb.Count > 0 then
begin
//display first row
updateFields;
end;

显示值:

procedure TForm1.updateFields;
var
Notes: string;
myDate :TDateTime;

begin
ebName.Text := sltb.FieldAsString(sltb.FieldIndex['Name']);
ebID.Text := inttostr(sltb.FieldAsInteger(sltb.FieldIndex['ID']));

if TryJulianDateToDateTime(sltb.FieldAsDouble(sltb.FieldIndex['Number']),myDate)
   then
   ebNumber.Text := DateTimeToStr(myDate)
   else
   ShowMessage('Not a valid Julian date');

myDate:=UnixToDateTime(sltb.FieldAsInteger(sltb.FieldIndex['Date']));
ebDate.Text := DateTimeToStr(myDate);

[...]
end;

输出:

在您遇到的 DB.ExecSql('CREATE TABLE Tags(...,Born Date NULL,...);');

替换为 DB.ExecSql('插入标签(....)值(...

+ quotedStr(frmTag.edtBorn.Text) 

with

'strftime("%Y-%m-%d","'+frmTag.edtBorn.Text+'")'

frmTag.edtBorn.Text 的值必须类似于 1975-10-21

您可以通过以下方式获取它:

You can get it with:

ebDate.Text := sltb.FieldAsString(sltb.FieldIndex['Born']);

这篇关于使用“用于SQLite3的简单Delphi包装器”插入日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆