简体   繁体   English

sqlalchemy-flask 应用程序中的属性错误(键),无法连接到数据库

[英]attribute error (key) in sqlalchemy-flask application, cant connect to database

this is my first time asking a question on stackoverflow.这是我第一次在 stackoverflow 上提问。 Im in a data science bootcamp and I missed a class and I am going through the recording and trying to follow along with the instructor showing us how to reference a sqlite database in a flask app, but I cant get the example running.我在数据科学训练营中,我错过了 class,我正在查看录音并尝试跟随讲师向我们展示如何在 Z319C3206A7F10C17C3B9116D4A957 中引用 sqlite 数据库,但我无法运行示例。 Im able to run flask apps that dont reference sqlite though.我能够运行不引用 sqlite 的 flask 应用程序。 For example, when i try and run python code that begins with this:例如,当我尝试运行以此开头的 python 代码时:

import numpy as np

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func

from flask import Flask, jsonify


#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///titanic.sqlite")

# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)

# Save reference to the table
Passenger = Base.classes.passenger

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

im greeted with an error that says我遇到了一个错误,上面写着

Traceback (most recent call last):
  File "C:\Users\frcon\Anaconda3\lib\site-packages\sqlalchemy\util\_collections.py", line 210, in __getattr__
    return self._data[key]
KeyError: 'passenger'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:/Users/frcon/Desktop/pythonstuff/RU-JER-DATA-PT-01-2020/01-Lesson-Plans/10-Advanced-Data-Storage-and-Retrieval/3/Activities/10-Ins_Flask_with_ORM/Solved/app.py", line 22, in <module>
    Passenger = Base.classes.passenger
  File "C:\Users\frcon\Anaconda3\lib\site-packages\sqlalchemy\util\_collections.py", line 212, in __getattr__
    raise AttributeError(key)
AttributeError: passenger

I guess it means that it cant find the "passenger" table but im sure it exists, I checked the database.我猜这意味着它找不到“乘客”表,但我确定它存在,我检查了数据库。 In the recording of class, my professor was able to run this code without modifying anything.在 class 的录音中,我的教授无需修改任何内容即可运行此代码。 Any help is super appreciate.任何帮助都非常感谢。 Thanks!谢谢!

Is the passenger table spelled the same in titanic.sqlite file they way you are referencing it?乘客表在titanic.sqlite文件中的拼写是否与您引用的方式相同?

You can run the following in jupyter notebook with the same dependencies.您可以在具有相同依赖项的 jupyter notebook 中运行以下命令。

engine = create_engine("sqlite:///titanic.sqlite")
Base = automap_base()
Base.prepare(engine, reflect=True)

# This should return the table names
Base.classes.keys()

My issue was that i needed to import os:)我的问题是我需要导入操作系统:)

engine = create_engine("sqlite:///titanic.sqlite") engine = create_engine("sqlite:///titanic.sqlite")

The issue is in above line: add full path like "c:/user/myname/desktop/myapp/titanic.sqlite" instead of just "titanic.sqlite"问题出在上面一行:添加完整路径,如“c:/user/myname/desktop/myapp/titanic.sqlite”,而不仅仅是“titanic.sqlite”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM