0

Ok, so I've got the following error when I try to login using my form:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

The code I've used to create my class for the database is:

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(30), unique=True)
    email = db.Column(db.String(60), unique=True)
    password = db.Column(db.String(50))

The initialization and configuration for my database(I've used SQLAlchemy) is the following piece of code:

movie_recommendation.config['SECRET_KEY'] = secret_key
movie_recommendation.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///c/Users/gheor/Documents/login-example/database.db'
bootstrap = Bootstrap(movie_recommendation)
db = SQLAlchemy(movie_recommendation)
login_manager = LoginManager()
login_manager.init_app(movie_recommendation)
login_manager.login_view = 'login'

Also, the code for register and login forms is the following(note: this seems to work just fine, it seems to be an issue just regarding my database) :

class LoginForm(FlaskForm):
    username = StringField('Username', validators=[InputRequired(), Length(min=10, max=30)])
    password = PasswordField('Password', validators=[InputRequired(), Length(min=10, max=50)])
    remember = BooleanField('Remember me!')

class RegisterForm(FlaskForm):
    email = StringField('Email', validators=[InputRequired(), Length(max=60)])
    username = StringField('Username', validators=[InputRequired(), Length(min=10, max=30)])
    password = PasswordField('Password', validators=[InputRequired(), Length(min=10, max=50)])

I apologize if this is a dumb question, but I'm pretty newbie using Flask and SQLAlchemy, and I really don't know how to modify that code to work fine. So any help will me really appreciated.

Andrei GH
  • 53
  • 1
  • 5
  • Unless you're calling `my_declarative_base.metadata.create_all(engine)`, to initially create the database, the file/tables won't be created and so a connection to it will fail. – Stephen May 25 '21 at 19:52
  • hello @Stephen, so I need to modify like: db.metada.create_all(movie_recommendation)? – Andrei GH May 25 '21 at 19:55
  • As long as db is the `declarative_base` of your model and `movie_recommendation` was returned from `create_engine`. See this question for more info: https://stackoverflow.com/questions/16284537/sqlalchemy-creating-an-sqlite-database-if-it-doesnt-exist – Stephen May 25 '21 at 20:00
  • Ok, so I didn't have any luck :( I got this error now: AttributeError: module 'config' has no attribute 'engine_base', when i try to do this Base.metadata.create_all(config.engine_base), after I renamed some things in my code – Andrei GH May 25 '21 at 20:14

0 Answers0