{"id":17914191,"url":"https://github.com/knadh/simplemysql","last_synced_at":"2026-03-15T17:06:25.234Z","repository":{"id":8771249,"uuid":"10457022","full_name":"knadh/simplemysql","owner":"knadh","description":"An ultra simple wrapper for Python MySQLdb with very basic functionality","archived":false,"fork":false,"pushed_at":"2020-07-13T10:33:52.000Z","size":40,"stargazers_count":79,"open_issues_count":3,"forks_count":36,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-15T11:06:54.339Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/knadh.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":"2013-06-03T14:56:36.000Z","updated_at":"2025-02-09T14:37:39.000Z","dependencies_parsed_at":"2022-09-02T10:02:39.973Z","dependency_job_id":null,"html_url":"https://github.com/knadh/simplemysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fsimplemysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fsimplemysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fsimplemysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knadh%2Fsimplemysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/knadh","download_url":"https://codeload.github.com/knadh/simplemysql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245186438,"owners_count":20574551,"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":[],"created_at":"2024-10-28T19:56:41.751Z","updated_at":"2026-03-15T17:06:25.181Z","avatar_url":"https://github.com/knadh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"#SimpleMysql\r\nAn ultra simple wrapper for Python MySQLdb with very basic functionality\r\n\r\n- Kailash Nadh, June 2013\r\n- Documentation: [http://nadh.in/code/simplemysql](http://nadh.in/code/simplemysql)\r\n- License: GPL v2\r\n\r\n## Installation\r\nWith pip or easy_install\r\n\r\n```pip install simplemysql``` or ```easy_install simplemysql```\r\n\r\nOr from the source\r\n\r\n```python setup.py install```\r\n\r\n# Usage\r\n## For normal connection\r\n```python\r\nfrom simplemysql import SimpleMysql\r\n\r\ndb = SimpleMysql(\r\n\thost=\"127.0.0.1\",\r\n\tdb=\"mydatabase\",\r\n\tuser=\"username\",\r\n\tpasswd=\"password\",\r\n\tkeep_alive=True # try and reconnect timedout mysql connections?\r\n)\r\n```\r\n## For SSL Connection\r\n```python\r\nfrom simplemysql import SimpleMysql\r\n\r\ndb = SimpleMysql(\r\n    host=\"127.0.0.1\",\r\n    db=\"mydatabase\",\r\n    user=\"username\",\r\n    passwd=\"password\",\r\n    ssl = {'cert': 'client-cert.pem', 'key': 'client-key.pem'},\r\n    keep_alive=True # try and reconnect timedout mysql connections?\r\n)\r\n\r\n```\r\n\r\n\r\n```python\r\n# insert a record to the \u003cem\u003ebooks\u003c/em\u003e table\r\ndb.insert(\"books\", {\"type\": \"paperback\", \"name\": \"Time Machine\", \"price\": 5.55, year: \"1997\"})\r\n\r\nbook = db.getOne(\"books\", [\"name\"], [\"year = 1997\"])\r\n\r\nprint \"The book's name is \" + book.name\r\n```\r\n\r\n# Query methods\r\ninsert(), update(), delete(), getOne(), getAll(), lastId(), query()\r\n\r\n## insert(table, record{})\r\nInserts a single record into a table.\r\n\r\n```python\r\ndb.insert(\"food\", {\"type\": \"fruit\", \"name\": \"Apple\", \"color\": \"red\"})\r\ndb.insert(\"books\", {\"type\": \"paperback\", \"name\": \"Time Machine\", \"price\": 5.55})\r\n```\r\n\r\n## update(table, row{}, condition[])\r\nUpdate one more or rows based on a condition (or no condition).\r\n\r\n```python\r\n# update all rows\r\ndb.update(\"books\", {\"discount\": 0})\r\n\r\n# update rows based on a simple hardcoded condition\r\ndb.update(\"books\",\r\n\t{\"discount\": 10},\r\n\t[\"id=1\"]\r\n)\r\n\r\n# update rows based on a parametrized condition\r\ndb.update(\"books\",\r\n\t{\"discount\": 10},\r\n\t(\"id=%s AND year=%s\", [id, year])\r\n)\r\n```\r\n## insertBatch(table, rows{})\r\nInsert Multiple values into table.\r\n\r\n```python\r\n# insert multiple values in table\r\ndb.insertBatch(\"books\", [{\"discount\": 0},{\"discount\":1},{\"discount\":3}])\r\n```\r\n\r\n## insertOrUpdate(table, row{}, key)\r\nInsert a new row, or update if there is a primary key conflict.\r\n\r\n```python\r\n# insert a book with id 123. if it already exists, update values\r\ndb.insertOrUpdate(\"books\",\r\n\t\t{\"id\": 123, type\": \"paperback\", \"name\": \"Time Machine\", \"price\": 5.55},\r\n\t\t\"id\"\r\n)\r\n```\r\n\r\n## getOne(table, fields[], where[], order[], limit[])\r\n## getAll(table, fields[], where[], order[], limit[])\r\nGet a single record or multiple records from a table given a condition (or no condition). The resultant rows are returned as namedtuples. getOne() returns a single namedtuple, and getAll() returns a list of namedtuples.\r\n\r\n```python\r\nbook = db.getOne(\"books\", [\"id\", \"name\"])\r\n```\r\n\r\n```python\r\n# get a row based on a simple hardcoded condition\r\nbook = db.getOne(\"books\", [\"name\", \"year\"], (\"id=1\"))\r\n```\r\n\r\n```python\r\n# get multiple rows based on a parametrized condition\r\nbooks = db.getAll(\"books\",\r\n\t[\"id\", \"name\"],\r\n\t(\"year \u003e %s and price \u003c %s\", [year, 12.99])\r\n)\r\n```\r\n\r\n```python\r\n# get multiple rows based on a parametrized condition with an order and limit specified\r\nbooks = db.getAll(\"books\",\r\n\t[\"id\", \"name\", \"year\"],\r\n\t(\"year \u003e %s and price \u003c %s\", [year, 12.99]),\r\n\t[\"year\", \"DESC\"],\t# ORDER BY year DESC\r\n\t[0, 10]\t\t\t# LIMIT 0, 10\r\n)\r\n```\r\n## lastId()\r\nGet the last insert id\r\n```python\r\n# get the last insert ID\r\ndb.lastId()\r\n```\r\n\r\n## lastQuery()\r\nGet the last query executed\r\n```python\r\n# get the SQL of the last executed query\r\ndb.lastQuery()\r\n```\r\n\r\n## delete(table, fields[], condition[], order[], limit[])\r\nDelete one or more records based on a condition (or no condition)\r\n\r\n```python\r\n# delete all rows\r\ndb.delete(\"books\")\r\n\r\n# delete rows based on a condition\r\ndb.delete(\"books\", (\"price \u003e %s AND year \u003c %s\", [25, 1999]))\r\n```\r\n\r\n## query(table)\r\nRun a raw SQL query. The MySQLdb cursor is returned.\r\n\r\n```python\r\n# run a raw SQL query\r\ndb.query(\"DELETE FROM books WHERE year \u003e 2005\")\r\n```\r\n\r\n## commit()\r\nInsert, update, and delete operations on transactional databases such as innoDB need to be committed\r\n\r\n```python\r\n# Commit all pending transaction queries\r\ndb.commit()\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknadh%2Fsimplemysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknadh%2Fsimplemysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknadh%2Fsimplemysql/lists"}