4

I'am trying to read sqlite3 database using pandas. Unfortunately in my code is SyntaxError but i can't find it.

The code is the same as in many tutorials so i don't know where is the problem

import pandas as pd
import sqlite3

con = sqlite3.connect("activity_log.db")
query = "SELECT * from log"
df = pd.read_sql_query(query, con)

print(df)

con.close()

result:

" Traceback (most recent call last):
  File "C:/Users/g50-45/Desktop/ML/1z10_webapp/data_analyser.py", line 7, in <module>
    df = pd.read_sql_query(query, con)

  File "C:\Users\g50-45\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 325, in read_sql_query
    pandas_sql = pandasSQL_builder(con)

  File "C:\Users\g50-45\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 575, in pandasSQL_builder
    if _is_sqlalchemy_connectable(con):

  File "C:\Users\g50-45\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 44, in _is_sqlalchemy_connectable
    import sqlalchemy

  File "C:\Users\g50-45\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\__init__.py", line 12, in <module>
    from sqlalchemy.sql import (

  File "C:\Users\g50-45\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\sql\__init__.py", line 7, in <module>
    from sqlalchemy.sql.expression import (

  File "C:\Users\g50-45\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\sql\expression.py", line 2452

    ^
SyntaxError: invalid syntax "
3
  • 2
    can you check what version of pandas you are using, and also sqlalchemy ? Commented Sep 9, 2019 at 20:59
  • is the sqlite database accessible via the folder structure? What happens if you connect to memory and try to write to it. does it still give you an error? con = sqlite3.connect(':memory:'). By checking if its a memory problem or not, it can help us narrow down where the error is coming from Commented Sep 9, 2019 at 21:16
  • 1
    I didn't think about it. I updated the pandas from 0.25.0 to 0.25.1 and sqlalchemy from 0.7.10 to 1.3.8. Now everything works as it should. Thank you very much Commented Sep 9, 2019 at 21:20

1 Answer 1

2

The sqlalchemy package looks like 6 years old (sqlalchemy\sql\expression.py.py", line 2452), before an important refactoring.

You should remove this package and reinstall it with a newer version or try an update with:

pip install SQLAlchemy --upgrade
Sign up to request clarification or add additional context in comments.

2 Comments

As you said . Upgrade was solution to my problem. Thanks a lot.
Nice! you're welcome. i saw it in the same time i push the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.