Skip to content

Instantly share code, notes, and snippets.

@dapepe
Created March 24, 2016 07:15
Show Gist options
  • Save dapepe/e9d0ec33037b3f8e0532 to your computer and use it in GitHub Desktop.
Save dapepe/e9d0ec33037b3f8e0532 to your computer and use it in GitHub Desktop.
Display a dump for an SQLite file
#!/usr/bin/env php
<?php
if ( !isset($argv[1]) ) {
fputs(STDERR, 'Usage: '.basename($argv[0]).' <sqlite-file>'."\n");
exit(1);
}
$dbFile = $argv[1];
$con = new PDO('sqlite:'.$dbFile);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $con->query('SELECT * FROM SQLITE_MASTER');
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();
$statement = null;
foreach ($rows as $row) {
if ( !isset($row['type'], $row['name'], $row['sql']) or
($row['type'] !== 'table') )
continue;
echo $row['sql'].";\n\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment