How can one see the structure of a table in SQLite?

Sqlite

Sqlite Problem Overview


How can I see the structure of table in SQLite as desc was in Oracle?

Sqlite Solutions


Solution 1 - Sqlite

PRAGMA table_info(table_name);

This will work for both: command-line and when executed against a connected database.

A link for more details and example. thanks SQLite Pragma Command

Solution 2 - Sqlite

Invoke the sqlite3 utility on the database file, and use its special dot commands:

  • .tables will list tables
  • .schema [tablename] will show the CREATE statement(s) for a table or tables

There are many other useful builtin dot commands -- see the documentation at http://www.sqlite.org/sqlite.html, section Special commands to sqlite3.

Example:

sqlite> entropy:~/Library/Mail>sqlite3 Envelope\ Index
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
addresses              ews_folders            subjects
alarms                 feeds                  threads
associations           mailboxes              todo_notes
attachments            messages               todos
calendars              properties             todos_deleted_log
events                 recipients             todos_server_snapshot
sqlite> .schema alarms
CREATE TABLE alarms (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, alarm_id,
                     todo INTEGER, flags INTEGER, offset_days INTEGER,
                     reminder_date INTEGER, time INTEGER, argument,
                     unrecognized_data BLOB);
CREATE INDEX alarm_id_index ON alarms(alarm_id);
CREATE INDEX alarm_todo_index ON alarms(todo);

Note also that SQLite saves the schema and all information about tables in the database itself, in a magic table named sqlite_master, and it's also possible to execute normal SQL queries against that table. For example, the documentation link above shows how to derive the behavior of the .schema and .tables commands, using normal SQL commands (see section: Querying the database schema).

Solution 3 - Sqlite

You can query sqlite_master

SELECT sql FROM sqlite_master WHERE name='foo';

which will return a create table SQL statement, for example:

$ sqlite3 mydb.sqlite
sqlite> create table foo (id int primary key, name varchar(10));
sqlite> select sql from sqlite_master where name='foo';
CREATE TABLE foo (id int primary key, name varchar(10))

sqlite> .schema foo
CREATE TABLE foo (id int primary key, name varchar(10));

sqlite> pragma table_info(foo)
0|id|int|0||1
1|name|varchar(10)|0||0

Solution 4 - Sqlite

You should be able to see the schema by running

.schema <table>

Solution 5 - Sqlite

.schema TableName

Where TableName is the name of the Table

Solution 6 - Sqlite

You will get the structure by typing the command:

.schema <tableName>

Solution 7 - Sqlite

If you are using PHP you can get it this way:

<?php
    $dbname = 'base.db';
    $db = new SQLite3($dbname);
    $sturturequery = $db->query("SELECT sql FROM sqlite_master WHERE name='foo'");

    $table = $sturturequery->fetchArray();
    echo '<pre>' . $table['sql'] . '</pre>';

    $db->close();
?>

Solution 8 - Sqlite

You can use the Firefox add-on called SQLite Manager to view the database's structure clearly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAliView Question on Stackoverflow
Solution 1 - SqliteAnonGeekView Answer on Stackoverflow
Solution 2 - SqlitemetamattView Answer on Stackoverflow
Solution 3 - SqliteMicha WiedenmannView Answer on Stackoverflow
Solution 4 - SqliteCosta WalcottView Answer on Stackoverflow
Solution 5 - SqliteAbiView Answer on Stackoverflow
Solution 6 - SqliteaTJView Answer on Stackoverflow
Solution 7 - SqliteGilView Answer on Stackoverflow
Solution 8 - SqliteJackView Answer on Stackoverflow