我正在尝试通过Jupyter Notebook运行我的Postgres数据库查询。我遇到了要与社区分享的TypeError,看看是否有更好的方法执行此代码:
import pandas as pd #version '0.23.3'
import sqlalchemy #version '1.2.15'
from sqlalchemy import create_engine
engine = create_engine('postgresql://username:xxxxxxxx@localhost/DatabaseName')
query_18 = """
SELECT c1.name AS country, region, l.name AS language,
basic_unit, frac_unit
FROM countries AS c1
FULL JOIN languages AS l
USING (code)
FULL JOIN currencies AS c2
USING (code)
WHERE region LIKE 'M%esia';"""
df = pd.read_sql_query(query_18, engine)
df.head()
这会引发TypeError:
TypeError: 'dict' object does not support indexing
我能够将错误作为query_18
中的string format issue发出。我的解决方案是使用%
来逃避%%
:
示例:
# ...
WHERE region LIKE 'M%%esia';
# ...
执行相同代码是否有更逻辑或更简洁的方式?