PDO数据库使用的SQL是独立的吗?
不同的数据库在 SQL 的实现上略有不同。 PDO 可以处理这个问题吗?
如果我编写一个与 PDO 一起使用的 SQL 查询来访问 MySQL 数据库,然后告诉 PDO 开始使用不同类型的数据库,该查询会停止工作吗?或者 PDO 会“转换”查询以使其继续工作吗?
如果 PDO 不这样做,是否有任何 PHP 库允许我根据特定语法编写 SQL,然后该库将处理 SQL 转换,以便它可以在不同的数据库上运行?
Different databases have slight variations in their implementations of SQL. Does PDO handle this?
If I write an SQL query that I use with PDO to access a MySQL database, and later tell PDO to start using a different type of database, will the query stop working? Or will PDO 'convert' the query so that it continues to work?
If PDO does not do this, are there any PHP libraries that allow me to write SQL according to a particular syntax, and then the library will handle converting the SQL so that it will run on different databases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 PHP 手册:
因此,您不能更改数据库并期望一切都像以前一样工作。这取决于您使用的查询。它们是“简单”的 SQL92 查询还是对特定数据库使用特殊功能...
例如,带有“LIMIT 10,20”的 mysql 查询必须重写才能与 Oracle DB 或 Sqlite 一起使用。他们使用“LIMIT 20 OFFSET 10”
From PHP manual :
So,you can not change the database and expect that everything works as before. It depends on the queries you have used. Are they "simple" SQL92 queries or do they use special features for a specific db...
Ex a mysql query with "LIMIT 10,20" must be rewritting to work with an Oracle DB or Sqlite. They use "LIMIT 20 OFFSET 10"
PHP 没有可以自动转换 SQL 的库。如果您想要这种功能,您应该查看 ORM 实现,例如 Doctrine。当然这是要付出代价的,因为在项目中使用它需要一个学习曲线,而且编写 SQL 不再像生成字符串那么简单。您应该问自己是否绝对需要独立于数据库的代码。
PHP doesn't have libraries that will automatically convert SQL for you. If you want that kind of functionality you should look at an ORM implementation like Doctrine. There is a price to pay of course, since there is a learning curve involved in using it in your project, plus writting SQL stops being as simple as churning out a string. You should ask yourself if you absolutely positively need code that's database independent.