Python Flask Sqlalchemy Subst Query
<p>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.</p>

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

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

<p>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
<a href="http://stackoverflow.com/questions/7133007/sqlalchemy-get-max-min-avg-values-from-a-table">sqlalchemy: get max/min/avg values from a table</a></p>

<p>SQL Documentation of functions
<a href="http://docs.sqlalchemy.org/en/latest/core/functions.html" rel="nofollow">http://docs.sqlalchemy.org/en/latest/core/functions.html</a></p>

<p>I was trying to implement a query such as</p>

<pre><code>links = Item.query(func.mid(Item.description, 0, 200).like('%helloworld%'))
</code></pre>

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

<p>Is there a way in sqlalchemy to navigate around this issue?  </p>

<p>My code:</p>

<pre><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
</code></pre>

<p>HTML/Jinja code:</p>

<pre><code> {% for link in links.items %}

&lt;div id="resultbox"&gt;

         &lt;div id="linkTitle"&gt;&lt;h4&gt;&lt;a href="{{ link.link }}"&gt;{{ link.title }}&lt;/a&gt;&lt;/h4&gt; &lt;/div&gt;
         &lt;div id="lastUpdated"&gt;Last Updated: {{ link.last_updated }} &lt;/div&gt;
         &lt;div id="linkLink"&gt;{{ link.link }}&lt;/div&gt;
             &lt;div id="linkDescription"&gt;{{ link.description | safe }}&lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Error</p>

<p>TypeError: 'BaseQuery' object is not callable</p>

<p>My database:
Sqlite</p>

<p>I wanted to a query in sql similar too:</p>

<pre><code>SELECT MID(column_name,start,length) AS some_name FROM table_name; 
</code></pre>

<p><strong>Overall I am trying to do this to the data we query in Column Description:</strong></p>

<p><strong>Example text:</strong></p>

<p>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.</p>

<p>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").</p>

<p>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]</p>

<p>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.</p>

<p><strong>To this</strong></p>

<p>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,</p>

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

<p>This has nothing to do with the <code>mid</code> function. The error message says <code>'BaseQuery' object is not callable.</code> Where are you calling <code>BaseQuery</code>? Here:</p>

<pre><code>Item.query(...)
</code></pre>

<p>The correct incantation is:</p>

<pre><code>db.session.query(func.mid(...))
</code></pre>

<p>or</p>

<pre><code>Item.query.with_entities(func.mid(...))
</code></pre>

python sql sqlalchemy flask-sqlalchemy