2

I mainly use raw sqlite expressions in my flask apps. I recently started to play with flask-SQLAlchemy. I would like to know what is the main advantage to use sqlalchemy over RAW sqlite expressions in my app. I know that with ORM I am not locked in one database engine and I can switch to Mysql for example without changing ORM code. Are there any other advantages?

In my flask app I use:

from flask import g
import sqlite3

@app.before_request
def before_request():
    g.db = sqlite3.connect(DATA_DIR + "/myDB.db")

@app.teardown_request
def teardown_request(exception):
    if hasattr(g, 'db'):
        g.db.close() 

In this case when I want to change DB structure I simply use smart app like Navicat and then I update raw statements. In SQLAlchemy it is little pain to change structure. Thank you very much in advance.

TomRavn
  • 1,134
  • 14
  • 30
  • 2
    It's an abstraction layer on top of the DBAPI. There are tools like [alembic](http://alembic.zzzcomputing.com/en/latest/) that you can use with sqlalchemy to manage migrations. Anything beyond this seems primarily opinion based. – abigperson Feb 17 '18 at 19:24
  • Hi, yes I know about these tools, but is there any advantage in performace or anything else? Thanks – TomRavn Feb 17 '18 at 19:49
  • 2
    Possible duplicate of [Why should you use an ORM?](https://stackoverflow.com/questions/448684/why-should-you-use-an-orm) – mirhossein Feb 18 '18 at 20:16

1 Answers1

1

from Why should you use an ORM?

Making data access more abstract and portable. ORM implementation classes know how to write vendor-specific SQL, so you don't have to. (Bill Karwin)

mirhossein
  • 682
  • 7
  • 16
  • 1
    Why not flag as a duplicate, if you feel that the answer fits verbatim? – Ilja Everilä Feb 18 '18 at 17:06
  • sorry, did not know i can flag it – mirhossein Feb 18 '18 at 20:16
  • 1
    Ok it looks like only advantage is portability of code between engines, I thought there is something more... This question cost me 6- points nice:) thank you – TomRavn Feb 19 '18 at 04:22
  • 2
    Few months later after playing with SQLALCHEMY I can say portability or switching between DB engines is not only one advantage. I am not db specialist and in SQLAlchemy I am able to write quite difficult DB queries, which would be quite advanced in RAW SQL. Further there is benefit that we are protected against SQL injection attacks, more details on this link - https://stackoverflow.com/questions/31949733/is-a-sqlalchemy-query-vulnerable-to-injection-attacks – TomRavn Jun 29 '18 at 01:38