Question

I am working with a opendatabase with phonegap and everything works fine in Chrome browser on my desktop but when I run it on my android device and click to the button that call insertRecord() it says not with eclipse I get error

10-26 10:37:13.191: E/Web Console(12134): Uncaught ReferenceError: insertRecord is not defined at file:///android_asset/www/index.html:137

My code look like this

document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {


var minChange;
var pumpBore;
var sInches;
var sMin;
var maxEfficiency;
var field;
var percent;
var calcSlide;
var results;
var id;
var listing;
var slider_value;
var getMin;

var dateDay = "Wed";
var dateNum = "22";
var date = new Date();
var dateDay = date.getDay();
var dateNum = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var dateTime = hour + ":" + min;

pumpBore = $("#pumpBore").val();
sInches = $("#sInches").val();
sMin = $("#sMin").val();
calcSlide = $("#calcSlide").val();
field = $('#field').val();
results = $('#results');
id = $('#id');
getMin = $('#slider-1').val();

// FUNCTION TO CALCULATE AND DISPLAY PRODUCTION
function calculate(sMin) {
    if (sMin == null)
        sMin = sMin

    pumpBore = $("#pumpBore").val();
    sInches = $("#sInches").val();
    calcSlide = $("#calcSlide").val();
    field = $('#field').val();

    console.log("Calc pump Bore: " + pumpBore);
    console.log("Calc Inches: " + sInches);
    console.log("Calc Min: " + sMin);
    console.log("Field Amount: " + field);
    console.log("Percentage: " + percent);
    console.log('Time: ' + dateTime);
    maxEfficiency = Math.round(parseFloat(pumpBore) * parseFloat(sInches)
            * parseFloat(sMin));

    console.log("Max Efficiency " + maxEfficiency);
    // console.log(calcSlide);
    $("#sMin").val(sMin);
    $("#barrels").val(maxEfficiency);

    percent = Math.round(parseFloat(field) / parseFloat(maxEfficiency) * 100);

    $('#percent').val(percent + "%");

    console.log("========================================");
    sMin = $('#sMin').val();
} // eof calculate

function consoleList() {
    console.log('============================');
    console.log("pump Bore: " + pumpBore);
    console.log("Stroke Inches: " + sInches);
    console.log("Strokes Per Minute: " + $('#sMin').val());
    console.log("Max Efficiancy: " + maxEfficiency);
    console.log("Field: " + field);
    console.log("Field %: " + percent);
    console.log('Time: ' + dateTime);
    console.log('Slider Value: ' + slider_value);
    console.log('============================');
    console.log(results);

}

var strokeMin = $('#sMin').val();
var createStatement = "CREATE TABLE IF NOT EXISTS Notes (id INTEGER PRIMARY KEY AUTOINCREMENT, dateDay TEXT, dateNum TEXT, dateTime TEXT, pumpBore TEXT, sInches TEXT, sMin TEXT, maxEfficiency TEXT, field TEXT, prodPercent TEXT)";
var selectAllStatement = "SELECT * FROM Notes ORDER BY id DESC";
var insertStatement = "INSERT INTO Notes (dateDay, dateNum, dateTime, pumpBore, sInches, sMin, maxEfficiency, field, prodPercent) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
var deleteStatement = "DELETE FROM Notes WHERE id=?";
var dropStatement = "DROP TABLE Notes";
var insertValues = "[pumpBore.value, sInches.value, sMin.value, maxEfficiency.value, field.value, percent.value]";
var testInsertValues = '["123", "234", "567", "657", "753", "2654"]';

var db = openDatabase("noteList", "1.0", "Note List", 500000);
var dataset;
createTable();

function onError(tx, error) {
    alert(error.message);
}

function showRecords() {
    results.html('');
    db.transaction(function(tx) {
        tx.executeSql(selectAllStatement, [], function(tx, result) {
            dataset = result.rows;
            for ( var i = 0, item = null; i < dataset.length; i++) {
                item = dataset.item(i);

                listing = '<div class="noteContainer">'
                        + '<div class="dateContainer">'
                        + '<span id="weekDay" class="">'
                        + item['dateDay']
                        + '</span>'
                        + '<span id="date" class="">'
                        + item['dateNum']
                        + '</span>'
                        + '<span id="time" class="">'
                        + item['dateTime']
                        + '</span>'
                        + '</div><!-- eof dateContainer -->'
                        + '<div class="specContainer">'
                        + '<span class="">pump Bore: '
                        + item['pumpBore']
                        + '</span>'
                        + '<span class="">Stroke Length: '
                        + item['sInches']
                        + '</span>'
                        + '<span class="">Stroke/Min: '
                        + item['sMin']
                        + '</span>'
                        + '<span class="">Barrels: '
                        + item['maxEfficiency']
                        + '</span>'
                        + '<span class="">Field Prod: '
                        + item['field']
                        + '</span>'
                        + '<span class="">Production %: '
                        + item['prodPercent']
                        + '</span>'
                        + '</div><!-- eof specContainer -->'
                        + '<div class="mailButton">'
                        + '<img src="images/mail_Button.png" alt="mail" />'
                        + '</div><!-- eof mail -->'
                        + '<div id="" style="clear:both"></div>'
                        + '<a href="#" onclick="deleteRecord('
                        + item['id']
                        + ')">delete</a></li>';
                +'</div><!-- eof noteContainer -->';
                console.log(i);
                results.append(listing);
            }
        });
    });
}

function createTable() {
    db.transaction(function(tx) {
        tx.executeSql(createStatement, [], showRecords, onError);
    });
}

function insertRecord() {
    db.transaction(function(tx) {
        // consoleList();
        console.log("The field:" + field);
        tx.executeSql(insertStatement, [ dateDay, dateNum, dateTime, pumpBore, sInches, getMin,
                maxEfficiency, field, percent ], loadAndReset, onError);
        // ("Row Inserted!");
    });
}

function loadRecord(i) {
    var item = dataset.item(i);
    dateDay.value = item['dateDay'];
    dateNum.value = item['dateNum'];
    dateTime.value = item['dateTime'];
    pumpBore.value = item['pumpBore'];
    sInches.value = item['sInches'];
    sMin.value = item['sMin'];
    maxEfficiency.value = item['maxEfficiency'];
    field.value = item['field'];
    percent.value = item['prodPercent'];
    id.value = item['id'];
}

function updateRecord() {
    db.transaction(function(tx) {
        tx.executeSql(updateStatement, [ dateDay.value, dateNum.value,
                dateTime.value, pumpBore.value, sInches.value, sMin.value,
                maxEfficiency.value, field.value, percent.value ],
                loadAndReset, onError);
    });
}

function deleteRecord(id) {
    db.transaction(function(tx) {
        tx.executeSql(deleteStatement, [ id ], showRecords, onError);
    });
    resetForm();
}

function dropTable() {
    db.transaction(function(tx) {
        tx.executeSql(dropStatement, [], showRecords, onError);
    });
    resetForm();
}

function loadAndReset() {
    // alert(listing);
    resetForm();
    showRecords();
}

function resetForm() {
    dateDay.value = '';
    dateNum.value = '';
    dateTime.value = '';
    pumpBore.value = '';
    sInches.value = '';
    sMin.value = '';
    field.value = '';
    percent.value = '';
    id.value = '';
}

$(function() {
    $('#slider-1').live('change', function(){
        slider_value = $(this).val()
        console.log(slider_value)
        minChange = slider_value;
        // minChange = 100 - minChange;
        minChange = Math.round(minChange);
        console.log("Min Change " + minChange);

        calculate(minChange);
        // do whatever you want with that value...
    })
});

// / Knob Turn Function
$(function() {

    $('#dialBG, #dialBG2 ').knobKnob({
        snap : 1,
        value : -10,
        turn : function(ratio) {
            console.log(ratio);
            //minChange = (ratio * 100);
            // minChange = 100 - minChange;
            //minChange = Math.round(minChange);
            //console.log("Min Change " + minChange);

            //calculate(minChange);
        }
    });

});

}

I have a link to it as kzoomarketing.com/apps/oilapp/121/ so it can be seen. On the calculator page the save button in the upper right records all the fields and populates them in the note page. It works fine in the web browser.

If I change the js functions be wrapped in ondeviceready and run it I get the log cat error mentioned above.

Can anyone help me get this to run on the android device.

Was it helpful?

Solution

The insertRecord function is defined inside the onDeviceReady function.

You have to reorganize your code so that event handlers like this are accessible globally (i.e., from HTML).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top