美文网首页
html5本地数据库操作

html5本地数据库操作

作者: apple_sun | 来源:发表于2018-03-26 12:02 被阅读0次
    var myWebDatabase = openDatabase('UserInformation', '1.0', 'user information', 3 * 1024 * 1024); //数据库名,版本号,对数据库的描述,数据库的大小
    var sql = "CREATE TABLE stu (id,name,age,sex)";
    myWebDatabase.transaction(function(tx) {
        tx.executeSql(sql, [], function(tx, result) { //新建表
                console.log('创建学生表成功');
            },
            function(tx, error) {
                console.log('创建学生表失败:' + error.message);
            });
        tx.executeSql('DROP TABLE IF EXISTS stu', [], function(tx, result) {
                console.log('删除学生表成功');
            },
            function(tx, error) {
                console.log('删除学生表失败:' + error.message);
            });
        var stu_id = '1';
        var stu_name = '张三';
        var stu_age = '14';
        tx.executeSql("INSERT INTO stu (id,name,age,sex) values(?,?,?,?)", [stu_id, stu_name, stu_age, "男"], function() { //新增数据
                console.log('数据添加成功!');
            },
            function(tx, error) {
                console.log('添加数据失败: ' + error.message);
            });
        tx.executeSql("update stu set name = '" + stu_name + "',age = '" + stu_age + "',sex = '男' where id= '" + stu_id + "'", [], function(tx, result) {
                console.log("数据更新成功")
            },
            function(tx, error) {
                console.log('更新失败: ' + error.message);
            });
        var sum = '1';
        tx.executeSql("select * from stu where id = '" + sum + "'", [], function(tx, result) {
                for(var i = 0; i < result.rows.length; i++) { //对查询出的数据进行处理
                    var row = result.rows.item(i);
                    var stu_name = document.getElementById("stu_name"); //学生姓名
                    stu_name.innerHTML = row.name;
                }
                console.log("根据id" + sum + "查询出的数据条数" + result.rows.length);
            },
            function(tx, error) {
                console.log('查询失败: ' + error.message);
            });
        var select_sql = "select * from stu where (age = '" + arr1[0] + "' or age = '" + arr1[1] + "' or age = '" + arr1[2] + "' or age = '" + arr1[3] + "' or age = '" + arr1[4] + "' or age = '" + arr1[5] + "' or Schedule_month = '" + arr1[6] + "' ) and sex = '男'"; //多条件查询
        tx.executeSql(select_sql, [], function(tx, result) {
                for(var i = 0; i < result.rows.length; i++) { //对查询出的数据进行处理
    
                }
                console.log("根据id" + sum + "查询出的数据条数" + result.rows.length);
            },
            function(tx, error) {
                console.log('查询失败: ' + error.message);
            });
    })
    

    相关文章

      网友评论

          本文标题:html5本地数据库操作

          本文链接:https://www.haomeiwen.com/subject/ihalcftx.html