如何使用动态行数更新数据库?
How to update database with dynamic number of rows?
我有一个表单,其中要更新的行数未知,有些可能需要插入,有些可能需要在数据库中更新。如果需要,我已经给出了每个输入的名称和 ID。
这是表单输出的示例,表单中的每一行都有 3 个输入:
plcid_1 =
consumed_1 = 850
runtime_1 = 20
plcid_2 = 1
consumed_2 = 500
runtime_2 = 25
plcid_3 =
consumed_3 = 100
runtime_3 = 50
plcid_4 =
consumed_4 = 485
runtime_4 = 20
plcid_5 = 2
consumed_5 = 400
runtime_5 = 39
我需要做的是遍历每个集合并执行以下操作:
当plcid_n
为空时:
INSERT INTO Table (consumed, runtime) VALUES (850,20)
INSERT INTO Table (consumed, runtime) VALUES (100,50)
INSERT INTO Table (consumed, runtime) VALUES (485,20)
但是当 plcid_n
有一个值时:
UPDATE Table SET consumed='500', runtime='25' WHERE plcid='1'
UPDATE Table SET consumed='400', runtime='39' WHERE plcid='2'
我对每个函数所做的任何操作都只使用每个单独的表单项而不是集合,所以我无法创建数据库查询。
这应该有效:
dim fld, plcid, consumed, runtime
for each fld in request.form
if left(fld, 6) = "plcid_" then
plcid = mid(fld, 7)
consumed = request.form("consumed_" & plcid)
runtime = request.form("runtime_" & plcid)
if request.form(fld) = "" then
sql = "INSERT INTO TABLE (consumed, runtime) VALUES(" & consumed & "," & runtime & ")"
' execute
else
sql = "UPDATE TABLE SET consumed = " & consumed & ", runtime = " & runtime & " WHERE plcid = " & plcid
' execute
end if
end if
next
我有一个表单,其中要更新的行数未知,有些可能需要插入,有些可能需要在数据库中更新。如果需要,我已经给出了每个输入的名称和 ID。
这是表单输出的示例,表单中的每一行都有 3 个输入:
plcid_1 =
consumed_1 = 850
runtime_1 = 20
plcid_2 = 1
consumed_2 = 500
runtime_2 = 25
plcid_3 =
consumed_3 = 100
runtime_3 = 50
plcid_4 =
consumed_4 = 485
runtime_4 = 20
plcid_5 = 2
consumed_5 = 400
runtime_5 = 39
我需要做的是遍历每个集合并执行以下操作:
当plcid_n
为空时:
INSERT INTO Table (consumed, runtime) VALUES (850,20)
INSERT INTO Table (consumed, runtime) VALUES (100,50)
INSERT INTO Table (consumed, runtime) VALUES (485,20)
但是当 plcid_n
有一个值时:
UPDATE Table SET consumed='500', runtime='25' WHERE plcid='1'
UPDATE Table SET consumed='400', runtime='39' WHERE plcid='2'
我对每个函数所做的任何操作都只使用每个单独的表单项而不是集合,所以我无法创建数据库查询。
这应该有效:
dim fld, plcid, consumed, runtime
for each fld in request.form
if left(fld, 6) = "plcid_" then
plcid = mid(fld, 7)
consumed = request.form("consumed_" & plcid)
runtime = request.form("runtime_" & plcid)
if request.form(fld) = "" then
sql = "INSERT INTO TABLE (consumed, runtime) VALUES(" & consumed & "," & runtime & ")"
' execute
else
sql = "UPDATE TABLE SET consumed = " & consumed & ", runtime = " & runtime & " WHERE plcid = " & plcid
' execute
end if
end if
next