/README.md

https://github.com/encode/databases · Markdown · 106 lines · 78 code · 28 blank · 0 comment · 0 complexity · 5f425983872bff1fc50dd17b96ff9f25 MD5 · raw file

  1. # Databases
  2. <p>
  3. <a href="https://github.com/encode/databases/actions">
  4. <img src="https://github.com/encode/databases/workflows/Test%20Suite/badge.svg" alt="Test Suite">
  5. </a>
  6. <a href="https://pypi.org/project/databases/">
  7. <img src="https://badge.fury.io/py/databases.svg" alt="Package version">
  8. </a>
  9. </p>
  10. Databases gives you simple asyncio support for a range of databases.
  11. It allows you to make queries using the powerful [SQLAlchemy Core][sqlalchemy-core]
  12. expression language, and provides support for PostgreSQL, MySQL, and SQLite.
  13. Databases is suitable for integrating against any async Web framework, such as [Starlette][starlette],
  14. [Sanic][sanic], [Responder][responder], [Quart][quart], [aiohttp][aiohttp], [Tornado][tornado], or [FastAPI][fastapi].
  15. **Documentation**: [https://www.encode.io/databases/](https://www.encode.io/databases/)
  16. **Community**: [https://discuss.encode.io/c/databases](https://discuss.encode.io/c/databases)
  17. **Requirements**: Python 3.6+
  18. ---
  19. ## Installation
  20. ```shell
  21. $ pip install databases
  22. ```
  23. You can install the required database drivers with:
  24. ```shell
  25. $ pip install databases[postgresql]
  26. $ pip install databases[mysql]
  27. $ pip install databases[sqlite]
  28. ```
  29. Driver support is providing using one of [asyncpg][asyncpg], [aiomysql][aiomysql], or [aiosqlite][aiosqlite].
  30. ---
  31. ## Quickstart
  32. For this example we'll create a very simple SQLite database to run some
  33. queries against.
  34. ```shell
  35. $ pip install databases[sqlite]
  36. $ pip install ipython
  37. ```
  38. We can now run a simple example from the console.
  39. Note that we want to use `ipython` here, because it supports using `await`
  40. expressions directly from the console.
  41. ```python
  42. # Create a database instance, and connect to it.
  43. from databases import Database
  44. database = Database('sqlite:///example.db')
  45. await database.connect()
  46. # Create a table.
  47. query = """CREATE TABLE HighScores (id INTEGER PRIMARY KEY, name VARCHAR(100), score INTEGER)"""
  48. await database.execute(query=query)
  49. # Insert some data.
  50. query = "INSERT INTO HighScores(name, score) VALUES (:name, :score)"
  51. values = [
  52. {"name": "Daisy", "score": 92},
  53. {"name": "Neil", "score": 87},
  54. {"name": "Carol", "score": 43},
  55. ]
  56. await database.execute_many(query=query, values=values)
  57. # Run a database query.
  58. query = "SELECT * FROM HighScores"
  59. rows = await database.fetch_all(query=query)
  60. print('High Scores:', rows)
  61. ```
  62. Check out the documentation on [making database queries](https://www.encode.io/databases/database_queries/)
  63. for examples of how to start using databases together with SQLAlchemy core expressions.
  64. <p align="center">&mdash; &mdash;</p>
  65. <p align="center"><i>Databases is <a href="https://github.com/encode/databases/blob/master/LICENSE.md">BSD licensed</a> code. Designed & built in Brighton, England.</i></p>
  66. [sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
  67. [sqlalchemy-core-tutorial]: https://docs.sqlalchemy.org/en/latest/core/tutorial.html
  68. [alembic]: https://alembic.sqlalchemy.org/en/latest/
  69. [asyncpg]: https://github.com/MagicStack/asyncpg
  70. [aiomysql]: https://github.com/aio-libs/aiomysql
  71. [aiosqlite]: https://github.com/jreese/aiosqlite
  72. [starlette]: https://github.com/encode/starlette
  73. [sanic]: https://github.com/huge-success/sanic
  74. [responder]: https://github.com/kennethreitz/responder
  75. [quart]: https://gitlab.com/pgjones/quart
  76. [aiohttp]: https://github.com/aio-libs/aiohttp
  77. [tornado]: https://github.com/tornadoweb/tornado
  78. [fastapi]: https://github.com/tiangolo/fastapi