1

我正在编写一个 CF 移动应用程序并正在使用该cfclient标签。我遇到了远程数据源连接问题,无法解决。

在 cfclient 中,我使用“rooms”作为我的数据源字符串,该字符串在 CF 管理员中定义为数据源。它正在连接到远程 SQL Server。“Tblblogs”存在于“rooms”数据库中,但在 cfclient 下我得到一个错误:> 不存在这样的表

但是,如果我采用相同的查询 [blgQ] 在 cfclient 之外选择“tblblogs”,它可以正常工作并且没有问题。我不确定为什么它没有在 cfclient (如管理员中定义)下建立正确的数据源连接。

<!DOCTYPE html>

<html >
    <body>
        <h2>Add Expense</h2>
        <form >
            <table >
                <tr>
                    <td>Date:</td> <td><input type="date" id="dateTxt"></td>
                </tr>
                <tr>
                    <td>Amount:</td> <td><input type="number" id="amtTxt"></td>
                </tr>
                <tr>
                    <td>Description</td>
                    <td><input type="text" id="descTxt"></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="button" id="addBtn">Add</button>
                    </td>
                </tr>
            </table>
        </form>

        <h2>Expenses:</h2>
        <table id="expList">
            <tr>
                <th>Date</th>
                <th>Amount</th>
                <th>Description</th>
            </tr>
        </table>
    </body>
</html>

<script >
    document.getElementById("addBtn").onclick = function(){
        addExpense();
    }
</script>


<!--- cfclient code starts here --->
<cfclient>
    <cfset document.getElementById("expList").innerHTML =''>
    <!--- on client side you do not need to pre-configure datasource --->
    <cfset dsn = "rooms">
    <cftry>

    <!--- create database if not already created --->
    <cfquery datasource="rooms">
        create table if not exists expenses (
            id integer primary key,
            expense_date integer,
            amount real,
            desc text
        )
    </cfquery>

    <!--- Get expense records from the table --->
    <cfquery datasource="rooms" name="expenses">
        select * from expense order by expense_date desc
    </cfquery>
    <cfset alert(expenses.amount)>



    <!--- Loop over expenses query object and display --->
    <cfloop query="expenses">
        <cfset var tmpDate = new Date(expense_date)>
        <cfset addExpenseRow(expense_date,amount,desc)>
    </cfloop>

        <cfcatch type="any" name="e">
            <cfset alert(e.message)>
        </cfcatch>
    </cftry>

    <!--- Helper function to add epxpense row to HTML table --->
    <cffunction name="addExpenseRow" >
        <cfargument name="expense_date" >
        <cfargument name="amt" >
        <cfargument name="desc" >


        <cfoutput >
            <cfsavecontent variable="rowHtml" >
                <tr>
                    <td>#dateFormat(expense_date,"mm/dd/yyyy")#</td>
                    <td>#amt#</td>
                    <td>#desc#</td>
                </tr>
            </cfsavecontent>
        </cfoutput>

        <cfset document.getElementById("expList").innerHTML += rowHtml>
    </cffunction>

    <!--- Called from JS script block in response to click event for addBtn --->
    <cffunction name="addExpense" >
        <cfset var tmpDate = new Date(document.getElementById("dateTxt").value)>
        <cfset var amt = Number(document.getElementById("amtTxt").value)>
        <cfset var desc = document.getElementById("descTxt").value>

        <!--- TODO: Do data validation --->
        <cftry>
        <!--- Insert expense row into database table --->       
            <cfquery datasource="rooms" result="result">
                insert into expense (expense_date,amount,desc) values(
                    <cfqueryparam cfsqltype="cf_sql_date" value="#tmpDate.getTime()#">,
                    <cfqueryparam cfsqltype="cf_sql_numeric" value="#amt#">,
                    <cfqueryparam cfsqltype="cf_sql_varchar" value="#desc#">
                )
            </cfquery>

            <cfcatch type="any" name="e">
                <cfset alert(e.message)>
            </cfcatch>
        </cftry>


        <!--- add the new expense row to HTML table --->
        <cfset addExpenseRow(tmpDate,amt,desc)>
    </cffunction>

</cfclient>

<cfquery datasource="rooms" name="blgQ">
        select * from tblblogs 
    </cfquery>
<cfdump var="#blgQ#"

> Blockquote
4

1 回答 1

3

<cfquery>on<cfclient>与常规的 不完全一样<cfclient>

它旨在与 Web SQL 进行轻量级交互。Web SQL 没有得到普遍支持,而且不太可能。<cfclient>也会遭受所有困扰的问题<cfform>。即 javascript 会前进,但这个标签生成的代码可能不会。

请参阅用于移动开发的客户端 CFML

我怀疑您正在尝试做一些可能更适合 AJAX 或 REST 的事情

于 2017-05-28T18:23:54.883 回答