将 JSON 对象插入数据库
我正在 Delphi XE2 中构建 DataSnap 服务器,但我无法找出将客户端发送给我的 JSON 对象插入数据库的最佳方法。
这是我收到的对象的格式:
{"PK":0,"FIELD1":"EXAMPLE","FIELD2":5, "DATE":""}
我找到的解决方案是以下代码
with qryDBMethods do
begin
SQL.Text := 'SELECT * FROM Table';
Open;
Append;
FieldByName('PK') .AsInteger := StrToInt(newId.ToString)
FieldByName('FIELD1').AsString := Object.Get('FIELD1').JsonValue.Value;
FieldByName('FIELD2').AsInteger := StrToInt(Object.Get('FIELD2').JsonValue.Value);
FieldByName('DATE') .AsDateTime:= Now;
Post;
之后,我将查询组件制作成 JSON 对象并返回给我的客户端,但问题是,这将是一个很大的问题应用程序具有密集的表,因此每次我想插入某些内容时执行“SELECT *”并不理想。最好的方法是什么?
I am building a DataSnap server in Delphi XE2, and I'm having trouble figuring out the best way to insert the JSON Object that the client sends me into a Database.
Here's the format of the object I'm receiving:
{"PK":0,"FIELD1":"EXAMPLE","FIELD2":5, "DATE":""}
The solution I found was the following code
with qryDBMethods do
begin
SQL.Text := 'SELECT * FROM Table';
Open;
Append;
FieldByName('PK') .AsInteger := StrToInt(newId.ToString)
FieldByName('FIELD1').AsString := Object.Get('FIELD1').JsonValue.Value;
FieldByName('FIELD2').AsInteger := StrToInt(Object.Get('FIELD2').JsonValue.Value);
FieldByName('DATE') .AsDateTime:= Now;
Post;
After that, I would have my query component made into a JSON Object and returned to my client, but problem is, this is going to be a big application with dense tables and so doing a "SELECT * " every time I want to insert something isn't ideal. What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 INSERT 语句代替。
Try using a INSERT sentence instead.
如果问题是打开
Select * From Table
时的数据量,为什么不执行Select * From TableWhere 1 <> 之类的操作? 1?
这样您就可以在不加载任何结果的情况下插入。
其他选项是制作插入脚本。
If what the problem is the amount of data when you open a
Select * From Table
, why not do something likeSelect * From Table Where 1 <> 1
?This way you would be able to insert while not loading any result.
Other option would to make an Insert script instead.