1

i'm trying to figure out the Uri string to connect correctly to db2 using flask with SqlAlchemy.

I've tried with

  • db2:///?Server=server&;Port=50000&User=admin&Password=admin&Database=test"
  • 'ibm_db_sa+pyodbc400://{username}:{password}@{host}:{port}/{database};currentSchema={schema}

Any clue for the correct value?

2 Answers 2

0

Just for the record the correct format (at least for me) is

ibm_db_sa://{username}:{password}@{host}:{port}/{database}

Also you need a few libraries to install via pip in order to work with alembic+flask

In the ibm_db_alembic document is a typo in the description usage, the correct one to import is

from ibm_db_alembic.ibm_db import IbmDbImpl

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a simple example:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'ibm_db_sa://enter_user:enter_password@enter_host:50000/BLUDB'

# for secure
# app.config['SQLALCHEMY_DATABASE_URI'] = 'ibm_db_sa://enter_user:enter_password@enter_host:50001/BLUDB;SECURITY=SSL;sslCertLocation=path/to/cer_file.cer;sslConnection=true;'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class User(db.Model):
    __tablename__ = 'enter_table_name'
    __table_args__ = {"schema":"enter_shema_name"}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(256), index=True)
    age = db.Column(db.Integer, index=True)
    address = db.Column(db.String(256))
    phone = db.Column(db.String(256))
    email = db.Column(db.String(256))


db.create_all()

If you need to generate fake data for the test, you can do it with the following piece of code:

from faker import Faker


def create_fake_users(n):
    """Generate fake users."""
    faker = Faker()
    for i in range(n):
        user = User(name=faker.name(),
                    age=random.randint(20, 80),
                    address=faker.address().replace('\n', ', '),
                    phone=faker.phone_number(),
                    email=faker.email())
        db.session.add(user)
    db.session.commit()
    print(f'Added {n} fake users to the database.')

And then the generated data can be sent to display in your template:

@app.route('/')
def index():
    users = User.query
    return render_template('your_template.html', title='Fake Users',
                           users=users)

And you can easily display the content in block content

{% block content %}
  <table id="data" class="table table-striped">
    <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Address</th>
        <th>Phone Number</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      {% for user in users %}
        <tr>
          <td>{{ user.name }}</td>
          <td>{{ user.age }}</td>
          <td>{{ user.address }}</td>
          <td>{{ user.phone }}</td>
          <td>{{ user.email }}</td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endblock %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.