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)
}
)
});
}
)
});
};