使用php从sqlite3数据库中检索表名

时间:2014-05-10 04:04:09

标签: php sqlite

我有以下代码的工作版本,结果为can be seen here

<?php
// Display all sqlite tables
    $db = new SQLite3('data.db');
    $tablesquery = $db->query("SELECT name FROM sqlite_master WHERE type='table';");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        echo $table['name'] . ' <br />';
    }
?>

问题是我在这个数据库USERSADMIN_LOGIN中只有两个表,但我出现了sqlite_sequence。有没有办法我只能显示表名而不是sqlite_sequence

2 个答案:

答案 0 :(得分:5)

使用PHP检查表是否已命名为sqlite_sequence,如果不是,则输出表的名称。

<?php
// Display all sqlite tables
    $db = new SQLite3('data.db');
    $tablesquery = $db->query("SELECT name FROM sqlite_master WHERE type='table';");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        if ($table['name'] != "sqlite_sequence") {
            echo $table['name'] . ' <br />';
        }
    }
?>

答案 1 :(得分:2)

如何更改初始查询以忽略sqlite_sequence作为name != 'sqlite_sequence条件?

// Display all sqlite tables
$db = new SQLite3('data.db');
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence';";
$tablesquery = $db->query($sql);

while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
    echo $table['name'] . ' <br />';
}

或者您可以使用sqlite*

跳过所有带有NOT LIKE前缀的表格
// Display all sqlite tables
$db = new SQLite3('data.db');
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite%';";
$tablesquery = $db->query($sql);

while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
    echo $table['name'] . ' <br />';
}