{ "info": { "author": "Rene Tanczos", "author_email": "gravmatt@gmail.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2.5", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3" ], "description": "sqlitemodel\n===========\n\nsqlitemodel is a wrapper for the sqlite3 database that enables you to\ncreate models you can easily save, query and retrieve from the database.\n\nThis is build with three classes who abstract the database communication\nand the object management.\n\nInstallation\n------------\n\nInstall through **pip**.\n\n::\n\n $ pip install sqlitemodel\n\nor get from source\n\n::\n\n $ git clone https://github.com/gravmatt/sqlitemodel.git\n $ cd sqlitemodel\n $ python setup.py install\n\nClasses\n-------\n\n- `**Model** - Abstraction class to build database models <#model>`__\n\n- `**SQL** - SQL query builder <#sql>`__\n\n- `**Database** - sqlite database interface <#database>`__\n\nModel\n-----\n\nClass to abstract the model communication with the database.\n\nUsage\n~~~~~\n\n**Import**\n\n::\n\n from sqlitemodel import Model, Database\n\n # IMPORTANT\n Database.DB_FILE = 'path/to/database.db'\n\n**Set the path to the database when your application starts or before\nyou try to accessing the database.**\n\nExample\n^^^^^^^\n\nBuilding a user class that inherits the Model class to show how it\nworks.\n\n::\n\n class User(Model):\n def __init__(self, id=None):\n Model.__init__(self, id, dbfile=None, foreign_keys=False, parse_decltypes=False)\n\n self.firstname = ''\n self.lastname = ''\n self.age = ''\n\n # Tries to fetch the object by its rowid from the database\n self.getModel()\n\n\n # Tells the database class the name of the database table\n def tablename(self):\n return 'users'\n\n\n # Tells the database class more about the table columns in the database\n def columns(self):\n return [\n {\n 'name': 'firstname',\n 'type': 'TEXT'\n },\n {\n 'name': 'lastname',\n 'type': 'TEXT'\n },\n {\n 'name': 'age',\n 'type': 'INTEGER'\n }\n ]\n\nThe two methods ``tablename()`` and ``columns()`` are required, to map\nthe table columns with the ``Model`` objects.\n\n``id`` argument and the ``getModel()`` method in the constructor are\noptional.\n\nIt also possible to use the ``selectCopy()`` method to query for any\ndata in the database table and fill the model object with the result.\n\n::\n\n selectCopy(SQL() | raw_sql_query_string)\n\nEx:\n\n::\n\n class User(Model):\n def __init__(self, id=None, email=None):\n Model.__init__(self, id)\n if(email):\n self.selectCopy(SQL().WHERE('email', '=', email))\n\n**The ``Model`` class constructor has an optional ``dbfile`` argument.\nIf it is set, the static variable ``Database.DB_FILE`` is ignored.**\n\nWorking with the User class\n^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n**Creating a new User**\n\n::\n\n # create a new user\n user = User()\n\n # creating the table inside the database\n user.createTable()\n\n # add infos about the user\n user.firstname = 'Rene'\n user.lastname = 'Tanczos'\n user.age = 25\n\n # save the user into the database\n user.save()\n\n**Retriving the User from the database**\n\n::\n\n # get it by id\n user = User(1)\n\n # get the user by his firstname and lastname\n # User().selectOne(SQL())\n user = User().selectOne(SQL().WHERE('firstname', '=', 'Rene').AND().WHERE( 'lastname', '=', 'Tanczos'))\n\n # Or get more the one user\n # this method will return an array of matching users\n users = User().select(SQL().WHERE('age', '=', 25))\n\nSQL\n---\n\nClass to build SQL query to reduce misspelling and to abstract this\nproblem a bit.\n\nUsage\n~~~~~\n\n**Import**\n\n::\n\n from sqlitemodel import SQL\n\n**INSERT**\n\n::\n\n sql = SQL().INSERT('users').VALUES(firstname='Rene', lastname='tanczos')\n\n print sql.toStr()\n # INSERT INTO users (firstname,lastname) VALUES (?,?);\n\n print sql.getValues()\n # ('Rene', 'tanczos')\n\n**UPDATE**\n\n::\n\n sql = SQL().UPDATE('users').SET('firstname', 'Rene').SET('lastname', 'Tanczos').WHERE('firstname', '=', 'Rene').AND().WHERE('lastname', '=', 'Tanczos')\n\n print sql.toStr()\n # UPDATE users SET firstname=?, lastname=? WHERE firstname=? AND lastname=?;\n\n print sql.getValues()\n # ('Rene', 'Tanczos', 'Rene', 'Tanczos')\n\n**SELECT**\n\n::\n\n sql = SQL().SELECT('name', 'age', 'size').FROM('users').WHERE('age', '=', 27).AND().WHERE('size', '<', 190).ORDER_BY('age', 'ASC').LIMIT(0, 10)\n\n print sql.toStr()\n # SELECT name, age, size FROM users WHERE age=? AND size