PhoneGap Web SQL查询没有带来结果

时间:2014-05-22 04:12:07

标签: cordova web-sql

我正在开发一个phonegap应用程序。我有以下代码:

function showSuper()
{

    openDB(function(db){
        db.transaction(function(tx){
            var q="Select * from Supermercado";

            tx.executeSql(q,[],renderSuper, function (){alert("Error executing SQL SELECT");});
        });     
    });
}

function renderSuper(tx,resuls){
    for (x=0;x<=results.length-1;x++)
    {
        var r=results.rows[x];
        alert(r);
        ht=ht+'<li><a href="page2.html" rel="external">';
        ht=ht+'<h1>'+ r.name+'</h1>;';
        ht=ht+'<img src="+r.logo+'"/>';
        ht=ht+'<p>Categor&iacute;a'+r.categoria+"</p>";
        ht=ht+'</a></li>';      
    }   
    $('#lstSuper').listview('refresh');
}

请注意,openDB是window.openDatabase()的快捷方式。当renderSuper()函数执行时,&#34;结果&#34;未定义。我没有得到它,我正在按照Phonegap的文档中给出的示例。谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

我很难在没有评论的情况下理解您的代码。

这是一些websql代码,它从websql数据库中选择高分,希望这有助于:)

var db;

try 
{   
    db = openDatabase('random game', '1.0', 'random game database', 2 * 10 * 10);
    console.log("LOCALDB - Database ready");
} catch (e) 
{
    // Error handling code goes here.
    if (e == INVALID_STATE_ERR) 
    {
        // Version number mismatch.
        alert("LOCALDB - Invalid database version.");
    }
    else 
    {
        alert("LOCALDB - Unknown error " + e + ".");
    }
}

function errCB(err, res) 
{
    console.log("LOCALDB - DB Error: " , err, res);
    console.log(JSON.stringify(res));

}

function createHighscoreTable(tx) 
{
    //tx.executeSql('DROP TABLE IF EXISTS HIGHSCORE'); //get rid of this when finish testing
    tx.executeSql('CREATE TABLE IF NOT EXISTS HIGHSCORE (id unique, highscore INT)');
    checkHighscoreValue();
}

function checkHighscoreValue(tx)
{
    db.transaction(function(tx) 
    {
        tx.executeSql('SELECT * FROM HIGHSCORE', [], checkHighscoreValueResults, errCB);
    });
}

function checkHighscoreValueResults(tx, results)
{
    var len = results.rows.length;
    if(len == 1)
    {
        var highscore = results.rows.item(0).highscore;
        console.log("LOCALDB - Current highscore is "+highscore);
        dbHighscore = highscore;
        calculateScore();
    }
    else
    {
        db.transaction(function(tx) 
        {
            //creates a default highscore of 0
            tx.executeSql('INSERT INTO HIGHSCORE (id, highscore) VALUES (1, 0)');
            console.log("LOCALDB - Default highscore added");
            dbHighscore = 0;
            calculateScore();
        });
    }
}