【问题标题】:PHP PDO + Sqlite, show indexes on tablePHP PDO + Sqlite,在表上显示索引
【发布时间】: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。

标签: php sqlite pdo


【解决方案1】:

是否可以使用 PHP 和 PDO 执行这些点命令?

不,它们只能在 SQLite 命令行界面中工作。

我正在尝试使用 sqlite 和 PHP 在表上显示索引。

PRAGMA index_list(table-name)

【讨论】:

    【解决方案2】:
    -- List all indexes
    SELECT * FROM sqlite_master WHERE type = 'index';
    -- List all indexes on table XXX
    SELECT * FROM sqlite_master WHERE type = 'index' and tbl_name = 'XXX';
    

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 2023-01-03
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2013-04-02
      • 2017-03-29
      • 1970-01-01
      相关资源
      最近更新 更多