Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用WHERE和“'”进行查询时,jupyter笔记本中的查询出错_Python_Sql Server_Pandas_Dataframe_Jupyter Notebook - Fatal编程技术网

Python 使用WHERE和“'”进行查询时,jupyter笔记本中的查询出错

Python 使用WHERE和“'”进行查询时,jupyter笔记本中的查询出错,python,sql-server,pandas,dataframe,jupyter-notebook,Python,Sql Server,Pandas,Dataframe,Jupyter Notebook,我试图在jupyter笔记本中进行查询,并将其保存在pandas数据框中 以下是我编写的代码: import pyodbc conn = pyodbc.connect('Driver={SQL Server};' 'Server=DESKTOP-P2RVLB2\RENO_DATACAMP;' 'Database=Introduction_to_SQL_Server;'

我试图在jupyter笔记本中进行查询,并将其保存在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

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

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

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


此外,我相信分号在查询结束时是不必要的

嘿,它的工作!谢谢mitch,我刚刚学习了sql和python,我刚刚开始了解如何使用双引号和单引号。是的,分号是不需要的