【问题标题】:I have problem with database sqlite (Flask)我对数据库 sqlite (Flask) 有问题
【发布时间】:2020-05-10 15:30:58
【问题描述】:

我想创建一个 sqlite 数据库文件“.db”:但出现错误。

from app import db
db.create_all()

但我收到以下错误:

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'

我的代码:

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)

我刚开始学习烧瓶和 Python,不知道如何解决这个问题。

【问题讨论】:

  • 你问题开头的两行代码的关系,其余的代码不清楚。另外,您何时收到包含的错误?在烧瓶服务器启动时?尝试提供页面时?

标签: python sqlite flask


【解决方案1】:

您的 sqlite 网址有问题。如果您使用两个正斜杠 ('sqlite://database.db'),您将引用烧瓶应用程序的工作目录。

Here is a StackOverflow post 提供有关 Windows 路径的更多详细信息。

【讨论】:

    【解决方案2】:

    此问题可能与数据库 URL 末尾的额外空格有关。另外,作为mentioned here,您可能只需要在sqlite: 之后的两个/,而不是三个。

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 2013-05-06
      • 2020-12-23
      • 2011-05-15
      • 1970-01-01
      • 2017-10-10
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多