我正在学习Python Flask,我正在将博客作为个人项目。我正在使用Flask和Sqlite的组合,但我被卡住了,因为我的系统(我使用的是Windows10)似乎无法找到数据库的路径。这是我的代码:
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)但是当我尝试在相应的网页中添加帖子时,我得到了这个错误:
sqlalchemy.exc.ArgumentError
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'sqlite: ////C:/Users/admin/Desktop/Blog_Project/blog.db'实际上,数据库应该存在,因为我在我的文件夹中看到了文件(路径是代码中的路径)。

你知道我该怎么解决这个问题吗?
发布于 2020-09-03 00:21:10
尝试使用双斜杠:
sqlite: ////C:\\Users\\admin\\Desktop\\Blog_Project\\blog.db或者,如果您想继续使用windows,请使用windows stay:
sqlite: ////C:\Users\admin\Desktop\Blog_Project\blog.db您可以在下面的详细答案中了解更多信息:Windows path in Python
发布于 2020-11-12 01:45:52
App.config‘’SQLALCHEMY_DATABASE_URI‘=’sqlite:/blog.db‘
https://stackoverflow.com/questions/63709263
复制相似问题