Zend Framework:开始使用 SQLite

发布于 2024-07-16 09:39:20 字数 168 浏览 5 评论 0原文

抱歉,如果这过于简单了。

我决定使用 SQLite 数据库而不是 MySQL 数据库。 我正在尝试了解 SQLite 是多么简单,并且想要一个简单的单答案教程,介绍如何将 SQLite 与 Zend Framework 一起使用、将 SQLite 数据库放在目录结构中的何处、如何创建数据库等。 。

Sorry if this is overly simplistic.

I've decided that I want to use an SQLite database instead of a MySQL database. I'm trying to wrap my head around how simple SQLite is and would like a simple, one answer tutorial on how to use SQLite with the Zend Framework, where to put my SQLite database in my directory structure, how to create the database, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尴尬癌患者 2024-07-23 09:39:20

@tuinstoel 是正确的,如果 SQLite 数据库不存在,则附加到它会隐式创建它。

SQLite 还支持或多或少类似于 MySQL 的命令 shell 的命令行客户端,允许您发出临时命令或运行 SQL 脚本。 请参阅此处的文档: http://www.sqlite.org/sqlite.html

当然你需要更改 ZF 应用程序中的 Zend_Db 适配器。 ZF 仅支持 PDO SQLite 扩展的适配器。 SQLite 不支持用户/密码凭据。 另外,由于 SQLite 是嵌入式数据库而不是客户端/服务器,因此“主机”参数毫无意义。

$db = Zend_Db::factory("pdo_sqlite", array("dbname"=>"/path/to/mydatabase.db"));

还有一点需要注意:当您获得关联数组格式的查询结果时,某些版本的 SQLite 坚持使用“tablename.columnname”作为数组中的键,而其他品牌的数据库则将键返回为简单的“columnname”。 ZF 中有一个关于此的突出错误,试图补偿并使 SQLite 与其他适配器的行为一致,但该错误尚未解决。

@tuinstoel is correct, attaching to an SQLite database implicitly creates it if it does not exist.

SQLite also supports a command-line client that is more or less like MySQL's command shell, allowing you to issue ad hoc commands or run SQL scripts. See documentation here: http://www.sqlite.org/sqlite.html

Of course you need to change the Zend_Db adapter in your ZF application. ZF supports only an adapter to the PDO SQLite extension. SQLite doesn't support user/password credentials. Also since SQLite is an embedded database instead of client/server, the "host" parameter is meaningless.

$db = Zend_Db::factory("pdo_sqlite", array("dbname"=>"/path/to/mydatabase.db"));

One more caveat: when you get query results in associative-array format, some versions of SQLite insist on using "tablename.columnname" as the keys in the array, whereas other brands of database return keys as simply "columnname". There's an outstanding bug in ZF about this, to try to compensate and make SQLite behave consistently with the other adapters, but the bug is unresolved.

半寸时光 2024-07-23 09:39:20

如果您连接到不存在的数据库,则会动态创建数据库。 (您可以关闭此行为)

If you make a connection to a not existing database, a database is created on the fly. (You can turn this behavour off)

明明#如月 2024-07-23 09:39:20

Zend Framework 快速入门教程(撰写本文时版本为 1.9.5)。 只需创建一个新项目(使用 zf 命令行工具。在这里查看一个很棒的教程设置),将这些行添加到您的 config.ini 文件中,然后您就可以开始了:

; application/configs/application.ini
[production]
resources.db.adapter       = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/databaseName.db"

现在,当您请求默认数据库适配器时,它将使用此适配器。 我还建议下载快速入门教程源代码并使用 load.sqlite.php 脚本。 您可以创建架构和数据文件,并使用这些表/列/值加载数据库。 这非常有帮助! 只需查看教程。 一切都在那里。


这个答案已从问题中移出,变成了 CW 答案,以否认对内容的所有权

This is now covered in the Zend Framework quickstart tutorial (version 1.9.5 as of this writing). Just make a new project (with zf command line tool. look here for a great tutorial on setting it up), add these lines to your config.ini file and you're good to go:

; application/configs/application.ini
[production]
resources.db.adapter       = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/databaseName.db"

Now when you ask for your default database adapter, it will use this one. I would also recommend downloading the quickstart tutorial source code and making use of the load.sqlite.php script. You can create a schema and data file and load the database with these tables/columns/values. It's very helpful! Just check out the tutorial. It's all in there.


This answer was moved out of the question into a CW answer to disavow ownership over the content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文