简体   繁体   English

Flask 抛出 SQL 查询作为语法错误

[英]Flask throwing SQL query as a syntax error

I am trying to run a query through Flask using a curser.我正在尝试使用光标通过 Flask 运行查询。 I checked my query on a syntax checker and it said there were no errors in my code;我在语法检查器上检查了我的查询,它说我的代码没有错误; however, when I run my file with python3 it gives me a syntax error.但是,当我使用 python3 运行我的文件时,它给了我一个语法错误。 Here is my code:这是我的代码:

CREATE 
OR REPLACE VIEW flight_expanded AS WITH flight_city AS 
(
   SELECT
      flight_number,
      airline,
      airplane_id,
      departure_date,
      departure_time,
      arrival_date,
      arrival_time,
      departure_airport,
      departure_city,
      arrival_airport,
      arrival_city,
      status,
      base_price 
   FROM
      flight 
      JOIN
         (
            SELECT
               name AS departure_airport_name,
               city AS departure_city 
            FROM
               airport
         )
         as s 
      JOIN
         (
            SELECT
               name AS arrival_airport_name,
               city AS arrival_city 
            FROM
               airport
         )
         as t 
   WHERE
      departure_airport = s.departure_airport_name 
      AND arrival_airport = t.arrival_airport_name
)
,
flight_size AS 
(
   SELECT
      flight_number as flight_num,
      airline AS airl,
      IFNULL(COUNT(ticket.ID), 0) as number_of_passengers 
   FROM
      flight NATURAL 
      LEFT JOIN
         ticket 
   GROUP BY
      flight_number,
      airline
)
SELECT
   flight_number,
   airline,
   airplane_id,
   departure_date,
   departure_time,
   arrival_date,
   arrival_time,
   departure_airport,
   departure_city,
   arrival_airport,
   arrival_city,
   status,
   base_price,
   IF(number_of_passengers < 0.7*number_of_seats, base_price, 1.2*base_price) AS sale_price,
   number_of_seats,
   number_of_passengers 
FROM
   (
      SELECT
         * 
      FROM
         flight_city AS s 
         JOIN
            (
               SELECT
                  ID,
                  airline AS al,
                  number_of_seats 
               FROM
                  airplane
            )
            AS t 
            ON (s.airplane_ID = t.ID 
            AND s.airline = t.al)
   )
   AS u 
   JOIN
      flight_size 
      ON (flight_size.flight_num = u.flight_number 
      AND flight_size.airl = u.airline)

Here is the error I get in the terminal when i run python3 airline.py :这是我在运行python3 airline.py时在终端中遇到的错误:

pymysql.err.ProgrammingError: (1064, "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 'flight_city AS (SELECT 
flight_number,airline,airplane_id,departure_date,departur' at line 1")

I would also like to mention this query works on my teammates windows system;我还要提一下这个查询在我的队友 windows 系统上有效; however, does not work on my mac using MAMP.但是,在我的使用 MAMP 的 Mac 上不起作用。 Thanks in advance.提前致谢。

EDIT: I don't have mysql installed on my system, it's just running through MAMP编辑:我的系统上没有安装 mysql,它只是通过 MAMP 运行

The reason why it's working on your teammate's machine but not yours is because of version compatibility.它在你队友的机器上工作而不是你的机器的原因是因为版本兼容性。 This is not a Python issue per se, because it's telling you that the issue is in the SQL syntax.这本身不是 Python 问题,因为它告诉您问题出在 SQL 语法中。 I would suggest that you update MySQL and try running the code again.我建议您更新 MySQL 并尝试再次运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM