{"id":16032555,"url":"https://github.com/steder/sqllib","last_synced_at":"2025-07-06T16:32:25.218Z","repository":{"id":3673512,"uuid":"4742825","full_name":"steder/sqllib","owner":"steder","description":"SQL Templating for Python","archived":false,"fork":false,"pushed_at":"2018-08-07T20:27:06.000Z","size":10,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T04:43:49.942Z","etag":null,"topics":["experiment","python","sql-builder"],"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/steder.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}},"created_at":"2012-06-21T18:34:43.000Z","updated_at":"2021-02-26T09:51:15.000Z","dependencies_parsed_at":"2022-09-05T19:20:47.933Z","dependency_job_id":null,"html_url":"https://github.com/steder/sqllib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/steder/sqllib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steder%2Fsqllib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steder%2Fsqllib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steder%2Fsqllib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steder%2Fsqllib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steder","download_url":"https://codeload.github.com/steder/sqllib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steder%2Fsqllib/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263934787,"owners_count":23532200,"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":["experiment","python","sql-builder"],"created_at":"2024-10-08T21:23:07.051Z","updated_at":"2025-07-06T16:32:25.188Z","avatar_url":"https://github.com/steder.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"sqllib\n===============\n\nSometimes you want to try to separate SQL from Python code but you're not interested\nin using a real ORM.\n\nBasically you'd just like to better organize and delineate your SQL and Python.\n\n`sqllib` is a simple python library that attempts to do that by allowing you to\nload up `*.sql` files from within your Python code.  It depends on some special markers in the sql files to give each query a name addressable from Python.\n\nsqllib \"hello world\" example\n=================================\n\nImagine you have a couple simple tables (for convenience I'm assuming SQLite but `sqllib`\nshould work with any Python DBAPI-compatible database.  Anyway, let's say you have tables\nlike this:\n\n```\n# example database schema:\ncreate table greetings (id int, language int, greeting text);\ncreate table languages (id int, name text);\n```\n\nNow you might have a very simple script to query those tables with Python and SQL combined that looks like this:\n\n```\n# hello.py\nimport sqllite3\n\nget_all_greetings = \"\"\"select * from greetings\"\"\"\n\nconnection = sqlite3.connect(\"mydb\")\ncursor = connection.cursor()\ncursor.execute(get_all_greetings)\nfor row in cursor.fetchall():\n    print row\n```\n\nAt this point we're obviously not very complicated as we have no parameters for the SQL queries\nand we're really only talking about a single trivial select statement as well, but as queries get larger and more more complicated you end up with potentially a lot of SQL along side your python code and intertwined with it.\n\nSome folks find this really messy and want to be able to extract this SQL into its own place.\n\nSo `sqllib` would let you do the following:\n\nFirst, you might want to create a directory structure for all your SQL files:\n\n```\nmkdir sql\ntouch greetings.sql\n```\n\nNext, actually create one or more SQL files and put stuff in 'em:\n\n```\n-- greetings.sql\n\n[create_greetings]\ncreate table greetings (id int, language int, greeting text)\n\n[add_greeting:id,language,greeting]\ninsert into greetings (id, language, greeting) values (?, ?, ?)\n\n[get_greeting:id]\nselect * from greetings where id = ?\n\n[create_languages]\ncreate table languages (id int, name text)\n\n[add_language:id,name]\ninsert into languages (id, name) values (?, ?)\n\n[get_all_greetings]\nselect * from greetings\n\n```\n\nNow we can update our python script with `sqllib`:\n\n```\n# hello_sqllib.py\nimport sqllite3\nimport sqllib\n\nsql = sqllib.Library.from_path(\"sql/greetings.sql\")\n\nconnection = sqlite3.connect(\"mydb\")\ncursor = connection.cursor()\ncursor.execute(sql.get_all_greetings.raw)\nfor row in cursor.fetchall():\n    print row\n```\n\nOf course, if your needs are pretty simple you can also connect the library to your database\nand actually \"call\" your SQL functions.\n\n```\n# hello_sqllib_connect.py\nimport sqllite3\nimport sqllib\n\nsql = sqllib.Library.from_path(\"sql/greetings.sql\")\nsql.connect(sqlite3.connect(\"mydb\"))\nfor row in sql.get_all_greetings()\n    print row\n```\n\nThis'll also work for SQL queries with parameters (the `?`, `%s`, or `:1` markers), for example,\nafter connecting as above one could do:\n\n```\nsql.add_language(1, \"esperanto\")\nsql.add_language(2, \"english\")\nsql.create_greeting(1, 2, \"Hello World!\")\nsql.get_greeting(1)[2] == \"Hello World!\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteder%2Fsqllib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteder%2Fsqllib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteder%2Fsqllib/lists"}