Wp db (WP-CLI)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 16 jun 2022 om 21:27 (Nieuwe pagina aangemaakt met '''wp db'' doet dingen met databases, zoals uitvoeren van SQL-commando's <pre> $ wp help db NAME wp db DESCRIPTION Performs basic database operations using...')
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen

wp db doet dingen met databases, zoals uitvoeren van SQL-commando's

$ wp help db

NAME

  wp db

DESCRIPTION

  Performs basic database operations using credentials stored in wp-config.php.

SYNOPSIS

  wp db <command>

SUBCOMMANDS

  check         Checks the current status of the database.
  clean         Removes all tables with `$table_prefix` from the database.
  cli           Opens a MySQL console using credentials from wp-config.php
  columns       Displays information about a given table.
  create        Creates a new database.
  drop          Deletes the existing database.
  export        Exports the database to a file or to STDOUT.
  import        Imports a database from a file or from STDIN.
  optimize      Optimizes the database.
  prefix        Displays the database table prefix.
  query         Executes a SQL query against the database.
  repair        Repairs the database.
  reset         Removes all tables from the database.
  search        Finds a string in the database.
  size          Displays the database name and size.
  tables        Lists the database tables.

EXAMPLES

    # Create a new database.
    $ wp db create
    Success: Database created.

    # Drop an existing database.
    $ wp db drop --yes
    Success: Database dropped.

    # Reset the current database.
    $ wp db reset --yes
    Success: Database reset.

    # Execute a SQL query stored in a file.
    $ wp db query < debug.sql

wp db query

Voer willekeurige SQL-code uit

$ wp help db query

NAME

  wp db query

DESCRIPTION

  Executes a SQL query against the database.

SYNOPSIS

  wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]

  Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
 and `DB_PASSWORD` database credentials specified in wp-config.php.

OPTIONS

  [<sql>]
    A SQL query. If not passed, will try to read from STDIN.

  [--dbuser=<value>]
    Username to pass to mysql. Defaults to DB_USER.

  [--dbpass=<value>]
    Password to pass to mysql. Defaults to DB_PASSWORD.

  [--<field>=<value>]
    Extra arguments to pass to mysql. [Refer to mysql docs](https://dev.mysql.com/doc/en/mysql-command-options.html).

  [--defaults]
    Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to
    misconfiguration.

EXAMPLES

    # Execute a query stored in a file
    $ wp db query < debug.sql

    # Check all tables in the database
    $ wp db query "CHECK TABLE $(wp db tables | paste -s -d, -);"
    +---------------------------------------+-------+----------+----------+
    | Table                                 | Op    | Msg_type | Msg_text |
    +---------------------------------------+-------+----------+----------+
    | wordpress_dbase.wp_users              | check | status   | OK       |
    | wordpress_dbase.wp_usermeta           | check | status   | OK       |
    | wordpress_dbase.wp_posts              | check | status   | OK       |
    | wordpress_dbase.wp_comments           | check | status   | OK       |
    | wordpress_dbase.wp_links              | check | status   | OK       |
    | wordpress_dbase.wp_options            | check | status   | OK       |
    | wordpress_dbase.wp_postmeta           | check | status   | OK       |
    | wordpress_dbase.wp_terms              | check | status   | OK       |
    | wordpress_dbase.wp_term_taxonomy      | check | status   | OK       |
    | wordpress_dbase.wp_term_relationships | check | status   | OK       |
    | wordpress_dbase.wp_termmeta           | check | status   | OK       |
    | wordpress_dbase.wp_commentmeta        | check | status   | OK       |
    +---------------------------------------+-------+----------+----------+

    # Pass extra arguments through to MySQL
    $ wp db query 'SELECT * FROM wp_options WHERE option_name="home"' --skip-column-names
    +---+------+------------------------------+-----+
    | 2 | home | http://wordpress-develop.dev | yes |
    +---+------+------------------------------+-----+

Voorbeeld 1

wp db query "show tables"   # No trailing ";" nodig

Voorbeeld 2

sql_string='select
   *
from
   wp_posts
join
   wp_postmeta
   on
   wp_posts.ID = wp_postmeta.post_id
where
   post_type="nav_menu_item";'

wp db query "$sql_string"

Opmerkingen:

  • Ik heb de indruk dat ik deze manier van formatteren van SQL prefereer. Als ik de CR/LB eruit haal, wordt het op wat voor manier dan ook, moeilijker om te lezen
  • In het WP-CLI-commando, moet je het argument tussen " zetten, om te voorkomen dat de spaties gezien worden als scheidingstekens tussen positional arguments.