public function SqlBase::query

8.0.x SqlBase.php public SqlBase::query($query, $input_file = NULL, $result_file = '')
7.x SqlBase.php public SqlBase::query($query, $input_file = NULL, $result_file = '')
master SqlBase.php public SqlBase::query($query, $input_file = NULL, $result_file = '')

Execute a SQL query.

Note: This is an API function. Try to avoid using drush_get_option() and instead pass params in. If you don't want to query results to print during --debug then provide a $result_file whose value can be drush_bit_bucket().


string $query: The SQL to be executed. Should be NULL if $input_file is provided.

string $input_file: A path to a file containing the SQL to be executed.

string $result_file: A path to save query results to. Can be drush_bit_bucket() if desired.

Return value

TRUE on success, FALSE on failure

9 calls to SqlBase::query()
SqlBase::createdb in lib/Drush/Sql/SqlBase.php
Create a new database.
SqlBase::drop in lib/Drush/Sql/SqlBase.php
Sqlmysql::db_exists in lib/Drush/Sql/Sqlmysql.php
Sqlmysql::listTables in lib/Drush/Sql/Sqlmysql.php
Extract the name of all existing tables in the given database.
Sqloracle::listTables in lib/Drush/Sql/Sqloracle.php
Extract the name of all existing tables in the given database.

... See full list


lib/Drush/Sql/SqlBase.php, line 143






public function query($query, $input_file = NULL, $result_file = '') {
  $input_file_original = $input_file;
  if ($input_file && drush_file_is_tarball($input_file)) {
    if (drush_shell_exec('gunzip %s', $input_file)) {
      $input_file = trim($input_file, '.gz');
    else {
      return drush_set_error(dt('Failed to gunzip input file.'));

  // Save $query to a tmp file if needed. We will redirect it in.
  if (!$input_file) {
    $query = $this->query_prefix($query);
    $query = $this->query_format($query);
    $input_file = drush_save_data_to_temp_file($query);

  $parts = array(
    $this->silent(), // This removes column header and various helpful things in mysql.
    drush_get_option('extra', $this->query_extra),
  $exec = implode(' ', $parts);

  if ($result_file) {
    $exec .= ' > ' . drush_escapeshellarg($result_file);

  // In --verbose mode, drush_shell_exec() will show the call to mysql/psql/sqlite,
  // but the sql query itself is stored in a temp file and not displayed.
  // We show the query when --debug is used and this function created the temp file.
  if ((drush_get_context('DRUSH_DEBUG') || drush_get_context('DRUSH_SIMULATE')) && empty($input_file_original)) {
    drush_log('sql-query: ' . $query, LogLevel::NOTICE);

  $success = drush_shell_exec($exec);

  if ($success && drush_get_option('file-delete')) {
    drush_op('drush_delete_dir', $input_file);

  return $success;