0

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

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

3
  • This is for a SQL database, correct? Not Postgres or MySQL? Commented Oct 5, 2016 at 18:38
  • SQLAlchemy does not "implement" any of the functions you listed. func is generic (with some portability support) and produces any SQL function expression you throw at it. What DB are you using and does it have the mid function you wish to use? Also if you have an error, please include it and the complete traceback. Commented Oct 5, 2016 at 18:41
  • From the documentation "Any name can be given to func. If the function name is unknown to SQLAlchemy, it will be rendered exactly as is." Commented Oct 5, 2016 at 18:54

1 Answer 1

3

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(...))
Sign up to request clarification or add additional context in comments.

6 Comments

OperationalError: (sqlite3.OperationalError) no such function: mid [SQL: u'SELECT mid(items.title, ?, ?) LIKE ? AS anon_1 \nFROM items\n LIMIT ? OFFSET ?'] [parameters: (1, 3, '%e%', 10, 0)]
SQLite does not have mid. It has substr.
Item.query without the (func.mid()) works but it brings back a massive amount of data from the column, rather i only want between x and y
@Eddwinn I have no clue what you are trying to do. I do think you are conflating selecting (what values to fetch) and filtering (what rows to retain).
@Eddwinn So, like I said, you are conflating selecting and filtering. It sounds like you want something like db.session.query(func.substr(...)).filter(Item.description.like(...)). I also encourage you to look at the full-text search capabilities of sqlite as that will be much better suited for searching.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.