Python Flask Sqlalchemy Subst Query
Question: I am working on an internel search engine at my company written in python
utilizing flask and sqlalchemy(sqlite). My current problem is that I would
like to.

A.) Query on a certain amount of information for the description field B.)
Preferable query before it 50 characters and after it.

Very similiar to google under the link field. If you search for something, it
returns the links with 100 characters of words below that.

I was reading the documentation and found that there is no mid() function in
sqlalchemy. I also noticed from this post that sqlalchemy only support max,
min, and avg [sqlalchemy: get max/min/avg values from a
table](http://stackoverflow.com/questions/7133007/sqlalchemy-get-max-min-avg-
values-from-a-table)

SQL Documentation of functions
<http://docs.sqlalchemy.org/en/latest/core/functions.html>

I was trying to implement a query such as

    
    
    links = Item.query(func.mid(Item.description, 0, 200).like('%helloworld%'))
    

I releazed sqlite has the syntax Substr and have tried
Item.query.filter(func.substr(Item.description,0, 200) == '%helloworld%')

Is there a way in sqlalchemy to navigate around this issue?

My code:

    
    
    from sqlalchemy.sql.functions import func
    
    def mainSearch(searchterm):
        links = Item.query(func.mid(Item.title, 1, 3).Item.title.like('%e%'))
        return links
    

HTML/Jinja code:

    
    
     {% for link in links.items %}
    
    <div id="resultbox">
    
             <div id="linkTitle"><h4><a href="{{ link.link }}">{{ link.title }}</a></h4> </div>
             <div id="lastUpdated">Last Updated: {{ link.last_updated }} </div>
             <div id="linkLink">{{ link.link }}</div>
                 <div id="linkDescription">{{ link.description | safe }}</div>
    </div>
    

Error

TypeError: 'BaseQuery' object is not callable

My database: Sqlite

I wanted to a query in sql similar too:

    
    
    SELECT MID(column_name,start,length) AS some_name FROM table_name; 
    

**Overall I am trying to do this to the data we query in Column Description:**

**Example text:**

An article (abbreviated to ART) is a word (prefix or suffix) that is used
alongside a noun to indicate the type of reference being made by the noun.
Articles specify grammatical definiteness of the noun, in some languages
extending to volume or numerical scope. The articles in the English language
are the and a/an, and (in certain contexts) some. "An" and "a" are modern
forms of the Old English "an", which in Anglian dialects was the number "one"
(compare "on", in Saxon dialects) and survived into Modern Scots as the number
"owan". Both "on" (respelled "one" by the Normans) and "an" survived into
Modern English, with "one" used as the number and "an" ("a", before nouns that
begin with a consonant sound) as an indefinite article.

In many languages, articles are a special part of speech, which cannot easily
be combined with other parts of speech. In English, articles are frequently
considered a part of a broader speech category called determiners, which
combines articles and demonstratives (such as "this" and "that").

In languages that employ articles, every common noun, with some exceptions, is
expressed with a certain definiteness (e.g., definite or indefinite), just as
many languages express every noun with a certain grammatical number (e.g.,
singular or plural). Every noun must be accompanied by the article, if any,
corresponding to its definiteness, and the lack of an article (considered a
zero article) itself specifies a certain definiteness. This is in contrast to
other adjectives and determiners, which are typically optional. This
obligatory nature of articles makes them among the most common words in many
languages—in English, for example, the most frequent word is the.[1]

Articles are usually characterized as either definite or indefinite.[2] A few
languages with well-developed systems of articles may distinguish additional
subtypes. Within each type, languages may have various forms of each article,
according to grammatical attributes such as gender, number, or case, or
according to adjacent sounds.

**To this**

An article (abbreviated to ART) is a word (prefix or suffix) that is used
alongside a noun to indicate the type of reference being made by the noun.
Articles specify grammatical definiteness of the noun,

So it doesnt crash the database by grabbing text of 100,000 words long. I only
need the first 100


Answer: This has nothing to do with the `mid` function. The error message says
`'BaseQuery' object is not callable.` Where are you calling `BaseQuery`? Here:

    
    
    Item.query(...)
    

The correct incantation is:

    
    
    db.session.query(func.mid(...))
    

or

    
    
    Item.query.with_entities(func.mid(...))
    

