/docs/index.md

https://github.com/encode/databases · Markdown · 104 lines · 77 code · 27 blank · 0 comment · 0 complexity · 2b2a61e912f20c36c12dc5bd52f76b2b 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. **Community**: [https://discuss.encode.io/c/databases](https://discuss.encode.io/c/databases)
  16. **Requirements**: Python 3.6+
  17. ---
  18. ## Installation
  19. ```shell
  20. $ pip install databases
  21. ```
  22. You can install the required database drivers with:
  23. ```shell
  24. $ pip install databases[postgresql]
  25. $ pip install databases[mysql]
  26. $ pip install databases[sqlite]
  27. ```
  28. Driver support is providing using one of [asyncpg][asyncpg], [aiomysql][aiomysql], or [aiosqlite][aiosqlite].
  29. ---
  30. ## Quickstart
  31. For this example we'll create a very simple SQLite database to run some
  32. queries against.
  33. ```shell
  34. $ pip install databases[sqlite]
  35. $ pip install ipython
  36. ```
  37. We can now run a simple example from the console.
  38. Note that we want to use `ipython` here, because it supports using `await`
  39. expressions directly from the console.
  40. ```python
  41. # Create a database instance, and connect to it.
  42. from databases import Database
  43. database = Database('sqlite:///example.db')
  44. await database.connect()
  45. # Create a table.
  46. query = """CREATE TABLE HighScores (id INTEGER PRIMARY KEY, name VARCHAR(100), score INTEGER)"""
  47. await database.execute(query=query)
  48. # Insert some data.
  49. query = "INSERT INTO HighScores(name, score) VALUES (:name, :score)"
  50. values = [
  51. {"name": "Daisy", "score": 92},
  52. {"name": "Neil", "score": 87},
  53. {"name": "Carol", "score": 43},
  54. ]
  55. await database.execute_many(query=query, values=values)
  56. # Run a database query.
  57. query = "SELECT * FROM HighScores"
  58. rows = await database.fetch_all(query=query)
  59. print('High Scores:', rows)
  60. ```
  61. Check out the documentation on [making database queries](database_queries.md)
  62. for examples of how to start using databases together with SQLAlchemy core expressions.
  63. <p align="center">&mdash; &mdash;</p>
  64. <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>
  65. [sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
  66. [sqlalchemy-core-tutorial]: https://docs.sqlalchemy.org/en/latest/core/tutorial.html
  67. [alembic]: https://alembic.sqlalchemy.org/en/latest/
  68. [asyncpg]: https://github.com/MagicStack/asyncpg
  69. [aiomysql]: https://github.com/aio-libs/aiomysql
  70. [aiosqlite]: https://github.com/jreese/aiosqlite
  71. [starlette]: https://github.com/encode/starlette
  72. [sanic]: https://github.com/huge-success/sanic
  73. [responder]: https://github.com/kennethreitz/responder
  74. [quart]: https://gitlab.com/pgjones/quart
  75. [aiohttp]: https://github.com/aio-libs/aiohttp
  76. [tornado]: https://github.com/tornadoweb/tornado
  77. [fastapi]: https://github.com/tiangolo/fastapi