检索 MySQL 表注释的最优雅的方法是什么?
我正在尝试以编程方式检索 MySQL 表的注释。向我建议的第一个方法是:
$shown = $db->query('show create table ' . TABLE_NAME)->fetch_row();
preg_match("/COMMENT='(.*)'/", $shown[0], $m);
$comment = $m[1];
但这种解决方法让我感到畏缩。我偶然发现了另一种方式:
$result = $db->query("select table_comment from information_schema.tables where table_schema = '" .
DATABASE_NAME . "' and table_name = '" TABLE_NAME '\'')->fetch_row();
$comment = $result[0];
它好一点(没有字符串解析),但它仍然让我感到不舒服,因为我正在挖掘我不属于自己的内部结构。
有没有一种很好、简单的方法来获取代码中的表注释?
I'm trying to programmatically retrieve the comment for a MySQL table. The first method suggested to me was:
$shown = $db->query('show create table ' . TABLE_NAME)->fetch_row();
preg_match("/COMMENT='(.*)'/", $shown[0], $m);
$comment = $m[1];
But that kind of workaround makes me cringe. I stumbled upon another way:
$result = $db->query("select table_comment from information_schema.tables where table_schema = '" .
DATABASE_NAME . "' and table_name = '" TABLE_NAME '\'')->fetch_row();
$comment = $result[0];
It's a little better (no string parsing), but it still makes me uncomfortable because I'm digging into internal structures where I don't feel like I belong.
Is there a nice, simple way to get at the table comment in code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
信息模式实际上并不是一个你不属于的内部结构。它是 ANSI SQL 标准的一部分,其目的是为您提供查询元数据的合法方法。
我会毫不犹豫地使用它。
一个缺点是 MySQL 的信息模式实现往往性能很差 。因此,在应该快速的例程中运行针对 IS 的查询时要小心。
Information schema isn't really an internal structure where you don't belong. It's part of the ANSI SQL standard and its purpose is to give you a legitimate way to query metadata.
I would feel no hesitation to use it.
The one disadvantage is that MySQL's implementation of information schema tends to have pretty poor performance. So be careful about running queries against IS in routines that should be quick.