0

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?

3 Answers 3

3

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:\Users\admin\Desktop\Blog_Project\blog.db

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

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

Comments

1

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

Comments

-1

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

2 Comments

Just want to remind your guys, there are THREE forward slash (/) after sqlite rather than two
I see similar solution on github with a lot of votes. github.com/miguelgrinberg/flasky/issues/…

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.