Giter VIP home page Giter VIP logo

csv2sqlupdate's Introduction

##csv2sqlupdate

Creates SQL UPDATE strings for PostgreSQL based on a csv file.

###args

  • filename: relative path to the filename of the csv file
  • tablename: name of the table which is the target of the UPDATE commands

###format of input file

The input file needs to be a comma-separated CSV file, with the first line as the header.

The header lists:

  • name of reference column: the column which contains the unique value used to identify the record to update
  • name of column to be updated: the column which contains the value to be updated
  • name of column to be updated: (copy of previous)

NOTE: The column names indicated in the second and third positions of the header should be the same. This corresponds to the data in the body below those header items: the second item in each row of the body contains the "old" value to be replaced, and the third item in each row contains the "new" value. This is an intentional piece of security, to be sure the change is intended.

The body lists:

  • unique value used to identify the record to update
  • the original value, which will be replaced
  • the new value

###output

The script creates a text file in the same directory as the input file. The output file name is "[input base]_output.sql".

The output file contains SQL update statements.

Single quotes in value strings are escaped with another single quote.

###example

INPUT:

+------------+----------+--------------+
|   geoid    | townname |   townname   |
+------------+----------+--------------+
| 5001300700 | Alburg   | Alburgh      |
| 5000902162 | Avery's  | Avery's Gore |
| 5002303250 | Barre    | Barre Town   |
| 5002303175 | Barre    | Barre City   |
+------------+----------+--------------+

INVOKE:

python csv2sqlupdate.py data.csv towns_hist

OUTPUT:

UPDATE towns_hist SET townname = 'Alburgh' WHERE geoid = '5001300700' AND townname = 'Alburg';
UPDATE towns_hist SET townname = 'Avery''s Gore' WHERE geoid = '5000902162' AND townname = 'Avery''s';
UPDATE towns_hist SET townname = 'Barre Town' WHERE geoid = '5002303250' AND townname = 'Barre';
UPDATE towns_hist SET townname = 'Barre City' WHERE geoid = '5002303175' AND townname = 'Barre';

csv2sqlupdate's People

Watchers

Andrew Smith 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.