delphi FireDAC 下的 Sqlite [5] - 数据的插入、更新、删除


本文整理自网络,侵删。

 

先在空白窗体上添加: TFDConnection、TFDPhysSQLiteDriverLink、TFDGUIxWaitCursor、TFDQuery、TDataSource、TDBGrid(并在设计时关联好).

代码:

{建立}

procedure TForm1.FormCreate(Sender: TObject);

const

  dbPath = 'C:\Temp\SQLiteTest.sdb';

  strTable = 'CREATE TABLE MyTable(Id integer PRIMARY KEY AUTOINCREMENT, Name string(10), Age byte)'; //Id, Name, Age 三个字段

                                                                                                      //integer PRIMARY KEY AUTOINCREMENT: 自增字段

begin

  if FileExists(dbPath) then DeleteFile(dbPath);

 

  FDConnection1.ConnectionString := 'DriverID=SQLite; Database=' + dbPath;

  FDConnection1.ExecSQL(strTable);

 

  FDQuery1.Open('SELECT * FROM MyTable');

end;

 

{插入}

procedure TForm1.Button1Click(Sender: TObject);

const

  strInsert = 'INSERT INTO MyTable(Name, Age) VALUES(:name, :age)'; //:name, :age 的方式(后面还要以数组的方式给出相应的值), 这比字符串的 Format 函数还要方便.

begin

  FDConnection1.ExecSQL(strInsert, ['AAA', 11]);

  FDConnection1.ExecSQL(strInsert, ['BBB', 22]);

  FDConnection1.ExecSQL(strInsert, ['CCC', 33]);

  FDConnection1.ExecSQL(strInsert, ['DDD', 44]);

  FDConnection1.ExecSQL(strInsert, ['EEE', 55]);

  FDQuery1.Refresh;

end;

 

{更新}

procedure TForm1.Button2Click(Sender: TObject);

begin

  FDConnection1.ExecSQL('UPDATE MyTable SET Age=:a WHERE Name=:n', [Random(100), 'AAA']);

  FDQuery1.Refresh;

end;

 

{删除}

procedure TForm1.Button3Click(Sender: TObject);

begin

  FDConnection1.ExecSQL('DELETE FROM MyTable WHERE Age>33');

  FDQuery1.Refresh;

end;

 

{查询符合条件的第一个结果}

procedure TForm1.Button4Click(Sender: TObject);

var

  V: Variant;

begin

  V := FDConnection1.ExecSQLScalar('SELECT Age FROM MyTable WHERE Name = :x', ['BBB']);

  ShowMessage(V);

end;

 

//:

select * from test where name = 'ABCDE' COLLATE NOCASE;

create table test (id Integer,name Text COLLATE NOCASE );

相关阅读 >>

Delphi showdebuginfo 窗口

Delphi webbrowser 使滚动条滚动到底部

Delphi嵌入循环汇编

Delphi 中format的字符串格式化使用说明

Delphi硬盘序列号修改器源码

Delphi通过setupapi.dll列举和停用硬件设备

Delphi xe android 程序切换到后台及从后台切换到前台实现

Delphi 获取外网ip的函数

Delphi 和微软的 azure 云存储

Delphi 判断文件名是否合法,判断文件名路径是否合法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...