{"id":19694592,"url":"https://github.com/petykowski/sql-script-generator","last_synced_at":"2025-02-27T10:48:03.692Z","repository":{"id":176069199,"uuid":"73430038","full_name":"petykowski/sql-script-generator","owner":"petykowski","description":"CSVtoSQL is a utility to generate different SQL scripts by referencing .csv files.","archived":false,"fork":false,"pushed_at":"2017-02-25T23:57:02.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-10T09:21:52.721Z","etag":null,"topics":["python-3","scripting","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petykowski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-10T23:37:22.000Z","updated_at":"2020-04-17T08:47:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"df50216a-6a54-4518-84c7-8d284bd8b15a","html_url":"https://github.com/petykowski/sql-script-generator","commit_stats":null,"previous_names":["petykowski/sql-script-generator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petykowski%2Fsql-script-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petykowski%2Fsql-script-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petykowski%2Fsql-script-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petykowski%2Fsql-script-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petykowski","download_url":"https://codeload.github.com/petykowski/sql-script-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241005429,"owners_count":19892788,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python-3","scripting","sql"],"created_at":"2024-11-11T19:23:39.078Z","updated_at":"2025-02-27T10:48:03.661Z","avatar_url":"https://github.com/petykowski.png","language":"Python","readme":"# CSVtoSQL\r\n\r\nCSVtoSQL is a utility to generate different SQL scripts from data in .csv files.\r\n\r\n## Output\r\n\r\n```SQL\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (1, 'Kabul', 'AFG', 'Kabol', 1780000);\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (2, 'Qandahar', 'AFG', 'Qandahar', 237500);\r\nCOMMIT;\r\n```\r\n\r\n## Setup\r\n\r\n1. Install Python version 3.0 or later.\r\n2. Clone (or download) the CSVtoSQL repository to your working directory.\r\n\r\n```Shell\r\ngit clone https://github.com/spetykowski/sql-script-generator.git\r\n```\r\n\r\n## Usage\r\n\r\n```Shell\r\npython csvtosql.py INSERT -t TABLENAME -f path/to/file.csv\r\n```\r\n\r\n### CSV Format\r\nThe first row of the csv should exactly match the column name of the target table.\r\n\r\n```\r\nID,Name,CountryCode,District,Population\r\n1,'Kabul','AFG','Kabol',1780000\r\n2,'Qandahar','AFG','Qandahar',237500\r\n```\r\n\r\n### Filename\r\nThe filename should exactly match that of the SQL table where the generated script will be used. If you would like to define a table name that is different from the filename use the `-t` flag.\r\n\r\n### Supported Data Types\r\n\r\n```\r\nString = 'String'\r\nInteger = 1\r\nDecimal = 12.55\r\nDate = TO_DATE('2017/02/06', 'yyyy/mm/dd')\r\n```\r\n\r\n### Parameters\r\nCSVtoSQL has a total of 5 parameters (2 required and 3 optional) available.\r\n\r\n```\r\ncommand {INSERT, MERGE} | (required) INSERT will convert the contents of a csv file(s) to SQL INSERT statement(s). MERGE will convert multiple SQL files into one comprehensive script. \r\n-c, --commit | (optional) Use this flag to prohibit CSVtoSQL from appending \"COMMIT;\" to the end of the SQL script.\r\n-f, --filepath | (required) Path to csv file (for single file process) or directory with multiple csv files (for batch process).\r\n-l, --last | (optional) Used to limit the number of records returned. Will return only the last `n` records.\r\n-t, --table | (optional) The SQL table where the data will be manipulated. (Default: Table Name = Filename without extention)\r\n```\r\n\r\n## Examples\r\n\r\n**via Terminal:**\r\n```Shell\r\npython csvtosql.py INSERT -f example/SQL_SAMPLE.csv\r\n```\r\n```SQL\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (1, 'Kabul', 'AFG', 'Kabol', 1780000);\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (2, 'Qandahar', 'AFG', 'Qandahar', 237500);\r\n...\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (9, 'Eindhoven', 'NLD', 'Noord-Brabant', 201843);\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (10, 'Tilburg', 'NLD', 'Noord-Brabant', 193238);\r\nCOMMIT;\r\n```\r\n\r\n**via Powershell:**\r\n```Shell\r\npython csvtosql.py INSERT -f .\\example\\SQL_SAMPLE.csv\r\n```\r\n```SQL\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (1, 'Kabul', 'AFG', 'Kabol', 1780000);\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (2, 'Qandahar', 'AFG', 'Qandahar', 237500);\r\n...\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (9, 'Eindhoven', 'NLD', 'Noord-Brabant', 201843);\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (10, 'Tilburg', 'NLD', 'Noord-Brabant', 193238);\r\nCOMMIT;\r\n```\r\n\r\n**Define Table Name:**\r\n```Shell\r\npython csvtosql.py INSERT -f example/SQL_SAMPLE.csv -t city \r\n```\r\n```SQL\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (1, 'Kabul', 'AFG', 'Kabol', 1780000);\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (2, 'Qandahar', 'AFG', 'Qandahar', 237500);\r\n...\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (9, 'Eindhoven', 'NLD', 'Noord-Brabant', 201843);\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (10, 'Tilburg', 'NLD', 'Noord-Brabant', 193238);\r\nCOMMIT;\r\n```\r\n\r\n**Generate Last 2 Rows and Skip Commit Command:**\r\n```Shell\r\npython csvtosql.py INSERT -f example/SQL_SAMPLE.csv -l 2 -c\r\n```\r\n```SQL\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (9, 'Eindhoven', 'NLD', 'Noord-Brabant', 201843);\r\nINSERT INTO SQL_SAMPLE (ID, Name, CountryCode, District, Population) VALUES (10, 'Tilburg', 'NLD', 'Noord-Brabant', 193238);\r\n```\r\n\r\n**Generate Multiple INSERT Files From Folder:**\r\n```Shell\r\npython csvtosql.py INSERT -f example/batch_insert/\r\n```\r\n```\r\noutput-city.sql\r\noutput-country.sql\r\noutput-countrylanguage.sql\r\n```\r\n\r\n**Merge SQL Files to a Single Script:**\r\n```Shell\r\npython3 csvtosql.py MERGE -f example/merge/\r\n```\r\n```SQL\r\n/* output-city.sql */ \r\n\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (1, 'Kabul', 'AFG', 'Kabol', 1780000);\r\n...\r\nINSERT INTO city (ID, Name, CountryCode, District, Population) VALUES (10, 'Tilburg', 'NLD', 'Noord-Brabant', 193238);\r\n\r\n/* output-country.sql */ \r\n\r\nINSERT INTO country (ID, Code, Name, Continent, Region) VALUES (1, 'ABW', 'Aruba', 'North America', 'Caribbean');\r\n...\r\nINSERT INTO country (ID, Code, Name, Continent, Region) VALUES (10, 'ARM', 'Armenia', 'Asia', 'Middle East');\r\n\r\n/* output-countrylanguage.sql */ \r\n\r\nINSERT INTO countrylanguage (ID, CountryCode, Language, IsOfficial) VALUES (1, 'ABW', 'Dutch', 'T');\r\n...\r\nINSERT INTO countrylanguage (ID, CountryCode, Language, IsOfficial) VALUES (10, 'AGO', 'Ambo', 'F');\r\n\r\nCOMMIT;\r\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetykowski%2Fsql-script-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetykowski%2Fsql-script-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetykowski%2Fsql-script-generator/lists"}