如何从实时代码数据网格中所做的更改更新数据库

How to update database from change made in livecode datagrid

我在实时代码中有一个数据网格。我想在用户对数据网格的一行进行更改后更新基础数据库中的数据

嗨,马尔特,

这看起来很有希望。唯一的问题是数据库中的名称字段 table 与列名不同。我想使用 sql 更新语句来更新数据库。我尝试了以下代码

on CloseFieldEditor pFieldEditor

--Connect to database
  databaseConnect
--Update Record

put  GetDataOfLine( the dgHilitedlines of me,"Pathogen") into strPathogen
put  GetDataOfLine( the dgHilitedlines of me,"Offset") into strIncubation
put  GetDataOfLine( the dgHilitedlines of me,"Duration") into     strDurationofIllness
put  GetDataOfLine( the dgHilitedlines of me,"ID") into IntID

put "UPDATE  tblPathogen SET fldPathogenName='" & strPathogen & "',     fldIncubation='" & strIncubation &  "', fldDurationofIllness='" &     strDurationofIllness & "'" into strSQL
put " WHERE fldPathogenID=" & IntID after strsql

put strsql into field "test"

--SaveDataToDatabase theTable, theRowID, theColumnBeingEdited, theNewText

end CloseFieldEditor    

问题是网格中的值在分配给变量之前变回原始值。如何更新网格以保存 pFieldEditor

的文本

如果您不使用自己的模板覆盖标准数据网格行为,则会向网格控件发送一条 CloseFieldEditor 消息。您可以使用该消息对数据更改做出反应并触发将数据存储在数据库中的处理程序。

这里的问题是您捕获了实际更新数据网格的处理程序。在常规情况下,数据网格的数据会在消息执行后更新。所以在你的情况下,你会想要自己更新数据...

修改后的脚本:

on CloseFieldEditor pFieldEditor
   -- declared locals to make it compile in strict mode
   local strPathogen,strIncubation,strDurationOfIllness,intID,strSQL
   local theColumnBeingEdited,tLineDataArray
   local tNewText


   put the text of pFieldEditor into tNewText
   -- this holds the value the user entered

   put the dgColumn of the target into theColumnBeingEdited
   -- find the column that has been changed

   put the dgDataOfLine[the dgHilitedLine of me] of me into tLineDataArray
   -- get the whole line of data and put it into an array

   put tNewText into tLinedataArray[theColumnBeingEdited]
   -- update the col in the array accordingly

   set the dgDataOfLine[the dgHilitedLine of me] of me to tLineDataArray
   -- update data in grid

   --Connect to database
   databaseConnect
   --Update Record

   put  GetDataOfLine( the dgHilitedlines of me,"Pathogen") into strPathogen
   put  GetDataOfLine( the dgHilitedlines of me,"Offset") into strIncubation
   put  GetDataOfLine( the dgHilitedlines of me,"Duration") into     strDurationofIllness
   put  GetDataOfLine( the dgHilitedlines of me,"ID") into IntID

   put "UPDATE  tblPathogen SET fldPathogenName='" & strPathogen & "',     fldIncubation='" & strIncubation &  "', fldDurationofIllness='" &     strDurationofIllness & "'" into strSQL
   put " WHERE fldPathogenID=" & IntID after strsql

   put strsql into field "test"

   --SaveDataToDatabase theTable, theRowID, theColumnBeingEdited, theNewText

end CloseFieldEditor    

这里解释得很好:http://lessons.runrev.com/m/datagrid/l/7337-how-do-i-save-changes-the-user-makes-in-an-editor-field-to-an-external-data-source

请记住,虽然脚本并未针对严格编译模式进行优化(我始终建议使用这种模式,因为它可以避免你搬起石头砸自己的脚)。如果你想让脚本在严格模式下编译,你还需要声明变量。