【发布时间】:2020-09-17 14:34:43
【问题描述】:
我正在尝试使用 sqlite 和 PHP 在表上显示索引。
此处的手册 (https://www.sqlite.org/cli.html) 建议:
.indexes ?TABLE?
但是,使用
$pdo->query('.indexes my_table');
演出:
PDOException: SQLSTATE[HY000]: General error: 1 near ".": syntax error
是否可以使用 PHP 和 PDO 执行这些点命令?
编辑:示例代码:
$this->pdo->query('DROP TABLE IF EXISTS test');
$this->pdo->query('CREATE TABLE test (id INT PRIMARY KEY NOT NULL, col1 text NOT NULL, col2 text NOT NULL)');
$this->pdo->query('CREATE INDEX col1 ON test (col1)');
$result = $this->pdo->query('PRAGMA table_info(test);')->fetchAll(\PDO::FETCH_OBJ);
print_r($result);
这不显示索引。以下错误:
$this->pdo->query('DROP TABLE IF EXISTS test');
$this->pdo->query('CREATE TABLE test (id INT PRIMARY KEY NOT NULL, col1 text NOT NULL, col2 text NOT NULL)');
$this->pdo->query('CREATE INDEX col1 ON test (col1)');
$result = $this->pdo->query('.indexes test')->fetchAll(\PDO::FETCH_OBJ);
print_r($result);
这会返回一个空数组:
$this->pdo->query('DROP TABLE IF EXISTS test');
$this->pdo->query('CREATE TABLE test (id INT PRIMARY KEY NOT NULL, col1 text NOT NULL, col2 text NOT NULL)');
$this->pdo->query('CREATE INDEX index_col1 ON test (col1)');
$result = $this->pdo->query('PRAGMA index_info(test);')->fetchAll(\PDO::FETCH_OBJ);
print_r($result);
【问题讨论】:
-
不幸的是,它不显示索引,只显示列类型。我已经用示例代码更新了问题
-
哦,对不起。以为那是等价的?
PRAGMA index_list(table-name)呢?我已将其添加到我的答案中 -
啊哈!谢谢!是的,我尝试过在其他地方看到过的 index_info,但没有奏效。命令是 index_list。