简体   繁体   English

在将sqlite3数据库读入pandas DataFrame时如何修复SyntaxError

[英]How to fix SyntaxError when reading sqlite3 database into a pandas DataFrame

I'am trying to read sqlite3 database using pandas. 我正在尝试使用pandas读取sqlite3数据库。 Unfortunately in my code is SyntaxError but i can't find it. 不幸的是在我的代码中是SyntaxError但我找不到它。

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 "

The sqlalchemy package looks like 6 years old ( sqlalchemy\\sql\\expression.py.py", line 2452 ), before an important refactoring. 在重要的重构之前sqlalchemy\\sql\\expression.py.py", line 2452 sqlalchemy包看起来像6岁( sqlalchemy\\sql\\expression.py.py", line 2452 )。

You should remove this package and reinstall it with a newer version or try an update with: 您应该删除此软件包并使用较新版本重新安装或尝试使用以下更新:

pip install SQLAlchemy --upgrade

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM