在coldfusion组件中,我有不同的功能,我在其中创建一个sql查询并执行i。代码如下:
<cffunction name="buildQuery" access="public" returntype="string">
<cfargument name="json" required="yes" type="struct">
<cfset blockStatements = "">
<cfloop index="j" from="1" to="#ArrayLen(arguments.json.rows[arguments.json.columns[1]])#">
<cfscript>
statement = "insert into " & arguments.json.tableName &" ("&ArrayToList(arguments.json.columns)&") values (";
for (i=1;i LTE ArrayLen(arguments.json.columns);i=i+1) {
value = transformValue(arguments.json.rows[arguments.json.columns[i]][j],arguments.json.tableInfo[arguments.json.columns[i]]);
<!--- transformValue just return my value formated, if requires '' o numeric without them--->
if(Find('identity',arguments.json.tableInfo[arguments.json.columns[i]],0)){
statement = Replace(statement, ","&arguments.json.columns[i], "" , "all");
}
<!--- here I remove all primary keys from the exported data--->
statement = statement & value & ",";
}
}
statement = statement & ");";
statement = Replace(statement, ",);", ");" , "all");
blockStatements = blockStatements & statement;
</cfscript>
</cfif>
</cfloop>
<cfif blockStatements neq "">
<cfscript> executeQuery(blockStatements);<cfscript>
</cfif>
</cffunction>
<cffunction name="executeQuery" access="public" returntype="string">
<cfargument name="query" required="yes" type="string">
<cfdump var="#query#">
<cfquery name="q_query" datasource="myDB">
#Replace(query, "''", "'" , "all")#
</cfquery>
</cffunction>
在line 4
中,在致电executeQuery
之前,queryString
有一些特殊字符(á,é,í,ó,ú)。我已直接在我的query
上测试了SQL DB
,并按指定的方式工作,允许我在varchar
列中包含这些特殊字符。
调用executeQuery
函数后,在line 10
上,所有这些特殊字符都被替换为'í' -> 'Ã-'
。然后,该函数在DB中执行此“新”查询和insert
。
调用函数时,如何在查询中保留这些特殊字符?
应该有一种方法可以正确地进行,而不是从另一方replace
。
我为上传表单添加了一个snipplet:
function validate(form) {
console.log(window.b = form);
}
<form action="Importar_form.cfm" onsubmit="return validate(this);" method="post" enctype="multipart/form-data">
<cfoutput>Escoja un archivo: </cfoutput><br/>
<input type="file" name="fileToUpload" id="fileToUpload" accept="text/plain, .txt"required>
<br/><br/>
<input type="submit" value="Upload" name="submit">
</form>
<cfif isdefined('form.FILETOUPLOAD')>
<cftry>
<cfscript>
myfile = FileRead("#form.FILETOUPLOAD#");
record=deserializeJSON(myfile);
</cfscript>
<cfcatch>
<cfthrow message = "Archivo dañado, no se puede cargar.">
</cfcatch>
</cftry>
<cfset componentPath = "myproject.Componentes.ImportData">
<cfset componentOBJ = createObject("component","#componentPath#")>
<cfset result = componentOBJ.buildQuery(
json = "#record#"
, method = "#form.method#"
)>
<script>
alert("Se ha importado con &eeacute;xito");
</script>
</cfif>
要上传的file.txt的内容:
{"tableName":"CRCParametros","tableInfo":{"Pcategoria":"varchar","Usucrea":"numeric","updatedat":"datetime","Pcodigo":"varchar","Pvalor":"varchar","TipoDato":"varchar","Mcodigo":"varchar","Pdescripcion":"varchar","createdat":"datetime","deletedat":"datetime","id":"intidentity","TipoParametro":"varchar","Parametros":"varchar","Usumodif":"numeric"},"keyColumnName":"Pcodigo","rows":{"Pcategoria":["Categoría"],"Usucrea":[24],"updatedat":[""],"Pcodigo":[30000601],"Pvalor":[10],"TipoDato":["E"],"Mcodigo":["CREDADM"],"Pdescripcion":["DiadeCorteQuincena1"],"createdat":["November,14201700:00:00"],"deletedat":[""],"id":[1],"TipoParametro":["R"],"Parametros":["1-15"],"Usumodif":[""]},"columns":["Pcategoria","Usucrea","updatedat","Pcodigo","Pvalor","TipoDato","Mcodigo","Pdescripcion","createdat","deletedat","id","TipoParametro","Parametros","Usumodif"]}
字面上ctrl + c,ctrl + v到内容。如您所见,json中的单词“Categoría
”将作为“CategorÃ-a
”在查询中保存在数据库中。但是DB支持角色“í
”。
任何解释都会很乐意欣赏!