我正在尝试使用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);
是否可以使用PHP和PDO来执行这些点命令?
不,它们只在SQLite命令行界面中工作。
我正在尝试使用sqlite和PHP显示表上的索引。
PRAGMA index_list(table-name)
-- 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';