EWG01PDemo/service/db.ts

300 lines
8.6 KiB
TypeScript
Raw Normal View History

2025-01-17 13:55:44 +08:00
export default {
dbName: 'shuto', // 数据库名称
dbPath: '_doc/shuto.db', // 数据库地址,推荐以下划线为开头
// 判断数据库是否打开
isOpen() {
// 数据库打开了就返回 true,否则返回 false
return plus.sqlite.isOpenDatabase({
name: this.dbName, // 数据库名称
path: this.dbPath // 数据库地址
});
},
// 创建数据库 或 有该数据库就打开
open() {
return new Promise((resolve, reject) => {
// 打开数据库
plus.sqlite.openDatabase({
name: this.dbName,
path: this.dbPath,
success: (e) => {
console.log(`${this.dbPath} 已打开`);
resolve(e); // 成功回调
},
fail(e) {
console.error(`打开数据库错误:${e}`);
reject(e); // 失败回调
}
})
})
},
// 关闭数据库
close() {
return new Promise((resolve, reject) => {
plus.sqlite.closeDatabase({
name: this.dbName,
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
},
// 数据库建表 sql:'CREATE TABLE IF NOT EXISTS dbTable("id" varchar(50),"name" TEXT)
// 创建 CREATE TABLE IF NOT EXISTS 、 表名不能用数字开头
createTable(dbTable : string) {
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: this.dbName,
sql: [`CREATE TABLE IF NOT EXISTS ${dbTable}`],
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
},
// 数据库删表 sql:'DROP TABLE dbTable'
dropTable(dbTable : string) {
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: this.dbName,
sql: [`DROP TABLE ${dbTable}`],
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
},
// 向表格里添加数据 sql:'INSERT INTO dbTable VALUES('x','x','x')' 对应新增
// 或者 sql:'INSERT INTO dbTable ('x','x','x') VALUES('x','x','x')' 具体新增
// 插入 INSERT INTO 、 dbTable 是表名、根据表头列名插入列值
insertTableData(dbTable : string, data : any) {
// 判断有没有传参
if (dbTable !== undefined && data !== undefined) {
// 判断传的参是否有值
let bol = (JSON.stringify(data) == "{}");
if (!bol) {
let properties:string = '', values: string = '';
Object.keys(data).forEach(key => {
properties += `${key},`;
let v = data[key];
if (typeof v == 'string') {
values += `'${data[key]}',`;
}
else {
values += `${data[key]},`;
}
});
const plen = properties.length;
const vlen = values.length;
if (plen <= 0 || vlen <= 0) {
return Promise.reject(`数据有误:${properties} -- ${values}`);
}
properties = properties.substring(0, plen-1);
values = values.substring(0, vlen-1);
let sql = `INSERT INTO ${dbTable} (${properties}) VALUES(${values})`;
console.log(sql);
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: this.dbName,
sql: [sql],
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
} else {
return new Promise((reject) => { reject("错误添加") })
}
} else {
return new Promise((reject) => { reject("错误添加") })
}
},
// 根据条件向表格里添加数据 有数据更新、无数据插入
// (建表时需要设置主键) 例如 --- "roomid" varchar(50) PRIMARY KEY
insertOrReplaceData(dbTable : string, data : any, condition : any) {
// 判断有没有传参
if (dbTable !== undefined && data !== undefined) {
let sql: string;
if (condition == undefined) {
sql = `INSERT OR REPLACE INTO ${dbTable} VALUES('${data}')`;
} else {
sql = `INSERT OR REPLACE INTO ${dbTable} (${condition}) VALUES(${data})`;
}
// console.log(sql);
return new Promise((resolve, reject) => {
// 表格添加数据
plus.sqlite.executeSql({
name: this.dbName,
sql: [sql],
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
} else {
return new Promise((reject) => { reject("错误添加") })
}
},
// 查询获取数据库里的数据 sql:'SELECT * FROM dbTable WHERE lname = 'lvalue''
// 查询 SELECT * FROM 、 dbTable 是表名、 WHERE 查找条件 lname,lvalue 是查询条件的列名和列值
selectTableData(dbTable : string, uname : string, namevalue : any, upass : string, passvalue : any, urrn : string, rrnvalue : any) {
if (dbTable !== undefined) {
let sql: string;
// 第一个是表单名称,后两个参数是列表名,用来检索
if (uname !== undefined && upass !== undefined && urrn !== undefined) {
// 三个检索条件
sql = `SELECT * FROM ${dbTable} WHERE ${uname} = '${namevalue}' AND ${upass} = '${passvalue}' AND ${urrn}='${rrnvalue}'`;
}
if (uname !== undefined && upass !== undefined && urrn == undefined) {
// 两个检索条件
sql = `SELECT * FROM ${dbTable} WHERE ${uname} = '${namevalue}' AND ${upass} = '${passvalue}'`;
}
if (uname !== undefined && upass == undefined && urrn == undefined) {
// 一个检索条件
sql = `SELECT * FROM ${dbTable} WHERE ${uname} = '${namevalue}'`;
// console.log(sql);
}
if (uname == undefined) {
sql = `SELECT * FROM ${dbTable}`;
}
return new Promise((resolve, reject) => {
// 表格查询数据 执行查询的SQL语句
plus.sqlite.selectSql({
name: this.dbName,
sql: sql,
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
} else {
return new Promise((reject) => { reject("错误查询") });
}
},
// 删除表里的数据 sql:'DELETE FROM dbTable WHERE lname = 'lvalue''
// 删除 DELETE FROM 、 dbTable 是表名、 WHERE 查找条件 lname,lvalue 是查询条件的列名和列值
deleteTableData(dbTable : string, lname : string, lvalue : any, ww? : string, ee? : any) {
if (dbTable !== undefined) {
let sql: string;
if (lname == undefined) {
sql = `DELETE FROM ${dbTable}`;
} else {
if (ww !== undefined) {
// 两个检索条件
sql = `DELETE FROM ${dbTable} WHERE ${lname} = '${lvalue}' AND ${ww} = '${ee}'`;
} else {
// 一个检索条件
sql = `DELETE FROM ${dbTable} WHERE ${lname} = '${lvalue}'`;
}
}
return new Promise((resolve, reject) => {
// 删除表数据
plus.sqlite.executeSql({
name: this.dbName,
sql: [sql],
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
} else {
return new Promise((reject) => { reject("错误删除") });
}
},
// 修改数据表里的数据 sql:"UPDATE dbTable SET 列名 = '列值',列名 = '列值' WHERE lname = 'lvalue'"
// 修改 UPDATE 、 dbTable 是表名, data: 要修改的列名=修改后列值, lname,lvalue 是查询条件的列名和列值
updateTableData(dbTable : string, data : any, lname : string, lvalue : any) {
let sql: string;
if (lname == undefined) {
sql = `UPDATE ${dbTable} SET ${data}`;
} else {
sql = `UPDATE ${dbTable} SET ${data} WHERE ${lname} = '${lvalue}'`;
}
// WHERE 前面是要修改的列名、列值,后面是条件的列名、列值
return new Promise((resolve, reject) => {
// 修改表数据
plus.sqlite.executeSql({
name: this.dbName,
sql: [sql],
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
},
// 获取指定数据条数 sql:"SELECT * FROM dbTable ORDER BY 'id' DESC LIMIT 15 OFFSET 'num'"
// dbTable 表名, ORDER BY 代表排序默认正序, id 是排序的条件 DESC 代表倒序,从最后一条数据开始拿
// LIMIT 15 OFFSET '${num}',这句的意思是跳过 num 条拿 15 条数据, num 为跳过多少条数据是动态值
// 例 初始num设为0就从最后的数据开始拿15条下次不拿刚获取的数据所以可以让num为15这样就能一步一步的拿完所有的数据
pullSQL(dbTable : string, id : any, num : number) {
return new Promise((resolve, reject) => {
plus.sqlite.selectSql({
name: this.dbName,
sql: `SELECT * FROM ${dbTable} ORDER BY '${id}' DESC LIMIT 15 OFFSET '${num}'`,
success(e) {
resolve(e);
},
fail(e) {
reject(e);
}
})
})
},
select(sql: string): Promise<any> {
return new Promise((resolve, reject) => {
plus.sqlite.selectSql({
name: this.dbName,
sql: sql,
success(data: any) {
console.log(`${sql} 执行成功`);
resolve(data);
},
fail(e) {
console.log(`${sql} 执行失败,${JSON.stringify(e)}`);
reject(e);
}
})
});
},
selectPage(sql: string, page: number = 1, size: number = 10): Promise<any> {
const _page = page < 1 ? 0 : page - 1;
return this.select(`${sql} limit ${size} offset ${_page*size}`);
}
}