插入新数据时,是否需要在NodeJS中刷新我的Sqlite3数据库?

问题描述

我正在运行一个NodeJS程序,在其中调用数据库搜索特定条目。在我的Python使用者中,我正在向数据库添加值,但是以某种方式,该数据库不会在NodeJS端进行更新。我将两个条目都输出到控制台中,并且当我从表中选择*时-在我的python端上,它输出新条目,但是在NodeJS端上,它只输出3个条目,这些条目是在部署之前添加的。 / p>

在NodeJS填充中,我调用数据库,输入查询,如果找到条目,则返回值,此后,我关闭连接,并在下一条消息上重新打开数据库

function findUserByLogin(hash) {
    const db = new sqlite3.Database('./database_worker/cracked_pw.sqlite',(err) => {
        if (err) {
            console.error(err.message);
        }
        console.log('Connected to the my database.');
    });


    var databaseCheckHash = x;
    var sql = 'SELECT cracked '
    sql += 'FROM password '
    sql += 'WHERE hash = ? '
    var sql2 = 'SELECT * from password'

    db.get(sql,hash,function(error,row) {
        if (error) {
            console.log("Error while checking the Database for Hash: " + error)
            return
        } else {
            try {
                db.all(sql2,[],(err,rows) => {
                    if (err) {
                        throw err;
                    }
                    rows.forEach((row) => {
                        console.log(row.id + " " + row.hash + " " + row.cracked);
                    });
                });

                // Calling an undefined `item `variable
                console.log("length: " + (row.cracked).length)
                databaseCheckHash = row.cracked;
                printUserEmail(row.cracked)

            } catch (e) {
                if (e instanceof ReferenceError) {
                    printError(e,true);
                } else {
                    printError(e,false);
                }
            } finally {
                if (databaseCheckHash.length == 3) {

                    databaseResult = databaseCheckHash;
                    return databaseResult;

                } else {
                    sendMsgToMQ();
                }
                db.close((err) => {
                    if (err) {
                        console.error(err.message);
                    }
                    console.log('Close the database connection.');
                });
            }


        }

    });
}

在我的Python使用者上,我要添加新条目:

import pika
import sqlite3
import os
print("[DB-Worker]Worker (consumer) storing information to database")

print("[DB-Worker]Check if sqlite database exists")
db = "./database_worker/cracked_pw.sqlite"

if not os.path.isfile(db):
    print("[DB-Worker]Database does not exist,creating Now")
    conn = sqlite3.connect(db)
    cur = conn.cursor()
    cur.execute('CREATE TABLE IF NOT EXISTS password (id INTEGER,hash VARCHAR type UNIQUE,cracked VARCHAR,PRIMARY KEY(id))')
    conn.commit()
    cur.close()
    conn.close()
else:
    print("[DB-Worker]DB exists")

def storage_callback(ch,method,properties,body):
    print(f"[DB-Worker]Attempting to save {body}")
    message = body.decode('UTF-8')
    list = message.split(',')
    hash = list[0]
    password = list[1]
    print(f"[DB-Worker]] hash is {hash},cracked pw is {password}")
    conn = sqlite3.connect(db)
    cur = conn.cursor()
    cur.execute('INSERT OR IGnorE INTO password (hash,cracked) values ("'+hash+'","'+password+'")')
    cur.execute("SELECT * FROM password")
    print(cur.fetchall())
    conn.commit()
    cur.close()
    conn.close()
    print(f"[DB-Worker]Committed to database")

我是否需要以某种方式重新加载或刷新NodeJS服务器端的数据库? 预先感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)