Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在sqlalchemy中执行存储过程,在内存中执行sqllite?_Python_Sqlite_Sqlalchemy - Fatal编程技术网

Python 如何在sqlalchemy中执行存储过程,在内存中执行sqllite?

Python 如何在sqlalchemy中执行存储过程,在内存中执行sqllite?,python,sqlite,sqlalchemy,Python,Sqlite,Sqlalchemy,我需要在内存中为我的单元测试创建一个存储过程。我使用python/sqlalchemy/sqlite进行单元测试。我得到一个错误: AttributeError:'SQLAlchemy'对象没有属性'executeSql' 是否有更好的方法使用会话创建存储过程 db = SQLAlchemy() class TestEscalation(unittest.TestCase): def setUp(self): app.config.from_object('c

我需要在内存中为我的单元测试创建一个存储过程。我使用python/sqlalchemy/sqlite进行单元测试。我得到一个错误:

AttributeError:'SQLAlchemy'对象没有属性'executeSql'

是否有更好的方法使用会话创建存储过程

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()
我已经整理好像;
db.session.execute(打开(“./resources/storedproc.sql”).read()

只需搜索这些关键字:sqlalchemy执行sql

答案有很多:

from sqlalchemy.sql import text
将engine.connect()作为con:

您可以在stack overflow上查看此页面,其他人已回答:

享受

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)
with engine.connect() as con:

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

for row in rs:
    print row