如何使用flask和sqlalchemy进行选择查询? [英] How to do a select query using flask and sqlalchemy?
本文介绍了如何使用flask和sqlalchemy进行选择查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是使用flask和SQLAlchemy的新手,我在postgress中有一个数据库,该数据库中有表 data的200条记录,我想执行Select语句,但是当我执行时,总是会给我同样的错误,这是我的代码:
I'm new using flask and SQLAlchemy, I have a database in postgress, this database have table "data" 200 records, I want to do a Select statment but when I do it always give me the same error, this my code:
这是我的模型。py
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Data(Base):
__tablename__ = 'data'
ids = Column(Integer(), primary_key=True)
customer_id = Column(String())
inventory_id = Column(String())
device_id = Column(String())
def toJSON(self):
json = {
"ids":self.ids,
"customer_id":self.customer_id,
"inventory_id":self.inventory_id,
"device_id":self.device_id,
}
return json
这是我的烧瓶应用程序:
This is my flask application:
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, request
from flask_cors import CORS, cross_origin
from model import Data, Base
app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost/mydatabase'
db = SQLAlchemy(app)
data = []
def index():
test_data()
return render_template("index.html",result = data)
def test_data():
sss = Base.Data.query.filter_by(ids=5).first()
print sss
app.add_url_rule('/', 'index', index)
if __name__ == '__main__':
app.run(debug = True, threaded=True)
这不起作用,并且总是有相同的错误:
This not works, and always have the same error:
AttributeError:类型为object 基本没有属性
'psirt_alerts_view'
AttributeError: type object 'Base' has no attribute 'psirt_alerts_view'
如何简单地选择?
更新:
[2017-02-14 12:02:29,980] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "c:\python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "c:\python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\python27\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "c:\python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "c:\python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "S:\ibm\ib.py", line 15, in index
test_data()
File "S:\ibm\ib.py", line 40, in test_data
sss = Base.data.query.filter_by(ids=5).first()
AttributeError: type object 'Base' has no attribute 'data'
推荐答案
db
实例具有会话对象,因此您可以执行以下查询:
db
instance has session object so you can do query like this:
db.session.query(Data).filter_by(ids=5).first()
这篇关于如何使用flask和sqlalchemy进行选择查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文