Search code examples
pythonmysqldatabasesearchcleardb

Searching a MySQL Database on ClearDB using Python script


We have built a series a forms using PHP that populate a MySQL Database and after learning more Python want to begin transitioning our whole web app over to a python back end instead of PHP. Can anyone offer a quick intro into searching a MySQL DB using Python?


Solution

  • The easiest way is to abstract the database using an ORM. I found that the python package SQLAlchemy works fantastically.

    A ORM let's you treat the database objects as normal python objects. Most queries can be abstracted and for 99% of cases you won't need to write SQL queries. Also this makes transition between database technologies very simple.

    Go check it out on:

    http://www.sqlalchemy.org/

    A search query would be something like:

    session.query(User).filter_by(first_name='bob')
    

    Now you will have all users with the first name 'bob'