当使用SQLite时,是否有可能创建一个存储过程?


当前回答

Yet, it is possible to fake it using a dedicated table, named for your fake-sp, with an AFTER INSERT trigger. The dedicated table rows contain the parameters for your fake sp, and if it needs to return results you can have a second (poss. temp) table (with name related to the fake-sp) to contain those results. It would require two queries: first to INSERT data into the fake-sp-trigger-table, and the second to SELECT from the fake-sp-results-table, which could be empty, or have a message-field if something went wrong.

其他回答

我自己也遇到过这个问题。我认为php pdo支持存储过程,但该模块正在处理它,并构建正常的sql查询发送到sqlite。因此,在php中,可以在代码中编写存储过程,但使用它们并不能提高性能。

如果我说错了,请指正。

Yet, it is possible to fake it using a dedicated table, named for your fake-sp, with an AFTER INSERT trigger. The dedicated table rows contain the parameters for your fake sp, and if it needs to return results you can have a second (poss. temp) table (with name related to the fake-sp) to contain those results. It would require two queries: first to INSERT data into the fake-sp-trigger-table, and the second to SELECT from the fake-sp-results-table, which could be empty, or have a message-field if something went wrong.

SQLite不得不牺牲一些人认为有用的其他特性,比如高并发性、细粒度访问控制、丰富的内置函数集、存储过程、深奥的SQL语言特性、XML和/或Java扩展、兆字节或千兆字节的可伸缩性,等等

来源:适当使用SQLite

如果你仍然感兴趣,Chris Wolf用存储过程实现了SQLite的原型。您可以在他的博客文章中找到详细信息:向SQLite添加存储过程

不,但你可以:

编写长的多语句脚本 创建临时单行表,例如Vars来保存变量 在递归CTE上创建视图,以便在纯SQL查询中编程任意函数。

所以你可以做大部分你通常用存储过程做的事情。

关于如何在SQL视图中编程函数,请参阅https://www.cafe-encounter.net/p3300/pretending-that-sqlite-has-stored-procedures-and-functions。

或者你可以:

编译短的单页C程序来编写任意函数

这比你想象的要简单和省事!

详细指南请访问https://www.cafe-encounter.net/p3244/installing-and-using-sqlite-extensions-on-macos-and-maybe-windows-linux-too。这确实增加了一些部署工作:您必须在应用程序中部署额外的dll/so/dylib文件。