Program Tip

MySQL 오류 # 1064를 어떻게 수정할 수 있습니까?

programtip 2020. 12. 4. 20:21
반응형

MySQL 오류 # 1064를 어떻게 수정할 수 있습니까?


MySQL에 명령을 내릴 때 # 1064 "syntax error"오류가 발생합니다.

  1. 무슨 뜻이에요?

  2. 어떻게 고칠 수 있습니까?


TL; DR

오류 # 1064는 MySQL이 명령을 이해할 수 없음을 의미합니다. 그것을 해결하기 위해:

  • 오류 메시지를 읽으십시오. 명령에서 MySQL이 혼란 스러워진 곳정확하게 알려줍니다 .

  • 명령을 검토하십시오. 프로그래밍 언어를 사용하여 명령을 만드는 echo경우 console.log(), 또는 이와 동등한 명령을 사용 하여 전체 명령 을 표시하여 볼 수 있습니다.

  • 설명서를 확인하십시오. 그 시점에서 MySQL이 예상했던 것과 비교 하면 문제가 종종 분명해집니다.

  • 예약어를 확인하십시오. 개체 식별자에서 오류가 발생한 경우 예약어가 아닌지 확인하고, 예약어 인 경우 올바르게 인용되었는지 확인합니다.

  1. 으악 !! # 1064 무엇을 의미 합니까?

    오류 메시지 는 엉뚱한 것처럼 보일있지만 (종종) 믿을 수 없을 정도로 정보를 제공하고 무엇이 잘못되었는지를 정확히 파악할 수있는 충분한 세부 정보를 제공합니다. MySQL이 당신에게 말하는 것을 정확히 이해함으로써, 당신은 미래에 이런 종류의 문제를 해결할 수 있습니다.

    많은 프로그램에서와 마찬가지로 MySQL 오류는 발생한 문제 유형따라 코딩됩니다 . 오류 # 1064 는 구문 오류입니다.

    • 당신이 말하는 "구문"은 무엇입니까? 요술인가?

      "구문"은 많은 프로그래머가 컴퓨터의 맥락에서만 접하는 단어이지만 실제로는 더 넓은 언어학에서 차용 한 것입니다. 이것은 문장 구조를 의미합니다 : 즉 , 문법의 규칙 ; 즉, 언어 내에서 유효한 문장을 구성하는 것을 정의하는 규칙입니다 .

      예를 들어, 다음 영어 문장에는 구문 오류가 있습니다 (무기한 관사 "a"는 항상 명사 앞에 와야하기 때문).

      이 문장에는 구문 오류 a가 있습니다.

    • 이것이 MySQL과 무슨 관련이 있습니까?

      컴퓨터에 명령을 내릴 때마다 가장 먼저 수행해야하는 작업 중 하나는 명령을 이해하기 위해 해당 명령을 "분석"하는 것입니다. "구문 오류"는 언어 내에서 유효한 명령을 구성하지 않기 때문에 구문 분석기가 요청 된 내용을 이해할 수 없음을 의미합니다. 즉, 명령이 프로그래밍 언어의 문법을 위반합니다 .

      컴퓨터가 명령을 사용하여 작업을 수행하기 전에 명령을 이해해야한다는 점에 유의하는 것이 중요합니다. 구문 오류가 있기 때문에 MySQL은 뒤에 무엇이 있는지 알지 못하므로 데이터베이스를보기 전에 포기 하므로 스키마 또는 테이블 내용이 관련이 없습니다.

  2. 어떻게 고치나요?

    분명히 명령이 MySQL의 문법을 위반하는지 확인해야합니다. 이것은 매우 뚫을 수없는 것처럼 들릴지 모르지만 MySQL은 여기서 우리를 돕기 위해 정말 열심히 노력하고 있습니다. 우리가해야 할 일은 ...

    • 메시지를 읽으십시오!

      MySQL 은 구문 분석기에서 구문 오류가 발생한 위치를 정확히 알려줄 뿐만 아니라 수정을위한 제안도합니다. 예를 들어, 다음 SQL 명령을 고려하십시오.

      UPDATE my_table WHERE id=101 SET name='foo'
      

      이 명령은 다음 오류 메시지를 생성합니다.

      ERROR 1064 (42000): 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 'WHERE id=101 SET name='foo'' at line 1

      MySQL은 모든 것이 괜찮아 보였지만 WHERE문제가 발생했습니다. 즉, WHERE그 시점에서 만날 것으로 예상되지 않았습니다 .

      메시지는 ...near '' at line...단순히 명령의 끝이 예기치 않게 발생했음을 의미합니다. 즉, 명령이 끝나기 전에 다른 내용이 나타나야합니다.

    • 명령의 실제 텍스트를 검토하십시오!

      프로그래머는 종종 프로그래밍 언어를 사용하여 SQL 명령을 만듭니다. 예를 들어 PHP 프로그램은 다음과 같은 (잘못된) 줄을 가질 수 있습니다.

      $result = $mysqli->query("UPDATE " . $tablename ."SET name='foo' WHERE id=101");
      

      이것을 두 줄로 쓰면

      $query = "UPDATE " . $tablename ."SET name='foo' WHERE id=101"
      $result = $mysqli->query($query);
      

      그런 다음 추가 echo $query;하거나 var_dump($query)쿼리가 실제로

      UPDATE userSET name='foo' WHERE id=101
      

      종종 오류를 즉시 확인하고 수정할 수 있습니다.

    • 명령에 따르십시오!

      MySQL은 또한 " 사용할 올바른 구문에 대한 MySQL 버전에 해당하는 설명서를 확인 "할 것을 권장합니다 . 그걸하자.

      저는 MySQL v5.6을 사용하고 있으므로 명령에 대한 해당 버전의 수동 입력으로UPDATE 넘어가겠습니다 . 페이지의 첫 번째 항목은 명령의 문법입니다 (모든 명령에 적용됨).

      UPDATE [LOW_PRIORITY] [IGNORE] table_reference
          SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
          [WHERE where_condition]
          [ORDER BY ...]
          [LIMIT row_count]
      

      The manual explains how to interpret this syntax under Typographical and Syntax Conventions, but for our purposes it's enough to recognise that: clauses contained within square brackets [ and ] are optional; vertical bars | indicate alternatives; and ellipses ... denote either an omission for brevity, or that the preceding clause may be repeated.

      We already know that the parser believed everything in our command was okay prior to the WHERE keyword, or in other words up to and including the table reference. Looking at the grammar, we see that table_reference must be followed by the SET keyword: whereas in our command it was actually followed by the WHERE keyword. This explains why the parser reports that a problem was encountered at that point.

    A note of reservation

    Of course, this was a simple example. However, by following the two steps outlined above (i.e. observing exactly where in the command the parser found the grammar to be violated and comparing against the manual's description of what was expected at that point), virtually every syntax error can be readily identified.

    I say "virtually all", because there's a small class of problems that aren't quite so easy to spot—and that is where the parser believes that the language element encountered means one thing whereas you intend it to mean another. Take the following example:

    UPDATE my_table SET where='foo'
    

    Again, the parser does not expect to encounter WHERE at this point and so will raise a similar syntax error—but you hadn't intended for that where to be an SQL keyword: you had intended for it to identify a column for updating! However, as documented under Schema Object Names:

    If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. (Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.) Reserved words are listed at Section 9.3, “Keywords and Reserved Words”.

    [ deletia ]

    The identifier quote character is the backtick (“`”):

    mysql> SELECT * FROM `select` WHERE `select`.id > 100;

    If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:

    mysql> CREATE TABLE "test" (col INT);
    ERROR 1064: You have an error in your SQL syntax...
    mysql> SET sql_mode='ANSI_QUOTES';
    mysql> CREATE TABLE "test" (col INT);
    Query OK, 0 rows affected (0.00 sec)


For my case, I was trying to execute procedure code in MySQL, and due to some issue with server in which Server can't figure out where to end the statement I was getting Error Code 1064. So I wrapped the procedure with custom DELIMITER and it worked fine.

For example, Before it was:

DROP PROCEDURE IF EXISTS getStats;
CREATE PROCEDURE `getStats` (param_id INT, param_offset INT, param_startDate datetime, param_endDate datetime)
BEGIN
    /*Procedure Code Here*/
END;

After putting DELIMITER it was like this:

DROP PROCEDURE IF EXISTS getStats;
DELIMITER $$
CREATE PROCEDURE `getStats` (param_id INT, param_offset INT, param_startDate datetime, param_endDate datetime)
BEGIN
    /*Procedure Code Here*/
END;
$$
DELIMITER ;

참고URL : https://stackoverflow.com/questions/23515347/how-can-i-fix-mysql-error-1064

반응형