{ "info": { "author": "Matej Ramuta", "author_email": "matej.ramuta@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# SmartNinja SQL\n\n## About\n\nSmartNinja SQL is a simple wrapper for SQLite database (support for others might be added in the future).\n\nThe purpose of this tool is to simplify connecting to SQLite and getting relevant data from it.\n\n**Important:** This is not an object-relational mapper (ORM). You still need to write SQL queries.\n\n## Installation\n\nThis is a pip package:\n\n\tpip install smartninja-sql\n\nThe package has one dependency, `prettytable`. It installs it automatically.\n\nThe package requires Python 3 (does not work with Python 2).\n\n## Usage\n\nTake a look at this basic usage example:\n\n```python\nfrom smartninja_sql.sqlite import SQLiteDatabase\n\t\ndb = SQLiteDatabase()\n\t\ndb.execute(\"\"\"CREATE TABLE IF NOT EXISTS User (\n id integer PRIMARY KEY AUTOINCREMENT, \n name text NOT NULL, \n age integer);\"\"\")\n\t\ndb.print_tables(verbose=True)\n```\n\nIf you open the connection with a database like this: `db = SQLiteDatabase()`, the database will be created in-memory only. When the program will finish, the database will be lost.\n\nIf you want a persistent database, enter a database name:\n\n```python\ndb = SQLiteDatabase(name=\"my_database.sqlite\")\n```\n\nIf this database does not yet exist, Python will create it for you. If it does exist, it will connect to it.\n\n### Print table data (name & fields)\n\nThis method prints the data about the tables in a database. By default the `verbose` parameter is `False` - in this case the method prints the names of the tables only:\n\n```python\n# same result in both cases\ndb.print_tables()\ndb.print_tables(verbose=False)\n```\n\nBut if you set `verbose` as `True`, the method will also print out all the **field names** in each table and their respective **types**:\n\n```python\ndb.print_tables(verbose=True)\n```\n\n### Queries\n\nDatabase statements are executed using the `.execute()` method:\n\n```python\n# create table\ndb.execute(\"\"\"CREATE TABLE IF NOT EXISTS User (\n id integer PRIMARY KEY AUTOINCREMENT, \n name text NOT NULL, \n age integer);\"\"\")\n\ndb.print_tables(verbose=True)\n\n# alter (edit) table\ndb.execute(\"ALTER TABLE User ADD email TEXT;\")\ndb.print_tables(verbose=True) # the table has a new field: email\n\n# insert data into a table\ndb.execute(\"INSERT INTO User (name, age, email) VALUES ('Matt', 31, 'matt@example.org')\")\ndb.execute(\"INSERT INTO User (name, age, email) VALUES ('Nina', 25, 'nina@example.org')\")\n\n# get data (select) and print it\nprint(db.execute(\"SELECT * FROM User;\"))\nprint(db.execute(\"SELECT * FROM User WHERE age=25;\"))\nprint(db.execute(\"SELECT * FROM User WHERE age=26;\"))\n```\n\nIf you're familiar with the `sqlite3` library, this method returns the `.fetchall()` result.\n\n### Pretty print\n\nThe `.pretty_print()` method does the same as the `.execute()` method, except that it also prints the data in a nice \ntable in the Terminal (using the `prettytable` package).\n\n```python\ndb.pretty_print(\"SELECT * FROM User;\")\n```\n\n### Connection and cursor\n\nThe SmartNinja SQL wrapper is designed to simplify work with the `sqlite3` library. But if you'd want to access the **connection** and **cursor** variables **directly**, you definitely can:\n\n```python\nconnection = db.conn\ncursor = db.cursor\n```\n\nBut you most likely won't have to.\n\n### Closing the cursor and connection\n\nYou can simply close the cursor and connection with a single method:\n\n```python\ndb.close()\n```\n\n## TODO\n\n- tests\n- CI pipeline", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/smartninja/smartninja-sql", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "smartninja-sql", "package_url": "https://pypi.org/project/smartninja-sql/", "platform": "", "project_url": "https://pypi.org/project/smartninja-sql/", "project_urls": { "Homepage": "https://github.com/smartninja/smartninja-sql" }, "release_url": "https://pypi.org/project/smartninja-sql/0.3/", "requires_dist": null, "requires_python": "", "summary": "SmartNinja SQL - a simple SQLite wrapper.", "version": "0.3" }, "last_serial": 4793212, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "a04d93cc552c27fc503eb139bceae80c", "sha256": "aed4ad0186f92e1480093eb6d8cc3c2a64d7927782d73dca14a331aadc50a2d2" }, "downloads": -1, "filename": "smartninja-sql-0.1.tar.gz", "has_sig": false, "md5_digest": "a04d93cc552c27fc503eb139bceae80c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3028, "upload_time": "2019-02-02T23:59:31", "url": "https://files.pythonhosted.org/packages/3f/0c/f83f5f7e1fc3edac0c40a0a283efbf8d5f8b1cc851f1904c0a35e182f2e3/smartninja-sql-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "521b42f8df53ce388bee149ac4c03016", "sha256": "84d6b6d9012cf42d3ac48088a13b5cb528cdc88fead3986dd21af21096cd78e7" }, "downloads": -1, "filename": "smartninja-sql-0.2.tar.gz", "has_sig": false, "md5_digest": "521b42f8df53ce388bee149ac4c03016", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3026, "upload_time": "2019-02-04T22:36:38", "url": "https://files.pythonhosted.org/packages/d4/dc/0b0733f4e5ef64dfcc026227eb9b3b470bae40a17cb9c43f96ec055b96d0/smartninja-sql-0.2.tar.gz" } ], "0.3": [ { "comment_text": "", "digests": { "md5": "8db0208f480450a93f6d5aa9c2cb7b33", "sha256": "9f652050b1bec4a3efc341d7954b9c618eb1ac95662ed810bdd76324ea1384d7" }, "downloads": -1, "filename": "smartninja-sql-0.3.tar.gz", "has_sig": false, "md5_digest": "8db0208f480450a93f6d5aa9c2cb7b33", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3337, "upload_time": "2019-02-07T22:06:53", "url": "https://files.pythonhosted.org/packages/b8/2e/9f2d28d260682fcc5b4a6c51cdc1fc21b9216acc822ceb323e09c182c146/smartninja-sql-0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "8db0208f480450a93f6d5aa9c2cb7b33", "sha256": "9f652050b1bec4a3efc341d7954b9c618eb1ac95662ed810bdd76324ea1384d7" }, "downloads": -1, "filename": "smartninja-sql-0.3.tar.gz", "has_sig": false, "md5_digest": "8db0208f480450a93f6d5aa9c2cb7b33", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3337, "upload_time": "2019-02-07T22:06:53", "url": "https://files.pythonhosted.org/packages/b8/2e/9f2d28d260682fcc5b4a6c51cdc1fc21b9216acc822ceb323e09c182c146/smartninja-sql-0.3.tar.gz" } ] }