【发布时间】:2018-05-12 17:09:44
【问题描述】:
构建了一个简单的函数来执行sql
from sqlalchemy.engine.url import URL
POOL_RECYCLE = 7200
POOL_SIZE = 10
MAX_OVERFLOW = 20
POOL_TIMEOUT = 30
class DatabaseConnect(object):
def __init__(self, dict_db):
self.__config = dict_db
self.Engine = create_engine(URL(**self.__config),
pool_size=POOL_SIZE,
max_overflow=MAX_OVERFLOW,
pool_timeout=POOL_TIMEOUT,
pool_recycle=POOL_RECYCLE)
def db_connect(self):
return self.Engine.connect()
一个dict用于包含连接数据库的信息。
DICT_DB_DW_PG_BI = {
'drivername': 'postgres',
'host': 'xxx',
'port': '5432',
'database': 'alpha',
'username': 'xx',
'password': 'xxx'}
当我运行 sql 时,我使用如下函数:
for i_sql in sql_input.split(';'):
engine = self.db_dw_pg_bi.Engine
engine.execute(sqlalchemy.text(i_sql))
我用上面的方法运行下面的sql,没有报错,但是查了数据库,发现数据库没有变化。
delete from test.lxy_norm_user;
drop table if exists test.lxy_norm_user;
【问题讨论】:
标签: python postgresql sqlalchemy