从MYSQL提取日期之间的数据到熊猫

时间:2019-12-16 19:14:55

标签: python mysql pandas date


**When i am running below query**

from sqlalchemy import create_engine
import pymysql
import pandas as pd
import datetime
db_connection_str = 'mysql+pymysql://root:*****@localhost/northwind'
db_connection = create_engine(db_connection_str)
df = pd.read_sql('SELECT * FROM employees where BirthDate between '1948-12-08' and '1960-05-29'', con=db_connection)
df

**I am receiving below Error**

  File "", line 7
    df = pd.read_sql('SELECT * FROM employees where BirthDate between '1948-12-8' and '1960-5-29'', con=db_connection)
                                                                          ^
SyntaxError: invalid syntax

--------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:2)

使用双引号将sql语句括起来,因为您在日期中使用了单引号:

df = pd.read_sql("SELECT * FROM employees where BirthDate between '1948-12-08' and '1960-05-29'", con=db_connection)