相关文章推荐
干练的面包  ·  js vue 前端element ...·  1 月前    · 
活泼的铁链  ·  借助 C++ 进行 Windows ...·  4 月前    · 
大力的枕头  ·  ERROR: Cannot create ...·  6 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I need to be able to read all the tables in an SQLite database without knowing what the tables will be called. I'm working on the following example.db

chinhook.db

Tables

Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track.

Currently, I'm using the following...

$db = new SQLite3('db/chinhook.db'); $tablesquery = $db->query("SELECT name FROM sqlite_master WHERE type='table';"); $tables = $tablesquery->fetchArray(SQLITE3_ASSOC); foreach($tables as $name){ echo $name.'<br />';

I can only ever seem to get the name of the first table. I can call data from the other tables no problem, but the sqlite_master table only contains one of the table names. Others online seem to report this method as successful. Any Ideas?

Thanks in advance.

Standard SQLite3 method for fetching the results of a query and placing them into either a numerical or associative array (or both). Similarly to the way mysql_fetch_array does. php.net/manual/en/sqlite3result.fetcharray.php Mike Aug 1, 2013 at 15:22 $db = new SQLite3('db/chinhook.db'); $tablesquery = $db->query("SELECT name FROM sqlite_master WHERE type='table';"); while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) { echo $table['name'] . '<br />'; ->fetch doesn't seem to be a method in SQLite3 Call to undefined method SQLite3Result::fetch() Mike Aug 1, 2013 at 15:20 Yeah, that was my bad. fetch() is defined in class SQLiteDatabase, but not in SQLite3 which you are using. ciruvan Aug 1, 2013 at 15:25

$db  = new PDO('sqlite:Northwind.db');
$sql = "SELECT  `name` FROM sqlite_master WHERE `type`='table'  ORDER BY name";
  $result = $db->query($sql);
      if($result){
        while($row = $result->fetch(PDO::FETCH_ASSOC)){
          echo '<li>'.$row['name'].'</li>';
   $db = new SQLite3('db/chinhook.db');
   $tablesquery = $db->query("SELECT sql FROM sqlite_master WHERE name='foo'");
   $table = $tablesquery->fetchArray();
   echo '<pre>'.$table['sql'] . '</pre>';
   $db->close();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.