2

I have developed a simple flask app locally where I was using SQLite database to perform the login. Now I have deployed it to Heroku and realized that Heroku doesn't support SQLite, that's why now I need to change it to another database.

Here's what I have done with SQLite:

Here's how i have created table:

from sqlalchemy import create_engine
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///tutorial.db', echo=True)
Base = declarative_base()

# Create USER model

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    username = Column(String)
    password = Column(String)

    def __init__(self, username, password):
        self.username = username
        self.password = password

Base.metadata.create_all(engine)

Here's how i'm using SQlite for login:

from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///tutorial.db', echo=True)

@app.route('/login', methods=['GET', 'POST'])
def lodadata():
    POST_USERNAME = str(request.form['uid'])
    POST_PASSWORD = str(request.form['upass'])

    session = sessionmaker(bind=engine)
    s = session()
    query = s.query(User).filter(User.username.in_([POST_USERNAME]),
                             User.password.in_([POST_PASSWORD]))
    result = query.first()
    if result:
        session['logged_in'] = True
    else:
        flash('Something wrong about Login Detail')

How can I change my database to another DB which supported by Heroku?

Help me, please!

Thanks in advance!

Abdul Rehman
  • 5,326
  • 9
  • 77
  • 150
  • Make dump of sqlite, import into postgres, change engine in your app. By the way, you can do it locally for testing. [this can help you](https://www.quora.com/What-is-the-best-way-to-copy-my-SQLite-database-to-PostgreSQL-so-I-can-deploy-it-to-Heroku), and [this](https://stackoverflow.com/questions/4581727/convert-sqlite-sql-dump-file-to-postgresql) – Danila Ganchar Feb 16 '18 at 08:19
  • There is 2 point in your question, are you looking for help to migrate your python code or your sql database ? For python part first have a look at http://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql to use the right engine. For your database it depends if you need to keep your data or not. – Rodolphe Feb 16 '18 at 12:39

0 Answers0