创建本地数据库web SQL

sqlite数据库:增删改

1) 创建sqlite

(创建的库名,版本号,描述,容量)

 var db = window.openDatabase('创建的库名.sdb', '1.0', '描述', 70000000);
2) 初始化表
 var initData = function () {
        db.transaction(function (tx) {//tx:开启一个事物对象,可以执行Sql脚本
            tx.executeSql(DROP TABLE IF EXISTS rec_archive_template_draft, [], function (tx, rs) {  			//判断这个里面是否有这个表,有的话删除
                tx.executeSql(
                        CREATE TABLE IF NOT EXISTS rec_archive_template_draft
                    (
                      '字段1'             INTEGER NULL,
                      '字段2'             TEXT    NULL,
                      '字段3'             INTEGER PRIMARY KEY AUTOINCREMENT,
                    ),
                    [],
                    function (tx, rs) {
                        // console.log('初始化并创建库完成',rs)
                    },
                    function (tx, error) {
                        // console.log(error)
                    }
                );
            }, function (tx, error) {
                console.log(error);
            });
        });
    };
3)sqlite中添加数据
var addData = function () {
    db.transaction(function (tx) {
        tx.executeSql(
                `INSERT INTO rec_archive_template_draft VALUES (?, ?, ?)`,
            [
                parentPId,
                11,
                null,
            ],
            function (tx, rs) {
                 // console.log('添加数据成功',rs)
            },
            function (tx, error) {
                // console.log(error);
            });

            //检索内容
            tx.executeSql('SELECT * FROM rec_archive_template_draft', [], function (tx, rs) {
                // console.log('数据库中现有数据:', rs);
            });
    });
          
};
4)sqlite查询数据
 var toggle = function () {
        db.transaction(function (tx) {
            tx.executeSql
            (
                    SELECT file_logic_name_base
                     FROM rec_archive_template_draft, [], function (tx, results) {
                    if (results.rows.length > 0) {
                    	console.log(results.rows)
                    }
                }
            )
        })
    };
5) 修改数据
var save = function () {
       db.transaction(function (tx) {
            tx.executeSql(SELECT last_insert_rowid()    //查询最新增加的一行记录  从哪个表中
                           FROM rec_archive_template_draft, null, function (tx, rs) {
                angular.forEach(rs.rows, function (value) {  //然后获取ID
                    angular.forEach(value, function (val) {
                        var id = val;
                        //sqlite中插入数据
                        tx.executeSql(
                            `UPDATE rec_archive_template_draft
                        SET 字段1 = ?, 字段2 = ?,
                          字段3 = ?
                        WHERE查询字段id = ?`,
                        [
                            字段1的值,
                            字段2的值,
                            字段3的值,
                            id,
                        ],
                        function (tx, rs) {
                            // console.log(rs);
                        },
                        function (tx, error) {
                            // console.log(error);
                        }
                    );

                });

            });
        });

    });
};
                        
6) 删除最新的一条数据
 $rootScope.del = function () {
        $rootScope.db.transaction(function (tx) {
            tx.executeSql
            (
                    SELECT last_insert_rowid()
                     FROM rec_archive_template_draft, null, function (tx, rs) {
                    angular.forEach(rs.rows[0], function (value, key) {
                        var id = value;
                        tx.executeSql(
                                DELETE FROM rec_archive_template_draft
                            WHERE id_file_logic = ?, [id], function (tx, res) {
                            }, function (tx, err) {
                                console.log(err)
                            }
                        )
                  });
            }
        )

    });
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值