首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Flask: sqlalchemy.exc.ArgumentError:无法从字符串'sqlite‘解析rfc1738 URL

Python Flask: sqlalchemy.exc.ArgumentError:无法从字符串'sqlite‘解析rfc1738 URL
EN

Stack Overflow用户
提问于 2020-09-03 00:02:23
回答 2查看 863关注 0票数 0

我正在学习Python Flask,我正在将博客作为个人项目。我正在使用Flask和Sqlite的组合,但我被卡住了,因为我的系统(我使用的是Windows10)似乎无法找到数据库的路径。这是我的代码:

代码语言:javascript
复制
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)

但是当我尝试在相应的网页中添加帖子时,我得到了这个错误:

代码语言:javascript
复制
sqlalchemy.exc.ArgumentError

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

实际上,数据库应该存在,因为我在我的文件夹中看到了文件(路径是代码中的路径)。

你知道我该怎么解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2020-09-03 00:21:10

尝试使用双斜杠:

代码语言:javascript
复制
sqlite: ////C:\\Users\\admin\\Desktop\\Blog_Project\\blog.db

或者,如果您想继续使用windows,请使用windows stay:

代码语言:javascript
复制
sqlite: ////C:\Users\admin\Desktop\Blog_Project\blog.db

您可以在下面的详细答案中了解更多信息:Windows path in Python

票数 3
EN

Stack Overflow用户

发布于 2020-11-12 01:45:52

App.config‘’SQLALCHEMY_DATABASE_URI‘=’sqlite:/blog.db‘

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63709263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档