0

I want to create an sqlite database file ".db": but I get an error.

from app import db
db.create_all()

but I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Никита\AppData\Local\Programs\Python\Python36\lib\site-packages
\flask_sqlalchemy\__init__.py", line 1033, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\Никита\AppData\Local\Programs\Python\Python36\lib\site-packages
\flask_sqlalchemy\__init__.py", line 1025, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Users\Никита\AppData\Local\Programs\Python\Python36\lib\site-packages
\flask_sqlalchemy\__init__.py", line 956, in get_engine
    return connector.get_engine()
  File "C:\Users\Никита\AppData\Local\Programs\Python\Python36\lib\site-packages
\flask_sqlalchemy\__init__.py", line 559, in get_engine
    sa_url = make_url(uri)
  File "C:\Users\Никита\AppData\Local\Programs\Python\Python36\lib\site-packages
\sqlalchemy\engine\url.py", line 229, in make_url
    return _parse_rfc1738_args(name_or_url)
  File "C:\Users\Никита\AppData\Local\Programs\Python\Python36\lib\site-packages
\sqlalchemy\engine\url.py", line 291, in _parse_rfc1738_args
    "Could not parse rfc1738 URL from string '%s'" % name
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'ServerDat
a.db'

My code:

from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db '
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    titel = db.Column(db.String(100), nullable=False)
    intro = db.Column(db.String(300), nullable=False)
    text = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return '<Article %r>' % self.id

@app.route('/')
def index():
    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True)

I'm just starting learning flask and Python and don't know, how to fix this.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
Nikita
  • 11
  • 2
  • 1
    The relation between the two lines of code at the beginning of your question, and the rest of you code is not clear. Also, when, exactly, do you get the error you included? Upon flask server startup? When trying to serve a page? – Amitai Irron May 10 '20 at 17:05

2 Answers2

0

You have a problem with your sqlite url. Were you to use two forward slashes instead, ('sqlite://database.db'), you would reference the working directory of the flask app.

Here is a StackOverflow post with more details about Windows paths.

Riley Pagett
  • 363
  • 1
  • 7
0

The issue could be related to the extra space at the end of your database URL. Also, as mentioned here, you might only need two / after sqlite:, not three.

Alan Kinnaman
  • 877
  • 12
  • 20