1

I have a working version of this code as below which the result 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 />';
    }
?>

The issue is I only have two tables in this database USERS and ADMIN_LOGIN but I am getting sqlite_sequence appearing. Is there a way I can only show the table names and not sqlite_sequence?

2 Answers 2

5

Use PHP to check if the table is named sqlite_sequence, and if it isn't then output the table's name.

<?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 />';
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

2

What about changing your initial query to ignore sqlite_sequence as a name != 'sqlite_sequence condition?

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

Or you can skip all tables with sqlite* prefix like this using 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 />';
}

1 Comment

You should skip all tables beginning with sqlite_ (such as the sqlite_statX tables).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.