webSQL使用

1.创建数据库

var db = openDatabase("trial", "1.0", "用户表", 1024 * 1024);

2.插入数据

    db.transaction(function (context) {
        context.executeSql(CREATE TABLE IF NOT EXISTS users (infoid ,infoname,grade,parent,type,value));
        context.executeSql(INSERT INTO users (infoid ,infoname,grade,parent,type,value) VALUES ("history", "履历","1","info","array",""));
        context.executeSql(INSERT INTO users (infoid ,infoname,grade,parent,type,value) VALUES ("grade", "资格","1","info","obj",""));
        context.executeSql(INSERT INTO users (infoid ,infoname,grade,parent,type,value) VALUES ("japanese", "日语","2","grade","int","1,2,3,4"));
        context.executeSql(INSERT INTO users (infoid ,infoname,grade,parent,type,value) VALUES ("english", "英文","2","grade","text",""));
    });

3.更新

    db.transaction( function(context) {
        context.executeSql(UPDATE users SET infoname=? where infoid=?, ["资格2","grade"]);
    });

## 4.删除

    db.transaction( function(context) {
        context.executeSql(DELETE from users where infoid=?, ["grade"]);
    });

## 5.删除表

    db.transaction(function (context) {
        context.executeSql(DROP TABLE users);
    });

## 6.生成语句

    sql =DELETE from users where infoid="history";
    sql =INSERT INTO users (infoid ,infoname,grade,parent,type,value) VALUES ("history", "履历","1","info","array","")

    db.transaction( function(context) {
        context.executeSql(sql);
    });

7.查询

    sql=SELECT * FROM users WHERE grade="2";
    obj=[];
    db.transaction( function(context) {
        context.executeSql(
            sql, [],
             function(context, result) {
                var rows = result.rows, length = rows.length, i=0;
                for(i; i < length; i  ) {
                    obj.push(rows.item(i));
                }
            }
        );
    });

来源张永光的博客

你可能感兴趣的:(js)