0

I'm a new Cordova-Android program maker. I've successfully create and insert the local database fields. But I stuck when I want to display them into input tag.

This is my table :

+----+------+---------+
| id | word | stats   |
+----+------+---------+
| 1  | abc  | stackov |
| 2  | def  | erflow  |
+----+------+---------+

I try this code

    $(document).ready(function(){
        docDB = window.openDatabase("Test", "1.0", "Test Data", 50000);

        docDB.transaction(function (tx) {
            tx.executeSql("SELECT * FROM Test WHERE word='abc'", [], function (tx, results) {
                var dblen = results.rows.length;
                if( dblen>0 ){
                    document.getElementById("abc").value = results.rows.item(0)['stats'];
                    }
            }, null);
        });
   });

I've searching for simplest way, because I just want to display single SQL column value word='abc' and other word value such word='def'. Can someone correct it?

And I would insert the result value into a input field

Name : <input type="text" id="abc">

Thanks for answering :)

2
  • Are you listening to the deviceready event? And are there some datas in the database? Commented Jan 26, 2015 at 19:03
  • yes I did declare deviceready. and yes, the database and the data is exist Commented Jan 27, 2015 at 9:45

2 Answers 2

1

Try adding your search value outside of the SQL statement and refer to the item as an object, see below:

$(document).ready(function(){
    docDB = window.openDatabase("Test", "1.0", "Test Data", 50000);

    docDB.transaction(function (tx) {
        tx.executeSql("SELECT * FROM Test WHERE word=?", ['abc'], function (tx, results) {
            var dblen = results.rows.length;
            if( dblen>0 ){
                document.getElementById("abc").value = results.rows.item(0).stats;
                }
        }, null);
    });
});

Removing the search parameters from the actual SQL statement has a few different benefits. One is, by passing in the variables they are escaped correctly as to not break the statement. Another is, it makes the statement more clear when debugging code.

For those who may not be familiar with the syntax, you add ? where you would place variable input or a search parameter and then in the array after the statement [] items are removed from the array and added to the SQL statement.

Example:

..."SELECT * FROM thisTable WHERE id IN(?) AND name LIKE ? and gender=?", ['1,32,456', '%tim%', 'M']

Generates:

"SELECT * FROM thisTable WHERE id IN('1,32,456') AND name LIKE '%tim%' and gender='M'"
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Dawson, thankyou very much. I don't understand about this [ ] mean before
0

You need to reaffirm the assessment in your sql code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.