Vikram Karthic
Vikram Karthic

Reputation: 478

Setting Up Flask DB2 connection

I am trying to connect my flask application to a DB2 database,but somehow I am unable to do. here is what I am trying to do, I am using flask extension flask_db2 for this purpose and below is how I am trying to connect

from flask import Flask from flask_db2 import DB2 app = Flask(__name__)

@app.route('/') def index() cur = db.connection.cursor("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;PWD=password)

Upvotes: 1

Views: 2164

Answers (1)

NoorJafri
NoorJafri

Reputation: 1827

from flask import Flask
from flask_db2 import DB2

app = Flask(__name__)

app.config['DB2_DATABASE'] = 'sample'
app.config['DB2_HOSTNAME'] = 'localhost'
app.config['DB2_PORT'] = 50000
app.config['DB2_PROTOCOL'] = 'TCPIP'
app.config['DB2_USER'] = 'db2inst1'
app.config['DB2_PASSWORD'] = 'db2inst1'

db = DB2(app) #You forgot that


@app.route('/')
def index():
    cur = db.connection.cursor()
    cur.execute(...)

First: You forgot to have an object to use the flask db2 module.

The DB2(app) is a constructor for your Flask extension and will bind your application to this module.

where as

db = DB2(app)

Initialises your db object with help of your extension's constructor which you can further use for your Database operations.

Secondly: use app configs to play with DB2 credentials. (This step should be done before you bind the app)

Third: You can execute your query like this

cur.execute("SELECT id FROM users") #something like that

I hope I have clarified :D just drop message. Ill try helping more.

Upvotes: 1

Related Questions