如何从flask-sqlalchemy中的表中查询一列或两列?

时间:2018-03-03 07:10:32

标签: python-3.x sqlite flask-sqlalchemy

我有一张表说CATEGORY,其中一列是CATEGORY_NAME。 我想查询表中的所有category_names。

在SQL中我会这样做 -

class ListIterator<T> implements Iterator<T> {
private ListNode<T> current;

public ListIterator(ListNode<T> node) {
    current = node;
}

//Move to the next position
public T next() throws NoSuchElementException {
    if (current==null) {
        throw new NoSuchElementException();
    } else {
        current = current.next;
    }
    return current.payload;
}

//Makes sure there is another spot to move to.
public boolean hasNext() {
    boolean freeSpace;

    if (current.next == null) {
        freeSpace = false;
    } else {
        freeSpace = true;
    }

    return freeSpace;
}

我如何在SELECT CATEGORY_NAME FROM CATEGORY; 中执行此操作?

我的模特是:

flask-sqlalchemy

我知道我们可以使用class Category(db.Model): id = db.Column(db.Integer, primary_key=True) category_name = db.Column(db.String(64), index=True, unique=True) 执行此操作,但它无法正常工作。

1 个答案:

答案 0 :(得分:0)

它以with_entities

的方式工作
cat_names = Category.query.with_entities(Category.category_name).all()
相关问题