add sqlite 保存下载历史

This commit is contained in:
2023-06-28 21:11:21 +08:00
parent 95f09a96b8
commit b439920702
9 changed files with 109 additions and 30 deletions

View File

@@ -1,15 +1,39 @@
const sqlite = require("sqlite3")
const path = require("path")
database = new sqlite.Database(path.join(__dirname,"/db"), function(e) {
const path = require("path");
const { cwd } = require("process");
let p = path.join(cwd(),"/db/database.db")
database = new sqlite.Database(p, function(err) {
if (err) throw err;
});
function run(){
database.run(...arguments)
let args = arguments
console.log(args)
return new Promise((res,err)=>{
database.run(...args,(error)=>{
if(error){
err(error)
}else{
res("添加成功")
}
})
})
}
function
function getAll(sql){
let args = arguments
return new Promise((res,err)=>{
database.all(...args,function (error,data){
if(error){
err(error)
}else{
res(data)
}
})
})
}
module.exports = {
run
run,
getAll
}

View File

@@ -1,6 +1,28 @@
const { run } = require("./base");
const { run, getAll } = require("./base");
function addDownList(title,url,save){
let sql = `insert into download(title,url,save) value(?,?,?)`
run(sql,title,url,save)
function addDownList(title,url,save,downDate,isOk){
let sql = `insert into download(title,url,save,downDate,isOk) values(?,?,?,?,?)`
run(sql,title,url,save,downDate,isOk)
}
async function getDownList(){
let sql = `select * from download where isOk == 0`
try{
let list = await getAll(sql)
return list
}catch(err){
console.error(err)
return []
}
}
async function setDownState(id,state){
let sql = `update download set isOk = ? where id = ?`
await run(sql,state,id)
}
module.exports = {
addDownList,
getDownList,
setDownState
}

View File

@@ -1,16 +1,34 @@
const { run } = require("./base");
const fs = require("fs");
const path = require("path")
function initTable(){
// 创建下载列表
/**
* id
* title 剧集标题
* url 剧集链接
* save 保存地址
* downDate 添加下载日期 时间戳
* isOk 是否下载完成 0 未完成 1 完成
*/
let sql = `create table download (
id INT PRIMARY KEY NOT NULL autoincrement,
title VARCHAR(255) not null,
url varchar(255) not null,
save varcahr(255) not null
save varcahr(255) not null,
downDate INTEGER not null,
isOk INTEGER not null
)`
run(sql)
}
function init(){
let isexists = fs.existsSync(path.join(__dirname,"/db/database.db"))
if(!isexists){
initTable()
}
}
module.exports = {
initTable
init
}