zoukankan      html  css  js  c++  java
  • H5本地数据库使用实例

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>

    <script type="text/javascript">
    var db = openDatabase("myDataBase", "1.0", "myLocalMsg", 1024 * 1024);
    /*
    db.transaction(function(tx){
    tx.executeSql('drop table myLocalMsg');
    });
    */
    var myNowUserId = '21538'
    var myCreateData = [
    {msgId:1,isRead:0,createTime:'2019-01-01 00:00:00'},
    {msgId:2,isRead:0,createTime:'2019-01-02 00:00:00'},
    {msgId:3,isRead:0,createTime:'2019-01-03 00:00:00'},
    {msgId:4,isRead:0,createTime:'2019-01-04 00:00:00'},
    {msgId:5,isRead:0,createTime:'2019-01-05 00:00:00'},
    {msgId:6,isRead:0,createTime:'2019-01-06 00:00:00'},
    {msgId:7,isRead:0,createTime:'2019-01-07 00:00:00'},
    {msgId:9,isRead:0,createTime:'2019-01-08 00:00:00'}
    ]

    db.transaction(function(tx){
    tx.executeSql('SELECT * FROM myLocalMsg where userId=? ',
    [myNowUserId],
    function(transaction,results){
    console.log("成功查询信息打印==================================")
    myLocalMsgData = results.rows
    console.log(myLocalMsgData)
    console.log(myCreateData)
    if( myLocalMsgData.length > 0 ){
    for( var i = 0 ; i < myCreateData.length ; i++ ){ //循环判断该条信息是否应该新增
    var isAddMsg = 1
    for( var j = 0 ; j < myLocalMsgData.length ; j++ ){
    if( myCreateData[i].msgId == myLocalMsgData[j].msgId ){
    isAddMsg = 0
    }
    }
    if( isAddMsg == 1 ){
    tx.executeSql('INSERT INTO myLocalMsg (userId,msgId,isRead,createTime) VALUES (?,?,?,?)',
    [myNowUserId,myCreateData[i].msgId,myCreateData[i].isRead,myCreateData[i].createTime],
    function(transaction,results){
    console.log("插入数据成功")
    console.log(results)
    },
    function(transaction,errMsg){
    console.log("插入数据失败信息打印:")
    console.log(errMsg)
    }
    )
    }

    }
    for( var i = 0 ; i < myLocalMsgData.length ; i++ ){ //循环判断该条信息是否应该删除
    var isDeleteMsg = 1
    for( var j = 0 ; j < myCreateData.length ; j++ ){
    if( myLocalMsgData[i].msgId == myCreateData[j].msgId ){
    isDeleteMsg = 0
    }
    }
    if( isDeleteMsg == 1 ){ //删除
    console.log(myLocalMsgData[i].msgId)
    console.log(myNowUserId)
    tx.executeSql("DELETE FROM myLocalMsg WHERE msgId= ? and userId = ?",
    [myLocalMsgData[i].msgId,myNowUserId],
    function(transaction,results) {
    console.log("删除成功")
    console.log(results)
    },
    function (transaction,errMsg) {
    console.log("删除失败")
    console.log(errMsg)
    }
    )
    }
    }
    }else{
    for(var i = 0 ; i < myCreateData.length ; i++ ){
    tx.executeSql('INSERT INTO myLocalMsg (userId,msgId,isRead,createTime) VALUES (?,?,?,?)',
    [myNowUserId,myCreateData[i].msgId,myCreateData[i].isRead,myCreateData[i].createTime],
    function(transaction,results){
    console.log("插入数据成功")
    console.log(results)
    },
    function(transaction,errMsg){
    console.log("插入数据失败信息打印:")
    console.log(errMsg)
    }
    )
    }
    }
    },
    function(transaction,errMsg){
    console.log("查询失败开始创建表格")
    tx.executeSql('CREATE TABLE myLocalMsg(userId int,msgId int,isRead int,createTime date)',
    [],
    function(transaction,results){
    console.log("创建表格成功")
    for(var i = 0 ; i < myCreateData.length ; i++ ){
    tx.executeSql('INSERT INTO myLocalMsg (userId,msgId,isRead,createTime) VALUES (?,?,?,?)',
    [myNowUserId,myCreateData[i].msgId,myCreateData[i].isRead,myCreateData[i].createTime],
    function(transaction,results){
    console.log("插入数据成功")
    console.log(results)
    },
    function(transaction,errMsg){
    console.log("插入数据失败信息打印:")
    console.log(errMsg)
    }
    )
    }
    },
    function(transaction,errMsg){
    console.log("创建表格失败信息打印:")
    console.log(errMsg)
    }
    )
    })
    })


    </script>


    </body>
    </html>

  • 相关阅读:
    target runtime apache v6.0 not defined解决
    java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
    The valid characters are defined in RFC 7230 and RFC 3986问题
    invalid END header解决方法
    You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed问题解决
    Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    在eclipse中import java web项目时遇到的一些问题并将该项目通过tomcat发布
    java byte转string 涉及到字节流中有中文
    spring+mybatis框架搭建时遇到Mapped Statements collection does not contain value for...的错误
    试试看读一下Zepto源码
  • 原文地址:https://www.cnblogs.com/haonantong/p/10728535.html
Copyright © 2011-2022 走看看