0

I'm trying to find a way to run this query in a custom drush command to dump chunks of data from the DB into CVS files.

The query works fine directly in the terminal but I was wondering how would you do it in code for Drupal 7? I guess I can transform a PHP array to a CVS file but wanted to know if Drupal has a way to write more complex queries like this :)

Big thanks in advance!

Query:

SELECT uid, name, mail INTO OUTFILE '/path/sqldumps/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM users 
WHERE uid != 0
  • 3
    db_query() will execute any arbitrary SQL string - are you having a problem using it or have you maybe not tried it yet? – Clive Jun 17 '15 at 9:10
  • 2
    As a side note the Views Data Export module can generate CSV data from the UI. – mpdonadio Jun 17 '15 at 12:40
0

Drupal has it's own abstraction layer to work with database , this method helps Drupal to work with different kind of databases like PgSQL or even oracle (just a database driver will handle everything).

You can get familiar with this api in : Drupal Database API

A more exclusive reference could be : Drupal Dynamic Queries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.