Git Product home page Git Product logo

csv2sql's Introduction

CSV to SQL for Drupal

Convert CSV to SQL and create a table in your Drupal installation

CSV are a great way to give a client to enter their data that will be migrated to the site. However, for the migration itself, SQL would be quicker.

Install the drush command by running drush dl csv2sql.

Execute the command: drush csv2sql /PATH/TO/file.csv

Scan a directory.

You have the option to scan a whole directory instead of giving an exact path to a csv file. Execute the command: drush csv2sql /PATH/TO/directory/

Will create a _raw_file table in the Drupal installation which drush is running under.

  • Each column is created as varchar 255 by default. However it is possible to override it by setting the header in the CSV file.
  • To prevent the user of MySql reserved words, each column is prefixed with an underscore (e.g. _order)
  • The first column is treated as the primary column
  • In the SQL a serial __id column is created
  • Index can be added to any column by adding index:TRUE to the column's header, The first column of each table is added to the index by default unless stated otherwise (index:FALSE) in the header, This will make for a faster migration if you need to use any column as key for referencing other entities.
Unique ID|index:FALSE Amount|type:int|length:11|default:0 Body|type:text|size:big User|index:TRUE
title1 3000 Some long text, that might even have line breaks. user1

The complex column will be translated in the DB to an amount column type int(11) where NULL value is allowed.

The values that can be passed in the header are the ones that are expected by db_create_table()

Dump SQL and import

In order to deploy a local copy to a remote server you may need to export the SQL tables, and later import them. Here's a handy bash command to do it - it exports all the tables prefixed with _raw_ into the raws.sql file. The second comamnd simply imports that SQL file to the remote server.

# Export SQL tables into a file.
drush sqlq "SHOW TABLES LIKE '_raw_%';" | awk -v ORS=, '{ print $1 }' | sed 's/,$//' | sed 's/^Tables_in_[^,]*,//' | drush sql-dump > raws.sql

# Replace the drush alias with your own.
`drush @remote-alias sql-connect` < raws.sql

csv2sql's People

Contributors

amitaibu avatar aronnovakinovae avatar helenaeksler avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.