import pandas as pd
import sqlalchemy as sqla
# 获取连接
def getCon():
return sqla.create_engine("postgresql+psycopg2://etl_user:etl_user@172.16.5.85/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 <module>
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 "<string>", 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
|