python 中pandas 使用read_sql_query 带模糊查询报错(使用 like ‘xxx%‘, PostgreSQL 数据库)

import pandas as pd
import sqlalchemy as sqla

# 获取连接
def getCon():
    return sqla.create_engine("postgresql+psycopg2://etl_user:[email protected]/bigdata_tp")

if __name__ == '__main__':
    db = getCon()

    df = pd.read_sql_query('''select * from pg_tables where tablename like 'isc%'  limit 200''', con=db)
    print(pd)

报错

"D:\Program Files\Python38\python.exe" D:/code/IdeaProjects/pythonTool/study/demo.py
Traceback (most recent call last):
  File "D:/code/IdeaProjects/pythonTool/study/demo.py", line 11, in 
    df = pd.read_sql_query('''select * from pg_tables where tablename like 'isc%'  limit 200''', con=db)
  File "D:\Program Files\Python38\lib\site-packages\pandas\io\sql.py", line 399, in read_sql_query
    return pandas_sql.read_query(
  File "D:\Program Files\Python38\lib\site-packages\pandas\io\sql.py", line 1557, in read_query
    result = self.execute(*args)
  File "D:\Program Files\Python38\lib\site-packages\pandas\io\sql.py", line 1402, in execute
    return self.connectable.execution_options().execute(*args, **kwargs)
  File "", line 2, in execute
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\util\deprecations.py", line 401, in warned
    return fn(*args, **kwargs)
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 3152, in execute
    return connection.execute(statement, *multiparams, **params)
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1280, in execute
    return self._exec_driver_sql(
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1584, in _exec_driver_sql
    ret = self._execute_context(
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1851, in _execute_context
    self._handle_dbapi_exception(
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 2036, in _handle_dbapi_exception
    util.raise_(exc_info[1], with_traceback=exc_info[2])
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1808, in _execute_context
    self.dialect.do_execute(
  File "D:\Program Files\Python38\lib\site-packages\sqlalchemy\engine\default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
TypeError: dict is not a sequence

Process finished with exit code 1

这里的错误是由 sql语句中的%引起,将%去掉即可正常执行。

解决方式: 使用%% 

select * from pg_tables where tablename like 'isc%%'  limit 200

你可能感兴趣的:(PostgreSQL,python,postgresql)