0

I need to create a stored procedure in memory for my unit test. I'm using python/sqlalchemy/sqlite for the unit tests. I get an error:

AttributeError: 'SQLAlchemy' object has no attribute 'executeSql'

Is there a better way to create the stored procedure using session?

db = SQLAlchemy()
class TestEscalation(unittest.TestCase):

     def setUp(self):
            app.config.from_object('config.Testing')
            self.app = app.test_client()
            with app.app_context():
                db.session.close()
                db.drop_all()
                db.create_all()
                db.session.add(lobalarmescalationtime(id=1, clientid ='AS0001',priorityid = 1,escalationtime = 120))
                db.executeSql('./dbscripts/storedproc.sql')
                db.session.commit()
            app.app_context().push()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ratha
  • 9,434
  • 17
  • 85
  • 163

2 Answers2

0

I have sorted out like; db.session.execute(open("./resources/storedproc.sql").read())

Ratha
  • 9,434
  • 17
  • 85
  • 163
0

just google these keywords: sqlalchemy execute sql

there are so many answer:

from sqlalchemy.sql import text

with engine.connect() as con:

data = ( { "id": 1, "title": "The Hobbit", "primary_author": "Tolkien" },
         { "id": 2, "title": "The Silmarillion", "primary_author": "Tolkien" },
)

statement = text("""INSERT INTO book(id, title, primary_author) VALUES(:id, :title, :primary_author)""")

for line in data:
    con.execute(statement, **line)

or

with engine.connect() as con:

rs = con.execute('SELECT * FROM book')

for row in rs:
    print row

and you can view this page on stack overflow with had answered by other guys:

How to execute raw SQL in Flask-SQLAlchemy app

enjoy

Han.Oliver
  • 525
  • 5
  • 8