coldfusion移动应用程序 - 连接到cfclient标记下的远程数据库服务器

时间:2017-05-28 10:46:05

标签: mobile coldfusion cfclient

我正在编写CF移动应用并使用cfclient标记。我遇到远程数据源连接的问题,无法解决它。

从cfclient,我使用“rooms”作为我的数据源字符串,它在CF管理员中定义为数据源。它连接到远程SQL Server。 “Tblblogs”存在于"房间"数据库,但在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

1 个答案:

答案 0 :(得分:3)

<cfquery>上的{p> <cfclient>与常规<cfclient>的内容并不相同。

旨在与Web SQL进行轻量级交互。 Web SQL并非普遍受支持,并且不太可能。 <cfclient>也会遇到困扰<cfform>的所有问题。即javascript将向前移动,但此标记生成的代码可能不会。

请参阅Client side CFML For Mobile Development

我怀疑你正在尝试用AJAX或REST

做一些更合适的事情