Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python代码中检查Airflow数据库中是否已经存在Airflow用户_Python_Airflow_Basic Authentication_Createuser - Fatal编程技术网

如何在Python代码中检查Airflow数据库中是否已经存在Airflow用户

如何在Python代码中检查Airflow数据库中是否已经存在Airflow用户,python,airflow,basic-authentication,createuser,Python,Airflow,Basic Authentication,Createuser,我是airflow的新手,有一项任务是为airflow Web服务器添加基本身份验证。我使用下面的python代码(从)实现了这一点。我能够创建一个用户并使用该用户/密码对Web服务器进行身份验证 # navigate to the airflow installation directory $ cd ~/airflow $ python Python 2.7.9 (default, Feb 10 2015, 03:28:08) Type "help", "cop

我是airflow的新手,有一项任务是为airflow Web服务器添加基本身份验证。我使用下面的python代码(从)实现了这一点。我能够创建一个用户并使用该用户/密码对Web服务器进行身份验证

# navigate to the airflow installation directory
$ cd ~/airflow
$ python
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
Type "help", "copyright", "credits" or "license" for more information.
>>> import airflow
>>> from airflow import models, settings
>>> from airflow.contrib.auth.backends.password_auth import PasswordUser
>>> user = PasswordUser(models.User())
>>> user.username = 'new_user_name'
>>> user.email = 'new_user_email@example.com'
>>> user.password = 'set_the_password'
>>> session = settings.Session()
>>> session.add(user)
>>> session.commit()
>>> session.close()
>>> exit()
但是,如果用户已经存在于数据库中,那么现在我需要确保只更新用户,而不是创建用户。 是否有
会话
对象的属性,如
获取
获取用户
,以了解用户是否已经存在

我可以看到在数据库的airflowdb.users表中创建的用户。但是,我需要从Python代码中进行检查


提前感谢您的帮助。

会话是的一个实例。它的
execute()
方法允许您运行SQL表达式。因此,您只需从用户表中选择:

from airflow import settings
session = settings.Session()
users = session.execute("SELECT * FROM users").fetchall()
print(users)
session.close()
输出:

[(1, 'new_user_name', 'new_user_email@example.com', '$2b$12$qr7bCywAv4kTeakli.fTXuNUXonqSzXUqZIX/ZVIMceZDEIsKhgNO', 0)]