Coldfusion csvToArray和insert插入数据库未按预期工作
本周早些时候,我问社区如何从Ben Nadel编写的一个名为csvToArray的函数中插入信息,然而,我最终使用了另一个似乎运行得同样好或更好的函数。现在的问题是:使用这个新函数后,我能够从数组中获取数据,但是当我将其插入数据库时,我从coldfusion中得到以下错误消息:“无法找到用作表达式一部分的数组对象的维度为2的第16位元素。”有没有人有这方面的经验,知道我为什么会收到这条消息?其他一切都按计划进行。以下是相关代码:Coldfusion csvToArray和insert插入数据库未按预期工作,coldfusion,coldfusion-10,Coldfusion,Coldfusion 10,本周早些时候,我问社区如何从Ben Nadel编写的一个名为csvToArray的函数中插入信息,然而,我最终使用了另一个似乎运行得同样好或更好的函数。现在的问题是:使用这个新函数后,我能够从数组中获取数据,但是当我将其插入数据库时,我从coldfusion中得到以下错误消息:“无法找到用作表达式一部分的数组对象的维度为2的第16位元素。”有没有人有这方面的经验,知道我为什么会收到这条消息?其他一切都按计划进行。以下是相关代码: <cfif request_method is "P
<cfif request_method is "Post" AND isDefined("form.fileUpload") EQ 1>
<cffile action="read" file="#form.filecontent#" variable="csvfile">
<cfinvoke component="#application.path.cfc#.Organizations" method="csvToArray" returnvariable="getArraData">
<cfinvokeargument name="fileContent" value="#csvfile#">
</cfinvoke>
<!--- Loop the array starting on the 2 index to remove the header info from the insert --->
<cfloop from="2" to="#arrayLen(getArraData)#" index="i">
<cfset indexNum = i > <!--- Start with 2 index then increment the value by 1 on the loop --->
<cfloop from="2" to="#arrayLen(getArraData[i])#" index="j">
<cfset dimNum = j > <!--- Start with 2 index then increment the value by 1 on the loop --->
<!--- After loop ends. nunbers are dynamically placed into position [2][2] and insert the values of the index deminsion--->
<cfquery datasource ="#application.dsn.name#" name="BudgetInsert">
<!--- Insert Data here --->
INSERT INTO sales_budget
(
nOrganizationID
,nLocationID
,nBudgetTypeID
,nBudgetYear
,month1
,month2
,month3
,month4
,month5
,month6
,month7
,month8
,month9
,month10
,month11
,month12
,nActive
,tCreationDate
,tLastUpdate
,cChangedBy
)
VALUES
(
<cfqueryparam cfsqltype="cf_sql_integer" value="#url.OrgID#">
,<cfqueryparam cfsqltype="cf_sql_integer" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_integer" value="2">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#getArraData[indexNum][dimNum]#">
,<cfqueryparam cfsqltype="cf_sql_bit" value="1">
,GetDate()
,GetDate()
,<cfqueryparam cfsqltype="cf_sql_integer" value="#Session.Auth.UserID#">
)
</cfquery>
<cfset dimNum = j +1 >
</cfloop>
indexNum = i +1
</cfloop>
</cfif>
插入销售预算
(
无组织ID
,nLocationID
,nBudgetTypeID
,预算年度
,月1日
,月2日
,三月三日
,月4日
,月5日
,月6日
,月7日
,月8日
,月9日
,月10日
,11月1日
,12月1日
,nActive
,tCreationDate
,日期
,由
)
价值观
(
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,GetDate()
,GetDate()
,
)
indexNum=i+1
您应该能够使用
读取CSV文件并将其移动到数据库中。虽然大多数现代数据库都有直接导入CSV文件的命令(根据Leigh的评论),但如果您没有正确的权限,那么这是最好的选择
<cfspreadsheet
action="read"
src = "filepath"
columns = "range"
columnnames = "comma-delimited list"
excludeHeaderRow = "true | false"
format = "CSV|HTML"
headerrow = "row number"
name = "text"
query = "query name"
rows = "range"
sheet = "number"
sheetname = "text">
这将返回一个普通的CF查询对象,您可以在该对象上循环插入。我将其描述为:内部循环需要引用外部循环,如下所述:如果您认为该解决方案将帮助其他人,则应将其作为“答案”发布。可以在S.O.上回答您自己的问题。也就是说,您是否有理由必须使用数组而不是使用DB工具来加载CSV,即
批量插入
(SQL Server),加载数据填充
(MySQL),等等。这些通常比循环更有效。