将 Web sql 结果显示为 html
  • ;
  • 发布于 2024-11-09 00:56:08 字数 1662 浏览 0 评论 0原文

    我已经使用 sqlite 创建了一个 Web sql,以使用 javascript 创建数据库,我希望将结果显示到列表项标记内的另一个 html 页面上。我怎样才能实现这个目标?我在开发移动应用程序时使用 jquery 和phonegap。

        // Populate the database 
    //
    function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }
    
    // Query the database
    //
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }
    
    // Query the success callback
    //
    function querySuccess(tx, results) {
        var len = results.rows.length;
        console.log("DEMO table: " + len + " rows found.");
        for (var i=0; i<len; i++){
            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " +      results.rows.item(i).data); 
    //the data from here to the html page.
        }
    }
    
    // Transaction error callback
    //
    function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
    }
    
    // Transaction success callback
    //
    function successCB() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(queryDB, errorCB);
    }
    

    html页面:

    <div data-role="content" class="ui-content" role="main">
    <div id="wrapper" >
        <div id="scroller" >
            <ul data-role="listview" data-theme="c" class="ui-btn-inset">
    
                <li>would like to put it inside here</li>
            </ul>
        </div>
    </div>
    

    是否可以包含一个链接?还需要 Ajax 吗?

    谢谢。

    I have created a web sql using sqlite to create a database using javascript and I want the results to be displayed onto another html page inside the list items tag. How can I achieve this? I am using jquery and phonegap as I am developing a mobile app.

        // Populate the database 
    //
    function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }
    
    // Query the database
    //
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }
    
    // Query the success callback
    //
    function querySuccess(tx, results) {
        var len = results.rows.length;
        console.log("DEMO table: " + len + " rows found.");
        for (var i=0; i<len; i++){
            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " +      results.rows.item(i).data); 
    //the data from here to the html page.
        }
    }
    
    // Transaction error callback
    //
    function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
    }
    
    // Transaction success callback
    //
    function successCB() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(queryDB, errorCB);
    }
    

    html page:

    <div data-role="content" class="ui-content" role="main">
    <div id="wrapper" >
        <div id="scroller" >
            <ul data-role="listview" data-theme="c" class="ui-btn-inset">
    
                <li>would like to put it inside here</li>
            </ul>
        </div>
    </div>
    

    Is it possible to include a link with it? Also is Ajax needed for this?

    Thanks.

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

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

    发布评论

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

    评论(2

    遥远的绿洲 2024-11-16 00:56:08
    var ul = document.getElementById("scroller").getElementsByTagName("UL")[0];
    for (var i=0; i<len; i++){
        var str = "Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " +      results.rows.item(i).data;
        str = str.replace(/>/g, ">").replace(/</g, "<").replace(/'/g, "'").replace(/"/g, """); // entity encode
        ul.innerHTML += "<li>" + str + "</li>";
    }
    

    这是一个快速解决方案。您只需将 LI 标签附加到 UL 的内部 HTML 中即可。

    var ul = document.getElementById("scroller").getElementsByTagName("UL")[0];
    for (var i=0; i<len; i++){
        var str = "Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " +      results.rows.item(i).data;
        str = str.replace(/>/g, ">").replace(/</g, "<").replace(/'/g, "'").replace(/"/g, """); // entity encode
        ul.innerHTML += "<li>" + str + "</li>";
    }
    

    Here's a quick solution. You just append LI tags to the inner HTML of the UL.

    缺⑴份安定 2024-11-16 00:56:08

    这是一个简化的示例:

    //Replace the title with the first row
    document.title = ("Row = " + 0 + " ID = " + results.rows.item(0).id + " Data =  " +      results.rows.item(0).data);
    
    //Append the rest of the rows to the title
    for (var i=1; i<len; i++)
      {
      document.title += ("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " +      results.rows.item(i).data); 
      }
    
    //Copy the result to the specified element
    document.querySelector("#scroller li").innerHTML("<pre>"+document.title+"</pre>");
    

    Here is a simplified example:

    //Replace the title with the first row
    document.title = ("Row = " + 0 + " ID = " + results.rows.item(0).id + " Data =  " +      results.rows.item(0).data);
    
    //Append the rest of the rows to the title
    for (var i=1; i<len; i++)
      {
      document.title += ("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " +      results.rows.item(i).data); 
      }
    
    //Copy the result to the specified element
    document.querySelector("#scroller li").innerHTML("<pre>"+document.title+"</pre>");
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文