PHP PDO + Sqlite,在表上显示索引

时间:2015-08-07 14:44:48

标签: php sqlite pdo

我正在尝试使用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);

2 个答案:

答案 0 :(得分:3)

  

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

不,它们只能在SQLite命令行界面中使用。

  

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

PRAGMA index_list(table-name)

答案 1 :(得分:0)

-- 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';