如何在Python3中创建Rest-API / Webservice以从Oracle DB中获取数据?

时间:2019-07-18 11:27:25

标签: python python-3.x web-services flask flask-restful

我想使用Flask-Python创建REST API,它将连接到Oracle DB ...将查询某个表并返回一个值。 如何创建?

连接到Oracle DB的代码- 脚本名称-app1.py

#!/usr/bin/python3.4
import cx_Oracle
con = cx_Oracle.connect('Username/Password@Hostname/Servicename')
cur = con.cursor()
cur.execute("SELECT ColumnName FROM TableName WHERE ROWNUM = 1")
result = cur.fetchall()
print(result[0][0])
if result[0][0]==6:
    print("Workflow is Running")
else:
    print("Workflow is not Running")
cur.close()
con.close()

输出-

$ python3 app1.py
6 
Workflow is Running 

我创建了一个普通的Hello World API-

Flask Web服务的代码- 脚本名称-app.py

#!/usr/bin/python3.4
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "Hello World!"
if __name__ == '__main__':
    app.run(debug=True)

输出-

$ python3 app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 241-829-998
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Jul/2019 11:43:18] "GET / HTTP/1.1" 200 -

两个脚本都可以正常运行。请告知我如何集成两个脚本,以便每当我在浏览器中访问“ http://127.0.0.1:5000/”时,我希望浏览器返回“工作流正在运行”,而不是“ Hello World” ”或“工作流未运行”。

谢谢。

0 个答案:

没有答案