Search code examples
mobilecoldfusioncfclient

coldfusion mobile app - connection to remote database server under cfclient tag


I am writing a CF mobile app and am using the cfclient tag. I am encountering issues with remote datasource connections and can’t get it resolved.

From cfclient, I am using “rooms” as my datasource string which is defined in the CF Administrator as a data source. It is connecting to a remote SQL Server. “Tblblogs” exists in the "rooms" database, but under cfclient I get an error that: > No such table exists

However, if I take same query [blgQ] that selects “tblblogs” outside cfclient, it works fine and without issue. I am not sure why it is not making right datasource connection under cfclient (as defined in the administrator) .

<!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

Solution

  • <cfquery> on <cfclient> is not really the same thing as the regular <cfclient>.

    It is intended to do light weight interactions with Web SQL. Web SQL is not universally supported, and it is not likely to. <cfclient> will also suffer from all the issues that plagued <cfform>. Namely javascript will move forward, but the code generated by this tag may not.

    See Client side CFML For Mobile Development.

    I suspect you are trying to do something that might be more appropriate with AJAX or REST