SQLite v2.8 (PHP5) 中列的元数据
如何使用 PHP5(如 MySql 的 mysql_fetch_field)获取 SQLite v2.8 表中每一列的元数据/约束(特别是主键和“允许空”)?
sqlite_fetch_column_types
(OO:$db->fetchColumnTypes
)仅获取列名称和数据类型:
http://dk.php.net/manual/ en/function.sqlite-fetch-column-types.php
SQLITE_MASTER 有信息 - 但不是作为变量。示例:SELECT name FROM SQLITE_MASTER;
... SQLITE_MASTER 仅输出具有此结构的数组 (v2.8):(
[type] => table
[name] => foo
[tbl_name] => foo
[rootpage] => 3
[sql] => CREATE TABLE foo ( id INTEGER PRIMARY KEY, name CHAR(255) )
什么是“rootpage”?)
How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)?
sqlite_fetch_column_types
(OO: $db->fetchColumnTypes
) only gets column name and datatype:
http://dk.php.net/manual/en/function.sqlite-fetch-column-types.php
SQLITE_MASTER has the info - but not as a variable. Example:SELECT name FROM SQLITE_MASTER;
... SQLITE_MASTER only outputs an array with this structure (v2.8):
[type] => table
[name] => foo
[tbl_name] => foo
[rootpage] => 3
[sql] => CREATE TABLE foo ( id INTEGER PRIMARY KEY, name CHAR(255) )
(And what is "rootpage"?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我在 Zend Framework 上工作时,我必须这样做才能为 SQLite 实现一个
describeTable()
方法。 SQLite 没有任何方法来获取有关元数据的非常详细的信息。我所做的是运行以下 SQLite 查询:
请参阅 http://www.sqlite.org/pragma。 html,标题“查询数据库模式的指令”下。
该查询返回的结果集包含以下列:
您可以查看通过下载 Zend Framework 并查看类
Zend_Db_Adapter_Pdo_Sqlite
的方法describeTable()
来获取 PHP 代码。您还可以通过 Framework.zend.com 上的代码存储库浏览器在线查看源代码(尽管它经常不起作用)。FWIW,这与 mysql_fetch_field() 不同。该方法返回有关结果集的元数据,这可能与有关表的元数据不同。
I had to do this to implement a
describeTable()
method for SQLite when I worked on Zend Framework. SQLite does not have any way to get very detailed information about metadata.What I did was to run the following SQLite query:
See http://www.sqlite.org/pragma.html, under the heading "Pragmas to query the database schema."
The result set returned from this query has columns for:
You can view the PHP code by downloading Zend Framework and looking in class
Zend_Db_Adapter_Pdo_Sqlite
, methoddescribeTable()
. You can also view the source online via the code repository browser at framework.zend.com (although it is frequently not working).FWIW, this is not like
mysql_fetch_field()
. That method returns metadata about a result set, which may not be the same thing as metadata about a table.对我有用的示例代码片段......来说明 Bill Karwin 的解决方案(这并不是试图回答我自己的问题!)
Example code snippet that worked for me ... to illustrate Bill Karwin's solution (it's not an attempt to answer my own question!)