我正试图在jupyter笔记本中进行查询,并将其保存在pandas数据帧中。
这是我写的代码:
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=DESKTOP-P2RVLB2RENO_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)
此外,我认为在查询的末尾不需要分号