簡體   English   中英

使用 WHERE 和“'”查詢時在 jupyter notebook 中查詢錯誤

[英]Query in jupyter notebook error when querying with WHERE and " ' "

我正在嘗試在 jupyter notebook 中進行查詢並將其保存在 pandas dataframe 中。

這是我寫的代碼:

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)

另外我相信在查詢結束時不需要分號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM