-1

I'm using Dojo 1.8, MySQL and the IBM Worklight framework 6.0 to create a mobile application. I need to handle Dates (dojo/DateTextBox) so that they can be inserted into the database and retrieved from the Database to populate the date text boxes.

Can I get an example of how I can accomplish this?

Spindoctor
  • 434
  • 3
  • 17

2 Answers2

0

This has got nothing to do with Dojo per-se. Worklight doesn't care which framework you're using for your application's UI, as long as you're using the Worklight API correctly. Meaning, as long as you pass the values-to-be-inserted to the adapter call.

See my answer here for an end-to-end example: https://stackoverflow.com/a/25164028/1530814


In the below example I pass the values directly using jQuery: $('#value1').val(), but it can be done in other ways too.

HTML:

<h1>Test Insert Into Database</h1>
<input type="text" id="value1" placeholder="value1"/><br/>
<input type="text" id="value2" placeholder="value2"/><br/>
<input type="button" value="Insert values to database" onclick="insertValuesToDB();"/>

main.js:

function insertValuesToDB() {
    var invocationData = {
        adapter: 'insertValuesAdapter',
        procedure: 'insertValuesProcedure',
        parameters: [$('#value1').val(), $('#value2').val()]
    };

    WL.Client.invokeProcedure(invocationData, {onSuccess: insertSuccess, onFailure: insertFailure});
}

function insertSuccess() {
    alert("success");
}

function insertFailure() {
    alert("failure");
}

Adapter XML:

...
...
<connectivity>
    <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
        <dataSourceDefinition>
            <driverClass>com.mysql.jdbc.Driver</driverClass>
            <url>jdbc:mysql://localhost:3306/worklight_training</url>
            <user>Worklight</user>
            <password>Worklight</password> 
        </dataSourceDefinition> 
    </connectionPolicy>
    <loadConstraints maxConcurrentConnectionsPerNode="5" />
</connectivity>

<procedure name="insertValuesProcedure"/>
...
...

Adapter implementation:

var insertValuesProcedureStatement = WL.Server.createSQLStatement("INSERT INTO users(userId, firstName, lastName, password) VALUES (?,?, 'someLastName', 'somePassword')");

function insertValuesProcedure(value1,value2) {
    return WL.Server.invokeSQLStatement({
        preparedStatement : insertValuesProcedureStatement,
        parameters : [value1,value2]
    });
}
Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • Following your example, I am simply retrieving the value of the DateTextbox and passing it to the adapter Fri Apr 17 2015 00:00:00 GMT-0400 (Eastern Daylight Time) When it is passed to the adapter however, I get this error "preparedStatement": "insert into property (propertytype, streetnumber, streetname, city, province, zippostal, email, phonenumber, availablefrom, availableto, userid, status) values ( } ] } Data truncation: Incorrect date value: '2015-04-17T04:00:00.000Z' for column 'availablefrom' at row 1. Performed query: insert into property – Spindoctor Apr 17 '15 at 15:21
  • Then check you're using the correct format for your date. http://stackoverflow.com/questions/14461550/data-truncation-incorrect-date-value – Idan Adar Apr 17 '15 at 15:23
0

This is what is needed to convert a java script date to a MYSQL date

Convert JS date time to MySQL datetime

Community
  • 1
  • 1
Spindoctor
  • 434
  • 3
  • 17