How to grant access privileges to DB2 table using SQLAlchemy in a jupyter notebook

别等时光非礼了梦想. 提交于 2020-01-25 06:47:32

问题


I've followed several examples to create and drop a DB2 table using SQLAlchemy within a python jupyter notebook. That works fine. But after creating the table, I need to set privileges so others can view it. I use this code to create a new table from a Pandas dataframe "df"

from sqlalchemy import create_engine, text
engine = create_engine(r"...")

df.to_sql(name='MYTABLE', schema='MYSCHEMA', con=engine, if_exists='replace', dtype=dashdb_typemap, index=False)

I can drop the table just fine with this code:

with engine.connect() as con:
    con.execute('DROP TABLE MYSCHEMA.MYTABLE')

But neither of these work to set permissions:

with engine.connect() as con:
    con.execute('GRANT ALL ON MYSCHEMA.MYTABLE TO PUBLIC')

with engine.connect() as con:
    con.execute(text('GRANT ALL ON MYSCHEMA.MYTABLE TO PUBLIC'))

I can run the SQL in QMF and it works fine. It just doesn't seem to work from the notebook. I'm wondering if anyone sees the flaw I need to correct?

Thanks


回答1:


Maybe connected with transaction isolation, try explicit transaction control before/after the grant/revoke, or configure for autocommit

with engine.connect() as con:
    con.execute('COMMIT')
    con.execute('GRANT ALL ON MYSCHEMA.MYTABLE TO PUBLIC')
    con.execute('COMMIT')

Works for me on Db2-LUW on-premises.



来源:https://stackoverflow.com/questions/58599535/how-to-grant-access-privileges-to-db2-table-using-sqlalchemy-in-a-jupyter-notebo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!