第十七课--H5Web SQL 数据库

<span id="status"></span>
<script>
    var e_id = 3;
    var e_log = 'blog';
    创建数据库
    var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
    创建表
    db.transaction(function (tx) {
       tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
    });
    添加
    db.transaction(function (tx) {
       tx.executeSql('INSERT INTO LOGS (id, log) VALUES (4, "同学")');
       tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "www.123.com")');
    });
    添加
    db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
        tx.executeSql('INSERT INTO LOGS (id,log) VALUES (?, ?)', [e_id, e_log]);
    });
    查
    db.transaction(function (tx) {
        tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
        var len = results.rows.length, i;
        msg = "<p>查询记录条数: " + len + "</p>";
        document.querySelector('#status').innerHTML +=  msg;

        for (i = 0; i < len; i++){
           alert(results.rows.item(i).log );
        }

        }, null);

    });
    删除
    db.transaction(function (tx) {
        tx.executeSql('DELETE FROM LOGS  WHERE id=1');
    });
    db.transaction(function(tx) {
        tx.executeSql('DELETE FROM LOGS WHERE id=?', [id]);
    });
    更新
    db.transaction(function (tx) {
        tx.executeSql('UPDATE LOGS SET log=\'www.w3cschool.cc\' WHERE id=2');
    });
    db.transaction(function(tx) {
        tx.executeSql('UPDATE LOGS SET log=\'www.w3cschool.cc\' WHERE id=?', [id]);
    });
</script>
看案例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Web SQL 数据库</title>
</head>
<body>
<div id="status" name="status">状态信息</div>
 <script type="text/javascript">

         var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
         var msg;

         db.transaction(function (tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
            tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "同学")');
            tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "www.123.com")');
            msg = '<p>数据表已创建,且插入了两条数据。</p>';
            document.querySelector('#status').innerHTML =  msg;
         });

         db.transaction(function (tx) {
            tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
               var len = results.rows.length, i;
               msg = "<p>查询记录条数: " + len + "</p>";
               document.querySelector('#status').innerHTML +=  msg;

               for (i = 0; i < len; i++){
                  msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
                  document.querySelector('#status').innerHTML +=  msg;
               }
            }, null);
         });

      </script>

</body>
</html>

 

上一篇:python-open文件处理


下一篇:jni中arm64-v8a,armeabi-v7a,armeabi文件夹的意义和用法<转>