Update query in sqlite3 with fob id coming from gateway code

if(msg.payload=="CR_SUCCESS"&& msg.id=='04222436')
{
let newMsg1 = {};
newMsg1.topic = `UPDATE NCS_DATABASE SET Status='DR1 OFF' WHERE Person_ID='${msg.id}'`;
return newMsg1;
}

You have shown the code but we also need to see the output of a debug node showing what is going into the function, and also what is coming out of the function node.
Give the debug nodes names so we can see which is which and set them to Show Complete Message.

9/2/2020, 11:10:59 PMnode: DR1ON debug
UPDATE NCS_DATABASE SET Status='DR1 ON' WHERE Person_ID='04222436' : msg : Object
object
topic: "UPDATE NCS_DATABASE SET Status='DR1 ON' WHERE Person_ID='04222436'"
_msgid: "74b1d386.4ba3ec"
9/2/2020, 11:07:58 PMnode: DR1OFF debug
UPDATE NCS_DATABASE SET Status='DR1 OFF' WHERE Person_ID='04222436' : msg : Object
object
topic: "UPDATE NCS_DATABASE SET Status='DR1 OFF' WHERE Person_ID='04222436'"
_msgid: "dc47d4aa.bf953

Getting information from you is very difficult, it is no good just posting the result unless you tell us what is wrong with it.

Sorry,Sir
Problem is my DB is not updating result as per my requirements

I want first DR ON should get updated when some person access the door with Person ID
then i want DROFF after 4 sec to be updated in DB.
problem is my flow and my idea is not working as per my requirement so help me by some idea.

Once again your answer is confusing. Is it the update that is not working or the timing? If you want to delay one of the messages then feed the first message direct to the database node and feed the second through the delay node.

update is like this
steps
1-> entered the person id,CR_SUCCESS i recieved,DRON geting updated and after 4 sec DROFF is updating.
problem:
Steps
1->again if i am enterring person id ,CR_SUCCESS i am getting but now that DRON is not updating in DB,it is showing the same DROFF ??????????

Sir Should i attach my flow for your reference?/

Do you mean it works the first time but not the second?
Connect a debug node showing what is coming out of the database node (set to show complete message). Go through the sequence so that it fails and screenshot the resulting debug output.

You have not shown all the debug nor shown the start of the first one nor fully expanded the second one.
Also how do you know that it has not worked?

check these attached screenshots

So you update a row in the database and change the column 'Status' to 'DR1 ON' then four seconds later change you update the same row in the database and change the column 'Status' to 'DR1 OFF'

What is the problem?

It is no good just saying that. You must explain clearly what the debug is showing and what it should be showing. You still have not said exactly what is going wrong.

Problem is after DROFF i want DRON again when i am enterring same person id if not then no change

And what is your flow logic to do this?

[
    {
        "id": "a0a22bbe.4dd42",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "b469431a.6aa488",
        "type": "mqtt in",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "topic": "FOB",
        "qos": "1",
        "datatype": "auto",
        "broker": "2b0fd3c9.2369b4",
        "x": 90,
        "y": 220,
        "wires": [
            [
                "950a518e.f68788"
            ]
        ]
    },
    {
        "id": "a5b28ca7.3f4ae8",
        "type": "mqtt out",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "topic": "MATCH",
        "qos": "1",
        "retain": "true",
        "broker": "2b0fd3c9.2369b4",
        "x": 1260,
        "y": 280,
        "wires": []
    },
    {
        "id": "4189be61.b54dd",
        "type": "sqlite",
        "z": "a0a22bbe.4dd42",
        "mydb": "ebf455b5.0e09e",
        "sqlquery": "msg.topic",
        "sql": "SELECT * FROM NCS WHERE Person_ID='{{payload}}';",
        "name": "SQLITE3",
        "x": 760,
        "y": 220,
        "wires": [
            [
                "81f4171d.a3c57"
            ]
        ]
    },
    {
        "id": "81f4171d.a3c57",
        "type": "switch",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "empty"
            },
            {
                "t": "nempty"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 850,
        "y": 300,
        "wires": [
            [
                "b6e09667.cfd1f8"
            ],
            [
                "47c73500.14fd6c"
            ]
        ]
    },
    {
        "id": "b6e09667.cfd1f8",
        "type": "change",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "CR_FAIL",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1040,
        "y": 220,
        "wires": [
            [
                "a5b28ca7.3f4ae8",
                "b4f536b1.835ee"
            ]
        ]
    },
    {
        "id": "47c73500.14fd6c",
        "type": "change",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "CR_SUCCESS",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1040,
        "y": 360,
        "wires": [
            [
                "a5b28ca7.3f4ae8",
                "b4c18fb9.b990e8",
                "743e1ca5.336ef4"
            ]
        ]
    },
    {
        "id": "4629413e.f8d6f",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "func": "let newMsg = {};\nnewMsg.topic = `SELECT Person_ID FROM NCS_DATABASE WHERE Person_ID='${msg.payload}'`;\nreturn newMsg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 520,
        "y": 220,
        "wires": [
            [
                "4189be61.b54dd"
            ]
        ]
    },
    {
        "id": "950a518e.f68788",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "id no space ",
        "func": "msg.payload = msg.payload.replace(/\\s/g, '')\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 290,
        "y": 220,
        "wires": [
            [
                "4629413e.f8d6f",
                "7cc57b61.7cddbc"
            ]
        ]
    },
    {
        "id": "e1c813c6.492a18",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "function2",
        "func": "let newMsg1 = {};\nnewMsg1.topic = `SELECT Fob_Tag_ID  FROM NCS_DATABASE WHERE Fob_Tag_ID ='${msg.payload}'`;\nreturn newMsg1;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 520,
        "y": 280,
        "wires": [
            [
                "4189be61.b54dd"
            ]
        ]
    },
    {
        "id": "89c07b92.62f2",
        "type": "mqtt in",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "topic": "TRADE",
        "qos": "1",
        "datatype": "auto",
        "broker": "2b0fd3c9.2369b4",
        "x": 90,
        "y": 280,
        "wires": [
            [
                "42ce4ee7.c335d8"
            ]
        ]
    },
    {
        "id": "42ce4ee7.c335d8",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "tagid no space ",
        "func": "msg.payload = msg.payload.replace(/\\s/g, '')\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 300,
        "y": 280,
        "wires": [
            [
                "e1c813c6.492a18",
                "ce2e3f7f.05cd6"
            ]
        ]
    },
    {
        "id": "8b7358f2.2eb348",
        "type": "mqtt in",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "topic": "FOB_and_TAG",
        "qos": "1",
        "datatype": "buffer",
        "broker": "2b0fd3c9.2369b4",
        "x": 120,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "b4c18fb9.b990e8",
        "type": "change",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "id",
                "pt": "msg",
                "to": "id",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1330,
        "y": 360,
        "wires": [
            [
                "cf6f5371.cdfa7",
                "c49d4ea7.7114e8"
            ]
        ]
    },
    {
        "id": "2cd6eb05.9d3e94",
        "type": "sqlite",
        "z": "a0a22bbe.4dd42",
        "mydb": "ebf455b5.0e09e",
        "sqlquery": "msg.topic",
        "sql": "UPDATE NCS_DATABASE\nSET Status='{{payload}}'\nWHERE Person_ID='{{id}}';",
        "name": "SQLITE3",
        "x": 1920,
        "y": 360,
        "wires": [
            [
                "b6956c2d.14843",
                "c0b6892.ddb8178"
            ]
        ]
    },
    {
        "id": "7cc57b61.7cddbc",
        "type": "change",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "id",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 160,
        "wires": [
            [
                "4629413e.f8d6f"
            ]
        ]
    },
    {
        "id": "ce2e3f7f.05cd6",
        "type": "change",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "id",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 340,
        "wires": [
            [
                "e1c813c6.492a18"
            ]
        ]
    },
    {
        "id": "1d52ba53.71e1de",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "DR1OFF_Person ID",
        "func": "/*\n\n `UPDATE NCS_DATABASE SET Status='${msg.payload}' WHERE Person_ID='04222436'`;\n;*/\nlet newMsg1 = {};\n newMsg1.topic =setInterval(function(){ alert(\"DR1OFF\"); }, 4000);\nreturn newMsg1;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 1220,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "56217bc.1faf604",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": "NCS_debug1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1780,
        "y": 280,
        "wires": []
    },
    {
        "id": "c0ab02b9.8d1e1",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": "NCS_debug11",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2300,
        "y": 620,
        "wires": []
    },
    {
        "id": "b6956c2d.14843",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": "DR1OFF debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 2020,
        "y": 540,
        "wires": []
    },
    {
        "id": "b4f536b1.835ee",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": " debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1470,
        "y": 220,
        "wires": []
    },
    {
        "id": "743e1ca5.336ef4",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": " debug2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1500,
        "y": 260,
        "wires": []
    },
    {
        "id": "cf6f5371.cdfa7",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "fun 1",
        "func": "\nlet newMsg1 = {};\nnewMsg1.topic = `UPDATE NCS_DATABASE SET Status='DR1 ON' WHERE Person_ID='${msg.id}'`;\nreturn newMsg1;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 1570,
        "y": 360,
        "wires": [
            [
                "2cd6eb05.9d3e94",
                "56217bc.1faf604"
            ]
        ]
    },
    {
        "id": "e406ca6a.43f64",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "FUN 1",
        "func": "if(msg.id==\"0422236\")\n{\nlet newMsg1 = {};\nnewMsg1.topic = `UPDATE NCS_DATABASE SET Status='DR1 OFF' WHERE Person_ID='${msg.id}'`;\nreturn newMsg1;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 2450,
        "y": 360,
        "wires": [
            [
                "16ccb566.94baeb",
                "7d03180.a2249e8"
            ]
        ]
    },
    {
        "id": "16ccb566.94baeb",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": "DR1OFF debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 2400,
        "y": 260,
        "wires": []
    },
    {
        "id": "7d03180.a2249e8",
        "type": "sqlite",
        "z": "a0a22bbe.4dd42",
        "mydb": "ebf455b5.0e09e",
        "sqlquery": "msg.topic",
        "sql": "UPDATE NCS_DATABASE\nSET Status='{{payload}}'\nWHERE Person_ID='{{id}}';",
        "name": "SQLITE3",
        "x": 2640,
        "y": 460,
        "wires": [
            [
                "162f712c.af459f"
            ]
        ]
    },
    {
        "id": "162f712c.af459f",
        "type": "debug",
        "z": "a0a22bbe.4dd42",
        "name": "NCS_debug10",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2700,
        "y": 340,
        "wires": []
    },
    {
        "id": "c49d4ea7.7114e8",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "fun 2",
        "func": "if(msg.id==\"04222437\")\n{\nlet newMsg1 = {};\nnewMsg1.topic = `UPDATE NCS_DATABASE SET Status='DR2 ON' WHERE Person_ID='${msg.id}'`;\nreturn newMsg1;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 1570,
        "y": 440,
        "wires": [
            [
                "2cd6eb05.9d3e94"
            ]
        ]
    },
    {
        "id": "1090af01.08e149",
        "type": "function",
        "z": "a0a22bbe.4dd42",
        "name": "FUN 2",
        "func": "if(msg.id==\"0422237\")\n{\nlet newMsg1 = {};\nnewMsg1.topic = `UPDATE NCS_DATABASE SET Status='DR2 OFF' WHERE Person_ID='${msg.id}'`;\nreturn newMsg1;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 2450,
        "y": 420,
        "wires": [
            [
                "7d03180.a2249e8",
                "c0ab02b9.8d1e1"
            ]
        ]
    },
    {
        "id": "c0b6892.ddb8178",
        "type": "delay",
        "z": "a0a22bbe.4dd42",
        "name": "",
        "pauseType": "delay",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "x": 2080,
        "y": 280,
        "wires": [
            [
                "e406ca6a.43f64",
                "1090af01.08e149"
            ]
        ]
    },
    {
        "id": "2b0fd3c9.2369b4",
        "type": "mqtt-broker",
        "z": "",
        "name": "",
        "broker": "tcp://mqtt.eclipse.org",
        "port": "1883",
        "clientid": "NCS_Client_03",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": false,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "ebf455b5.0e09e",
        "type": "sqlitedb",
        "z": "",
        "db": "/home/shipra/Documents/NCS_Projects/IOT/NCS_API/NCS.db",
        "mode": "RWC"
    }
]