【问题标题】:How to update database from change made in livecode datagrid如何从实时代码数据网格中所做的更改更新数据库
【发布时间】:2015-09-13 21:05:52
【问题描述】:

我在 livecode 中有一个数据网格。我想在用户对数据网格的一行进行更改后更新底层数据库中的数据

嗨,马尔特,

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

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 的文本

【问题讨论】:

    标签: datagrid livecode


    【解决方案1】:

    如果您不使用自己的模板覆盖标准数据网格行为,则会将 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

    请记住,这些脚本并未针对严格的编译模式进行优化(我始终建议您使用它,因为它可以避免您自己动手)。如果您希望脚本在严格模式下编译,您还需要声明变量。

    【讨论】:

    • 您好 Malte,感谢您的回复。我只是需要一些澄清。该链接建议使用 CloseFieldEditor 句柄,但尚不清楚何时触发此事件。我尝试了以下代码来测试触发器,但没有发生任何事情`在 CloseFieldEditor pFieldEditor answer "CloseFieldEditor" end CloseFieldEditor `
    • 嗨,Sheils,一旦用户输入的字段消失(用户点击返回,或选项卡超出相关字段),就会触发 rCloseFieldEditor 消息,除非您使用模板设置数据网格这使您可以使用标准行为直接在网格覆盖中进行编辑。
    • 嗨 Malte,请在下面的回答中查看我的其他问题
    • 您好 Malte,非常感谢,一切正常。然而,你只是错过了一点点。使用您的代码,该行中的所有其他数据都将被删除,因此我将整行放入数组中,然后使用以下代码更新数组 - 相应地更新数组中的 col 将我的 dgDataOfLine[我的 dgHilitedLine] 放入tLinedataArray 将 strNewText 放入 tLinedataArray[theColumnBeingEdited]
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2018-10-22
    相关资源
    最近更新 更多