刚接触mORMot2,感觉发现了Free Pascal世界的后端的新大陆,笔者正在摸索。
准备:TSQLRestServerDB实例、已连接到数据库
使用OneFieldValues方法获取所有符合条件的数据的ID
使用每行数据的ID获取此行在其它列中的数据
随手写的示例,测试环境使用MariaDB并使用ZeosLib进行连接,可以正常工作
示例将会使用ShowMessage展示符合条件的元素的数量以及第一个元素的ID
1 2 3 4 5 6 7 8 9 10 11 | procedure TForm1.Button2Click(Sender: TObject); var rec: TOrmRecord; arr: TRawUtf8DynArray; begin DB.OneFieldValues(TOrmRecord, 'id', 'content='+#39+Edit1.Caption+#39, arr); rec:=TOrmRecord.Create(DB.Orm, StrToInt(arr[0])); ShowMessage(IntToStr(length(arr))); ShowMessage(IntToStr(rec.ID)); rec.Free; end; |
本人参考的帖子:https://www.synopse.info/forum/viewtopic.php?id=122
或者使用TSQLRestServerDB的RetrieveListObjArray方法,通过动态数组变量传回数据(记得释放对象):
1 2 3 4 5 6 7 8 9 10 11 12 | procedure TForm1.Button2Click(Sender: TObject); var rec: TOrmRecord; arr: array of TOrmRecord; begin DB.RetrieveListObjArray(arr, TOrmRecord, 'content=?', [Edit2.Caption]); ShowMessage(IntToStr(length(arr))); for rec in arr do begin ShowMessage(IntToStr(rec.ID)); rec.Free;//勿忘 end; end; |
参考文章:https://stephan-bester.medium.com/the-orm-in-mormot-3a11c8ac0c35
发表回复