{"id":50762016,"url":"https://github.com/kevinmarquesp/csv2sql","last_synced_at":"2026-06-11T11:01:20.141Z","repository":{"id":218213211,"uuid":"745877733","full_name":"kevinmarquesp/csv2sql","owner":"kevinmarquesp","description":"Script that reads a CSV file and constructs a SQL query string that inserts every row of this file into an SQL database -- you can also connect to an PostgreSQL database (by default) and actually run that generated insert command","archived":false,"fork":false,"pushed_at":"2026-04-25T17:54:10.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-25T19:24:15.877Z","etag":null,"topics":["cli","cli-tool","csv","csv-parser","csv-parser-python","learning-by-doing","python","sql","study-project"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kevinmarquesp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-01-20T12:23:28.000Z","updated_at":"2026-04-25T17:57:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"945455a4-0447-4113-99c2-b5279c7895a3","html_url":"https://github.com/kevinmarquesp/csv2sql","commit_stats":null,"previous_names":["kevinmarquesp/csv_to_sql","kevinmarquesp/csv2sql"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kevinmarquesp/csv2sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinmarquesp%2Fcsv2sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinmarquesp%2Fcsv2sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinmarquesp%2Fcsv2sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinmarquesp%2Fcsv2sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kevinmarquesp","download_url":"https://codeload.github.com/kevinmarquesp/csv2sql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinmarquesp%2Fcsv2sql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34195117,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cli","cli-tool","csv","csv-parser","csv-parser-python","learning-by-doing","python","sql","study-project"],"created_at":"2026-06-11T11:01:18.318Z","updated_at":"2026-06-11T11:01:20.123Z","avatar_url":"https://github.com/kevinmarquesp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSV To SQL Script\nScript that reads a CSV file and constructs a SQL query string that inserts every row of this file into an SQL database -- you can also connect to an PostgreSQL database (by default) and actually run that generated insert command.\n\nI was studying SQL and I needed to pass an example CSV file to my database in order to do some operations and study the behavior of the SQL syntax, so I thought that it would be a really good quick project to rest a bit and let all this SQL stuff for later. Probably, this script isn't the best solution for that problem, so if you want a proper tool to do that, maybe you'll need to do a little research. But, by the way, if this little side project is useful to you, I would be happy.\n\nThe thing that I was studying with this project is to find a good and simple Python project structure that is easy to use/install and test. This happens to be a really simple command line tool, so it's better to hold the entire code base in a single file, so the user could just copy that source code and use it as an app -- the challenge was to make a testable command line tool that doesn't require installing any libraries, just using what Python already offers me by default.\n\nhttps://github.com/kevinmarquesp/csv_to_sql/assets/71402116/58e99629-bd08-4d5c-be08-d62f3f684db0\n\n## Installation Process\nAs I said, the important file is the `csv_to_sql/csv_to_sql.py` file, so the way to go is just copy and paste the contents of this file in somewhere on your `$PATH`. You can just select the text from your browser, open your text editor and paste its contents, or you can just follow the steps below:\n```bash\ngit clone https://github.com/kevinmarquesp/csv2sql\ncp csv2sql/csv_to_sql/csv_to_sql.py ~/.local/bin/csv2sql\nrm -rf csv2sql  #removes the cloned repository\n```\n\n## Uninstall Process\nTo uninstall you'll need to remove this file from your system, so it depends on where you've installed it in the first place. But you followed the recommended steps to install in this document, you can uninstall with:\n```bash\nrm ~/.local/bin/csv2sql\n```\n\n## Development \u0026 Contribution\nThis code **should not** have any external dependencies that the user should use `python3 -m pip` to install, the objective is to be *plug and play* tool. Also, I think everything becomes a lot easier -- for the user, at least -- if all the code base is condensed into a single file, which could be a challange to mantain, it's important to write good and readable code for that.\n\nI'm using a simple \"*pythonic*\" **styleguide**, the only constraint is that each line should not be longer than 80 characters and that I'm using double quotes (`\"\"`) as the main characters to denote a string -- that's because english has a bunch of `'` in its grammar, and escaping each of them is just too annoying.\n\n\u003e [!TIP]\n\u003e Every time you update the docstrings of the `csv_to_sql.py` file, you can run the `build_docs.sh` script to automatically update the contents of this `README.md` file. But you'll have to install the `pydoc-markdown` library from `pip`; its a command line utility that generates a Markdown documentation based on the docstrings inside a file/module.\n\nAbout the **tests**, it has a similar philosophy. There is a single test script -- the `test.py` file -- that uses the default `unittest` library. You can run these tests with: `python3 test.py`. Also, it's a good idea to be creative and do something to run this tests constantly, you can use a watch command to run every time the `csv_to_sql/csv_to_sql.py` is modified or something like that, I personally likes to put it in a *while sleep* loop.\n\n### Code's Documentation (autogenerated)\n\u003c!--{{ PYDOC_MARKDOWN }}--\u003e\n```python\ndef log(msg: str)\n```\n\nSimple function that replaces some special characters to their\nrespective color codes, like replacing an `[g]`, which means \"green\", to\n`\\033[32m` of the string, then printing it on the screen.\n\n+ **msg**: The message that you want to show on the terminal output.\n\n\n\n```python\ndef parse_arguments(args: list[str]) -\u003e Namespace\n```\n\nGiven an list of arguments -- e.g. `['argument', '-o', '--option',\n'optton_value']` -- this function puts everything together and returns a\n`Namespace` object with all that arguments parsed to Python's types and\neasily accessible with the `parsed_args.option` notation.\n\n+ **args**: List of arguments that the user has specified, maybe you would\n            like to use it with the `sys.argv` list to access the command\n            line arguments.\n\n\n\n```python\ndef join_list_format_sql(data: list[str]) -\u003e str\n```\n\nUtility function that joins every instance of a list with a `, `\ncharacter, and put that string between `()`. Creating a string that is\ncompatible with SQL syntax, you can use it to select columns in a table or\nto list values for each column.\n\n+ **data**: List of strings that will be joined together.\n\n\n\n```python\ndef escape_sql_characters(sql_str: str) -\u003e str\n```\n\nGiven a string, it will put a `\\` character for each character that\ncould cause some trouble in you SQL string. It's important if an user is\ncalled `Claire O’Connell` for an example, the `'` cound couse some syntax\nerror -- also, it's important to avoid SQL injection.\n\n+ **sql_str**: The string that you want to escape the characters;\n\n\n\n```python\ndef format_sql_row(row: list[str]) -\u003e list[str]\n```\n\nThe most complicated function, this function creates a list with\nstrings that is compatible with SQL syntax. For an example: it replaces a\n\"Foo\" string for \"E'Foo'\" -- with special characters escaped -- and a \"1\"\nstring to just \"1\". Maybe you'll need to check the test cases or the source\ncode in order to understand that function well...\n\n\u003e [!NOTE]\n\u003e For an example: If you give an `[\"52\", \"Rice\", \"20.7\", \"TRUE\"]` list, it\n\u003e will return a list that looks like `[\"52\", \"E'Rice'\", \"20.7\", \"TRUE\"]`\n\n+ **row**: Is the list of strings that you want to format to be compatible\n           with SQL syntax.\n\n\n\n```python\ndef get_sql_slices(file_path: str, dlmtr: str = \",\")\n```\n\nUtility function that opens the file and parses the header and column\nvalues string slices that are compatible with the SQL syntax.\n+ **file_path**: Path string to access the file contents;\n+ **dlmtr**: This is `,` by default, but you can set a custom delimiter if\n             your file is formatted in a different way.\n\n\n\n```python\ndef get_insert_query(file_path: str, dlmtr: str = \",\") -\u003e str\n```\n\nOpen the specified file to format a SQL statement that inserts every\nrow values on the `.csv` file into the table that has the same name of the\n`.csv` file. Also, it checks for erros when opening the file, if something\ngoes wrong, it will exit the script with status 1.\n\n+ **file_path**: Path string to access the file contents;\n+ **dlmtr**: This is `,` by default, but you can set a custom delimiter if\n             your file is formatted in a different way.\n\n\n\n```python\ndef connect_and_send(host: str, port: int, user: str, password: str,\n                     db_name: str, insert_query: str) -\u003e None\n```\n\nThis is the most important function. By default, it uses the database\nauthentication information provided in the command line arguments to\nconnect to a PostgreSQL database and send that query. If you want this\nscript to work with different databases, maybe you would like to edit the\nsource code for that, and that's the function that you're looking for.\n\n+ **host**: Hostname of your database server;\n+ **port**: Port for the connection to your database server;\n+ **user**: Username to access the database tables;\n+ **password**: Password of your database user;\n+ **db_name**: The database this script should access once it's connected;\n+ **insert_query**: Query that this code generates by reading a `.csv`\n                    file.\n\n\n\n```python\ndef main(args: list[str]) -\u003e None\n```\n\nMain function that parses command line arguments, logs information,\ngenerates SQL insert queries from CSV files, and sends them to a database.\nThe function works as follows:\n\n1. Parses the command line arguments;\n1. Iterates over each CSV file path in the parsed arguments;\n1. Generates an SQL insert query for the current CSV file;\n1. If the `print` flag is set in the parsed arguments, it prints the insert\n   query and breaks the loop; and\n1. Connects to the database and sends the insert query.\n\n+ **args**: List of command line arguments.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevinmarquesp%2Fcsv2sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkevinmarquesp%2Fcsv2sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevinmarquesp%2Fcsv2sql/lists"}