【问题标题】:Query in jupyter notebook error when querying with WHERE and " ' "使用 WHERE 和“'”查询时在 jupyter notebook 中查询错误
【发布时间】:2021-04-29 06:17:33
【问题描述】:

我正在尝试在 jupyter notebook 中进行查询并将其保存在 pandas 数据框中。

这是我写的代码:

import pyodbc 
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=DESKTOP-P2RVLB2\RENO_DATACAMP;'
                      'Database=Introduction_to_SQL_Server;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()
# SELECT the country column FROM the eurovision table
sql_query = pd.read_sql_query('SELECT description, event_year from Introduction_to_SQL_Server.dbo.grid WHERE description ='Vandalism';',conn)
print(sql_query)
print(type(sql_query))

并显示如下错误:

File "<ipython-input-69-0c8fd6cbc2be>", line 9
    sql_query = pd.read_sql_query('SELECT description, event_year from Introduction_to_SQL_Server.dbo.grid WHERE description ='Vandalism';',conn)
                                                                                                                               ^
SyntaxError: invalid syntax

请帮我解决这个错误,提前谢谢!

【问题讨论】:

    标签: python sql-server pandas dataframe jupyter-notebook


    【解决方案1】:

    对整个查询使用双引号,在查询内部使用单引号。

    sql_query = pd.read_sql_query("SELECT description, event_year from Introduction_to_SQL_Server.dbo.grid WHERE description ='Vandalism'",conn)
    
    

    我也相信分号在查询结束时不是必需的

    【讨论】:

    • 嘿,它的工作!谢谢mitch,我只是在学习sql和python,而且我刚刚开始知道如何使用双引号和单引号。是的,不需要分号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多