Documentation du Dr FRAPPE

Ce wiki regroupe les résultats de mes expériences en informatique accumulés au cours de mes recherches sur le net.

Dans la mesure du possible, j'ai cité mes sources ; il en manque certainement… :-)

Ceci est une ancienne révision du document !


SQLite : une base de données SQL

Pré-requis

Installation

  1. Installez le paquet sqlite3 ou en ligne de commande :

    ...@...:~$ sudo apt install sqlite3

Configuration

Utilisation

Avec un client graphique : SQLiteManager

En ligne de commande

  1. Lancez dans un terminal :

    ...@...:~ $ sqlite3
    SQLite version 3.31.1 2020-01-27 19:55:54
    Enter ".help" for usage hints.
    Connected to a transient in-memory database.
    Use ".open FILENAME" to reopen on a persistent database.
    sqlite>

    Le prompt change et signale que vous êtes dans le terminal SQlite :

Commandes SQLite

  1. .help : Lister les commandes :

    sqlite> .help
    .archive ...             Manage SQL archives
    .auth ON|OFF             Show authorizer callbacks
    .backup ?DB? FILE        Backup DB (default "main") to FILE
    .bail on|off             Stop after hitting an error.  Default OFF
    .binary on|off           Turn binary output on or off.  Default OFF
    .cd DIRECTORY            Change the working directory to DIRECTORY
    .changes on|off          Show number of rows changed by SQL
    .check GLOB              Fail if output since .testcase does not match
    .clone NEWDB             Clone data into NEWDB from the existing database
    .databases               List names and files of attached databases
    .dbconfig ?op? ?val?     List or change sqlite3_db_config() options
    .dbinfo ?DB?             Show status information about the database
    .dump ?TABLE? ...        Render all database content as SQL
    .echo on|off             Turn command echo on or off
    .eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN
    .excel                   Display the output of next command in spreadsheet
    .exit ?CODE?             Exit this program with return-code CODE
    .expert                  EXPERIMENTAL. Suggest indexes for queries
    .explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto
    .filectrl CMD ...        Run various sqlite3_file_control() operations
    .fullschema ?--indent?   Show schema and the content of sqlite_stat tables
    .headers on|off          Turn display of headers on or off
    .help ?-all? ?PATTERN?   Show help text for PATTERN
    .import FILE TABLE       Import data from FILE into TABLE
    .imposter INDEX TABLE    Create imposter table TABLE on index INDEX
    .indexes ?TABLE?         Show names of indexes
    .limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT
    .lint OPTIONS            Report potential schema issues.
    .load FILE ?ENTRY?       Load an extension library
    .log FILE|off            Turn logging on or off.  FILE can be stderr/stdout
    .mode MODE ?TABLE?       Set output mode
    .nullvalue STRING        Use STRING in place of NULL values
    .once (-e|-x|FILE)       Output for the next SQL command only to FILE
    .open ?OPTIONS? ?FILE?   Close existing database and reopen FILE
    .output ?FILE?           Send output to FILE or stdout if FILE is omitted
    .parameter CMD ...       Manage SQL parameter bindings
    .print STRING...         Print literal STRING
    .progress N              Invoke progress handler after every N opcodes
    .prompt MAIN CONTINUE    Replace the standard prompts
    .quit                    Exit this program
    .read FILE               Read input from FILE
    .recover                 Recover as much data as possible from corrupt db.
    .restore ?DB? FILE       Restore content of DB (default "main") from FILE
    .save FILE               Write in-memory database into FILE
    .scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off
    .schema ?PATTERN?        Show the CREATE statements matching PATTERN
    .selftest ?OPTIONS?      Run tests defined in the SELFTEST table
    .separator COL ?ROW?     Change the column and row separators
    .session ?NAME? CMD ...  Create or control sessions
    .sha3sum ...             Compute a SHA3 hash of database content
    .shell CMD ARGS...       Run CMD ARGS... in a system shell
    .show                    Show the current values for various settings
    .stats ?on|off?          Show stats or turn stats on or off
    .system CMD ARGS...      Run CMD ARGS... in a system shell
    .tables ?TABLE?          List names of tables matching LIKE pattern TABLE
    .testcase NAME           Begin redirecting output to 'testcase-out.txt'
    .testctrl CMD ...        Run various sqlite3_test_control() operations
    .timeout MS              Try opening locked tables for MS milliseconds
    .timer on|off            Turn SQL timer on or off
    .trace ?OPTIONS?         Output each SQL statement as it is run
    .vfsinfo ?AUX?           Information about the top-level VFS
    .vfslist                 List all available VFSes
    .vfsname ?AUX?           Print the name of the VFS stack
    .width NUM1 NUM2 ...     Set column widths for "column" mode

  2. .exit ou .quit : Quitter le programme sqlite
  3. Modifier le format de sortie :
    1. .mode MODE ?TABLE? : Mode de sortie
      1. MODE est à choisir parmi :
        1. csv : valeurs séparées par des virgules
        2. column : colonnes alignées à gauche (voir .width)
        3. html : <table> (code HTML)
          sqlite> .mode html

          affichera :

    2. insert : commande SQL insert pour la table TABLE
    3. line : Une valeur par ligne
    4. [list] : Valeurs délimitées par la chaîne de séparation .separator
    5. tabs : valeurs séparées par des tabulations
    6. tcl : Liste TCL des éléments
  4. Afficher le nom des colonnes / Changer l'aspect des colonnes (.mode column) :
    1. .header(s) on|[off] : Affiche (ou non) les titres

      Par exemple,

      sqlite> .header on
      sqlite> .mode column

      affichera les résultats comme ceci : L'affichage par défaut

      sqlite> .header off
      sqlite> .mode list

      affiche comme ceci :

    2. .width NUM1 NUM2 … : largeur des colonnes pour le mode column [par défaut, 10 caractères]

      Par exemple,

      sqlite> .width 2 15 10 20 3

      affichera:

  5. .separator STRING : change le séparateur utilisé par le mode de sortie et par .import

    En mode liste,

    sqlite> .separator ", "

    affichera :

Désinstallation

Voir aussi


Basé sur « Article » par Auteur.