Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query or identifier is too long in Jupyter? [duplicate]

I have a Jupyter notebook in which I want to execute a query. I'm using sqlalchemy and the following code is working just fine if I run it as script in PyCharm:

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine('mysql+mysqlconnector://user:password@server_address:3306/DBase', echo=False)
connection = engine.connect()

query = "SELECT week(date) as week, count(*) FROM table GROUP BY week"
df = pd.read_sql(query, con=engine)
print(df)

However, when I try to run this in Jupyter notebook I'll get the error:

ProgrammingError: (mysql.connector.errors.ProgrammingError) 1059 (42000): Identifier name 'SELECT week(date) as week, count(*) FROM table GROUP BY week' is too long

When I try something like

query = "SELECT count(*) as number FROM table"

It does work correctly. As soon as I add some statement to the query it will throw the error. I'm not sure why it is complaining about an identifier specifically. It doesn't seem to be due to the 'AS' since it will also throw the error if I leave that out and add a WHERE statement. Any suggestions?

like image 612
Chrisvdberge Avatar asked Oct 17 '25 09:10

Chrisvdberge


1 Answers

Instead of using this:

df = pd.read_sql(query, con=engine)

You should use this:

df = pd.read_sql_query(query, con=engine)

Your query is somehow getting treated as a SQL Table name when you're calling read_sql, the reason for that is unknown for me and I also couldn't find a reason for it from the documentation.

like image 131
ruohola Avatar answered Oct 20 '25 07:10

ruohola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!