如何在节点mysql中使用IF EXISTS语句?

时间:2019-05-21 08:24:35

标签: mysql sql node.js if-statement syntax-error

我正在使用NodeJs mysql模块,并且想在实际查询密码之前检查帐户是否存在。

这是sql:

let sql = `IF EXISTS (SELECT name FROM users WHERE name='${data.name}') SELECT password FROM users WHERE name='${data.name}'`;

如果没有if语句并且一旦添加IF EXISTS部分,无论我如何调整,总会收到如下错误:

code: 'ER_PARSE_ERROR',

errno: 1064,

sqlMessage:
   'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near...,

sqlState: '42000'

如果有人可以帮助我,我将不胜感激!

2 个答案:

答案 0 :(得分:1)

首先,您的SQL是开放式sql注入。使用准备好的语句,或者至少进行一些清理输入数据的尝试。

这很不正确,这不是正确的处理方式。您只需检查密码-并验证是否返回任何行:

let sql = 'SELECT password FROM users WHERE name='${data.name}';

然后执行SQL并检查是否返回了任何行。如果是,则读取密码,否则该用户名不存在。

答案 1 :(得分:0)

您没有使用正确的SQL语法,请使用此语法。

import cv2
import numpy as np

def find_nn(point, neighborhood):
"""
Finds the nearest neighborhood of a vector.

Args:
    point (float array): The initial point.
    neighborhood (numpy float matrix): The points that are around the initial point.

Returns:
    float array: The point that is the nearest neighbor of the initial point.
    integer: Index of the nearest neighbor inside the neighborhood list
"""
min_dist = float('inf')
nn = neighborhood[0]
nn_idx = 0
for i in range(len(neighborhood)):
    neighbor = neighborhood[i]
    dist = cv2.norm(point, neighbor, cv2.NORM_L2)
    if dist < min_dist:
        min_dist = dist
        nn = neighbor
        nn_idx = i
nn_idx = nn_idx + j + 1
return nn, nn_idx 

#taking 6 random points on a board of 200 x 200
points = [(10, 10), (115, 42), (36, 98), (78, 154), (167, 141), (189, 4)]
board = np.ones((200, 200, 3), dtype = np.uint8) * 255
for i in range(6):
    cv2.circle(board, points[i], 5, (0, 255, 255), -1)

for j in range(5):
    nn, nn_idx = find_nn(points[j], points[j+1:])
    points[j+1], points[nn_idx] = points[nn_idx], points[j+1]

for i in range(5):
    cv2.arrowedLine(board, points[i], points[i+1], (255, 0, 0), 1, tipLength = 0.07)
cv2.imshow('image', board)
cv2.waitKey(0)
cv2.destroyAllWindows()