使用 Web 数据库时出现 Phonegap 错误:“表达式 mydb.transaction 的结果不是函数”

发布于 2024-12-06 07:56:09 字数 5088 浏览 0 评论 0原文

我第一次尝试在 iOS 应用程序中使用 html5 的网络数据库,并使用phonegap。但我陷入了这个错误,它说“表达式 mybd.transaction 的结果不是函数”

如果我使用警报进行检查,initDB 正在执行,但是当涉及到 createTables 函数时,会出现上述错误,我对此无能为力其上。

我已经使用了这个实现 -> http://wiki.phonegap.com/w/page/16494756/Adding%20SQL%20Database%20support%20to%20your%20iPhone%20App

<script type="text/javascript">
    function validateFloat()
    {
        var mydb=false;
        var fuelUnits = document.myForm.UnitsOfFuel;
        var bFuelUnits = false;
        var bUnitPrice = false;
        switch (isNumeric(fuelUnits.value))
        {
            case true:
            bFuelUnits = true;
            fuelUnits.style.background="white";
            break;
            case false:
            fuelUnits.focus();
            fuelUnits.style.background="yellow";
            break;
        }
        var unitPrice = document.myForm.PricePerUnit;
        switch (isNumeric(unitPrice.value))
        {
            case true:
            bUnitPrice = true;
            unitPrice.style.background="white";
            break;
            case false:
            unitPrice.focus();
            unitPrice.style.background="yellow";
            break;
        }
        if(bFuelUnits && bUnitPrice)
        {
            if(initDB(mydb))
            {
                if(createTables(mydb))
                {   
                    loadCelebs();
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
        else
        {
            return false;
        }
    }
    function isNumeric(n)
    {
        var n2 = n;
        n = parseFloat(n);
        return (n!='NaN' && n2==n);
    }

    // initialise the database

    function initDB(mydb) 
    {
        try 
        { 
            if (!window.openDatabase) 
            { 
                alert('not supported'); 
                return false;
            } 
            else 
            { 
                var shortName = 'phonegap'; 
                var version = '1.0'; 
                var displayName = 'PhoneGap Test Database'; 
                var maxSize = 65536; // in bytes 
                mydb = openDatabase(shortName, version, displayName, maxSize);
                alert("initDB");
                return true;
            }
        } 
        catch(e) 
        { 
            // Error handling code goes here. 
            if (e == INVALID_STATE_ERR) 
            { 
                // Version number mismatch. 
                alert("Invalid database version."); 
                return false;
            } 
            else 
            { 
                alert("Unknown error "+e+"."); 
                return false;
            } 
            return true; 
        } 
    }

    // db error handler - prevents the rest of the transaction going ahead on failure
    function errorHandler(transaction, error) 
    { 
        alert("errorHandler");
        // returns true to rollback the transaction
        return true;  
    }

    // null db data handler
    function nullDataHandler(transaction, results) 
    { 

    } 

    // create tables for the database
    function createTables(mydb) 
    {

        try 
        {
            mydb.transaction(

                             function(tx) {

                             tx.executeSql('CREATE TABLE celebs(id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL DEFAULT "");', [], nullDataHandler(tx,results), errorHandler(tx,error)); 

                             tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler(tx,results), errorHandler(tx,error)); 

                             tx.executeSql('insert into celebs (id,name) VALUES (2,"Keira Knightley");', [], nullDataHandler(tx,results), errorHandler(tx,error)); 

                             });
            alert("createTables");
            return true;
        } 
        catch(e) 
        {
            alert(e.message);
            return false;
        }
    }

    // load the currently selected icons
    function loadCelebs()
    {
        try 
        {
            mydb.transaction(
                function(tx) {
                tx.executeSql('SELECT * FROM celebs ORDER BY name',[], celebsDataHandler(tx,results), errorHandler(tx,error));
                });
        } 
        catch(e) 
        {
            alert(e.message);
        }
    } 


    // callback function to retrieve the data from the prefs table
    function celebsDataHandler(transaction, results) 
    {
        alert("here also?");
        // Handle the results 
        var html = "<ul>"; 
        for (var i=0; i<results.rows.length; i++) 
        { 
            var row = results.rows.item(i); 
            html += "<li>"+row['name']+"</li>\n";
        } 
        html +="</ul>";
        alert(html);
    }

</script>

I'm trying to use html5's web-database using phonegap for an iOS app for the first time. But I'm stuck at this error which says "result of expression mybd.transaction is not a function"

If I check using alerts, initDB is getting executed but when it comes to createTables function, the above error rises and I'm helpless from thereon.

I've used this implementation -> http://wiki.phonegap.com/w/page/16494756/Adding%20SQL%20Database%20support%20to%20your%20iPhone%20App

<script type="text/javascript">
    function validateFloat()
    {
        var mydb=false;
        var fuelUnits = document.myForm.UnitsOfFuel;
        var bFuelUnits = false;
        var bUnitPrice = false;
        switch (isNumeric(fuelUnits.value))
        {
            case true:
            bFuelUnits = true;
            fuelUnits.style.background="white";
            break;
            case false:
            fuelUnits.focus();
            fuelUnits.style.background="yellow";
            break;
        }
        var unitPrice = document.myForm.PricePerUnit;
        switch (isNumeric(unitPrice.value))
        {
            case true:
            bUnitPrice = true;
            unitPrice.style.background="white";
            break;
            case false:
            unitPrice.focus();
            unitPrice.style.background="yellow";
            break;
        }
        if(bFuelUnits && bUnitPrice)
        {
            if(initDB(mydb))
            {
                if(createTables(mydb))
                {   
                    loadCelebs();
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
        else
        {
            return false;
        }
    }
    function isNumeric(n)
    {
        var n2 = n;
        n = parseFloat(n);
        return (n!='NaN' && n2==n);
    }

    // initialise the database

    function initDB(mydb) 
    {
        try 
        { 
            if (!window.openDatabase) 
            { 
                alert('not supported'); 
                return false;
            } 
            else 
            { 
                var shortName = 'phonegap'; 
                var version = '1.0'; 
                var displayName = 'PhoneGap Test Database'; 
                var maxSize = 65536; // in bytes 
                mydb = openDatabase(shortName, version, displayName, maxSize);
                alert("initDB");
                return true;
            }
        } 
        catch(e) 
        { 
            // Error handling code goes here. 
            if (e == INVALID_STATE_ERR) 
            { 
                // Version number mismatch. 
                alert("Invalid database version."); 
                return false;
            } 
            else 
            { 
                alert("Unknown error "+e+"."); 
                return false;
            } 
            return true; 
        } 
    }

    // db error handler - prevents the rest of the transaction going ahead on failure
    function errorHandler(transaction, error) 
    { 
        alert("errorHandler");
        // returns true to rollback the transaction
        return true;  
    }

    // null db data handler
    function nullDataHandler(transaction, results) 
    { 

    } 

    // create tables for the database
    function createTables(mydb) 
    {

        try 
        {
            mydb.transaction(

                             function(tx) {

                             tx.executeSql('CREATE TABLE celebs(id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL DEFAULT "");', [], nullDataHandler(tx,results), errorHandler(tx,error)); 

                             tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler(tx,results), errorHandler(tx,error)); 

                             tx.executeSql('insert into celebs (id,name) VALUES (2,"Keira Knightley");', [], nullDataHandler(tx,results), errorHandler(tx,error)); 

                             });
            alert("createTables");
            return true;
        } 
        catch(e) 
        {
            alert(e.message);
            return false;
        }
    }

    // load the currently selected icons
    function loadCelebs()
    {
        try 
        {
            mydb.transaction(
                function(tx) {
                tx.executeSql('SELECT * FROM celebs ORDER BY name',[], celebsDataHandler(tx,results), errorHandler(tx,error));
                });
        } 
        catch(e) 
        {
            alert(e.message);
        }
    } 


    // callback function to retrieve the data from the prefs table
    function celebsDataHandler(transaction, results) 
    {
        alert("here also?");
        // Handle the results 
        var html = "<ul>"; 
        for (var i=0; i<results.rows.length; i++) 
        { 
            var row = results.rows.item(i); 
            html += "<li>"+row['name']+"</li>\n";
        } 
        html +="</ul>";
        alert(html);
    }

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-12-13 07:56:09

您需要返回在 initDB() 函数中创建的新创建的 mydb 实例,然后使用返回的实例。

如果您要为传递给函数的参数重新分配新值(这就是您正在做的事情),则需要返回该值,否则更改将丢失。

请注意,如果您将对象传递给函数(您没有这样做),则可以修改该对象的属性,并且这些更改将保留在该函数的范围之外。

function initDB(mydb) {
    mydb.initialized = true;
}
mydb.initialized = false;
initDB(mydb);
// mydb.initialized => true

vs...

function initDB(mydb) {
    mydb = new DB(); // new reference
    mydb.initialized = true;
}
mydb.initialized = false;
initDB(mydb);
// mydb.initialized => false

当然,你传入的也是一个原始布尔值,而不是一个对象。基元按值传递,因此您必须返回新创建的 mydb


更新

您还错误地使用了传入的事务处理程序。再次查看 Phone Gap Wiki,了解他们如何将函数引用分配给变量并将这些引用传递到事务方法中。就像现在一样,您正在调用函数而不是传递它们。

所以,不要这样做(你现在正在做的事情):

function errorHandler(tx, error) {
    alert("error");
    return true;  
}
function nullDataHandler(tx, results) { }

tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler(tx, results), errorHandler(tx, error));

这样做:

var errorHandler = function (tx, error) {
    alert("error");
    return true;  
}
var nullDataHandler = function(tx, results) { }

tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler, errorHandler);

我希望这能解决问题。另外,请记住,如果这回答了您的问题,请投票并将其标记为答案,以供未来访问者参考。

You need to return the newly created mydb instance that is created within the initDB() function and then use the returned instance.

If you are reassigning a new value to a parameter that is passed into a function (which is what you are doing), it needs to be returned or the changes will be lost.

Note that if you are passing in an object to the function (which you are not doing), you can modify properties of that object and those changes will be persisted outside the scope of that function.

function initDB(mydb) {
    mydb.initialized = true;
}
mydb.initialized = false;
initDB(mydb);
// mydb.initialized => true

vs...

function initDB(mydb) {
    mydb = new DB(); // new reference
    mydb.initialized = true;
}
mydb.initialized = false;
initDB(mydb);
// mydb.initialized => false

Of course, you are also passing in a primitive boolean value, not an object. Primitives are passed by value so you must return the newly created mydb.


UPDATE

You are also using your passed-in transaction handlers wrong. Look at the phone gap wiki again to see how they are assigning the function references to variables and passing those references into the transaction methods. As it is now, you are calling the functions instead of passing them.

So, instead of this (what you are doing now):

function errorHandler(tx, error) {
    alert("error");
    return true;  
}
function nullDataHandler(tx, results) { }

tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler(tx, results), errorHandler(tx, error));

do this:

var errorHandler = function (tx, error) {
    alert("error");
    return true;  
}
var nullDataHandler = function(tx, results) { }

tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler, errorHandler);

I hope this clears it up. Also, remember, if this answered your question, upvote it and mark it as the answer for a reference to future visitors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文