Python Flask: sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'sqlite

Question:

I am learning Python Flask and I am working to a blog as personal project. I am using a combination of Flask and Sqlite but I am stuck because it seems that my system (I am using Windows 10) is not able to find the path to the database. This is my code:

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




 
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite: ////C:/Users/admin/Desktop/Blog_Project/blog.db'

db = SQLAlchemy(app)

class Blogpost(db.Model):

    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(50))
    subtitle = db.Column(db.String(50))
    author = db.Column(db.String(20))
    date_posted = db.Column(db.DateTime)
    content = db.Column(db.Text)

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

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

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


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


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


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

@app.route('/addpost', methods=['POST'])
def addpost():
    title = request.form['title']
    subtitle = request.form['subtitle']
    author = request.form["author"]
    content = request.form['content']

    post = Blogpost(title=title, subtitle=subtitle, author=author, content=content, date_posted=datetime.now())

    db.session.add(post)
    db.session.commit()

    return redirect(url_for('index'))

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

But when I try to add a post in the corresponding webpage, I get this error:

sqlalchemy.exc.ArgumentError

sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'sqlite: ////C:/Users/admin/Desktop/Blog_Project/blog.db'

Actually the database should exist, since I see the file in my folder (the path is the one in the code)

enter image description here

Have you got any idea how I can solve the problem?

Asked By: Franz Biberkopf

||

Answers:

Try using double slashes:

sqlite: ////C:\Users\admin\Desktop\Blog_Project\blog.db

or if you want to stay on windows, use the windows formation:

sqlite: ////C:UsersadminDesktopBlog_Projectblog.db

you can learn more in this detailed answer: Windows path in Python

Answered By: shiny

app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///blog.db’

Answered By: user13825988

For the convenience of Linux and Windows users, I have summarized the solutions for this problem on Windows and Linux:

from sqlalchemy import create_engine

# relative path on Linux: with three slashes
e = create_engine('sqlite:///relative/path/to/database.db')

# absolute path on Linux: with four slashes
e = create_engine('sqlite:////absolute/path/to/database.db')

# absolute path on Windows
e = create_engine('sqlite:///C:\absolute\path\to\database.db')

For detailed documents: SQLAlchemy 1.4 Documentation

Answered By: showteth
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.