{"id":1179,"date":"2023-07-30T18:37:00","date_gmt":"2023-07-30T13:07:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1179"},"modified":"2024-03-01T17:09:54","modified_gmt":"2024-03-01T11:39:54","slug":"database-in-appwrite-using-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/database-in-appwrite-using-python","title":{"rendered":"How to Create a Database in Appwrite Using Python"},"content":{"rendered":"\n<p>Appwrite is an open-source backend platform that reduces a developer&#8217;s effort and time spent building a backend server from scratch. It is a backend-as-a-service solution that handles backend tasks for web, mobile, and Flutter apps.<\/p>\n\n\n\n<p>Appwrite offers databases, authentication, storage, real-time communication, and many other services.<\/p>\n\n\n\n<p>Creating and configuring a database in a web app is time-consuming, and this tutorial will walk you through the process of doing so in simple steps using Appwrite&#8217;s Python SDK (Software Development Kit) or package.<\/p>\n\n\n\n<p>If you don&#8217;t want to deal with preliminary tasks before setting up the database in the Appwrite cloud, you can skip to the database creation section.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-collecting-required-info\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-collecting-required-info\"><\/a>Collecting Required Info<\/h1>\n\n\n\n<p>Before you begin creating a database on the Appwrite cloud, you must first obtain the following credentials from the Appwrite console:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Appwrite API key<\/strong><\/li>\n\n\n\n<li><strong>Appwrite Project ID<\/strong><\/li>\n\n\n\n<li><strong>Appwrite Cloud API Endpoint<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-1-obtaining-project-id\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-step-1-obtaining-project-id\"><\/a>Step 1: Obtaining Project ID<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To gain access to the Appwrite console, create an account on the Appwrite cloud (<a rel=\"noreferrer noopener\" href=\"https:\/\/cloud.appwrite.io\/\" target=\"_blank\">https:\/\/cloud.appwrite.io\/<\/a>) or log in if you already have one.<\/li>\n\n\n\n<li>Click the&nbsp;<strong>&#8220;Create project&#8221;<\/strong>&nbsp;button, give your project a name, and then save the&nbsp;<strong>&#8220;Project ID&#8221;<\/strong>.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1536\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1.png\" alt=\"Project Creation and Saving Project ID\" class=\"wp-image-1190\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1.png 1920w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1-300x240.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1-1024x819.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1-768x614.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1-1536x1229.png 1536w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Account-creation-1-2048x1638.png 2048w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-step-2-obtaining-api-key\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-step-2-obtaining-api-key\"><\/a>Step 2: Obtaining API Key<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>After you&#8217;ve created a new project, scroll down and click the&nbsp;<strong>&#8220;API Key&#8221;<\/strong>&nbsp;button.<\/li>\n\n\n\n<li>Fill in the API name and select the expiration time, then click the&nbsp;<strong>&#8220;Next&#8221;<\/strong>&nbsp;button, then add the scope&nbsp;<strong>&#8220;Database&#8221;<\/strong>, and finally click the&nbsp;<strong>&#8220;Create&#8221;<\/strong>&nbsp;button.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1536\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1.png\" alt=\"API Key Setup\" class=\"wp-image-1191\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1.png 1920w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1-300x240.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1-1024x819.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1-768x614.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1-1536x1229.png 1536w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Setup-1-2048x1638.png 2048w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scroll down the Appwrite console and click on&nbsp;<strong>&#8220;API Keys&#8221;<\/strong>&nbsp;under the&nbsp;<strong>&#8220;Integrations&#8221;<\/strong>&nbsp;section, followed by your newly created&nbsp;<strong>API name<\/strong>.<\/li>\n\n\n\n<li>Copy the project&#8217;s API Key by clicking on the&nbsp;<strong>&#8220;API Key Secret&#8221;<\/strong>&nbsp;button.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1536\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret.png\" alt=\"API Key Secret\" class=\"wp-image-1192\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret.png 1920w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret-300x240.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret-1024x819.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret-768x614.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret-1536x1229.png 1536w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/API-Secret-2048x1638.png 2048w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-installing-appwrites-python-package\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-installing-appwrites-python-package\"><\/a>Installing Appwrite&#8217;s Python Package<\/h1>\n\n\n\n<p>Using Python to create a database on the Appwrite cloud requires a Python package called&nbsp;<code>appwrite<\/code>, which provides API access to interact with the Appwrite backend and perform various tasks.<\/p>\n\n\n\n<p>Open a terminal window and type the following command to install the Python package&nbsp;<code>appwrite<\/code>&nbsp;with&nbsp;<code>pip<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">pip install appwrite<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-a-database\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-creating-a-database\"><\/a>Creating a Database<\/h1>\n\n\n\n<p>Creating a database on the Appwrite cloud involves simple steps. With all the required credentials gathered, let&#8217;s create a database using Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-importing-required-modules\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-importing-required-modules\"><\/a>Importing Required Modules<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from appwrite.client import Client\nfrom appwrite.services.databases import Databases\nfrom appwrite.id import ID\nimport os\nfrom dotenv import load_dotenv<\/pre><\/div>\n\n\n\n<p><code>from appwrite.client import Client<\/code>: To interact with the Appwrite API, the&nbsp;<code>Client<\/code>&nbsp;class is imported from the&nbsp;<code>appwrite.client<\/code>&nbsp;module. The&nbsp;<code>Client<\/code>&nbsp;class will allow you to configure the API key, Project ID, and API endpoint for making Appwrite backend requests.<\/p>\n\n\n\n<p><code>from appwrite.services.databases import Databases<\/code>: To work with Database on the Appwrite cloud, the&nbsp;<code>Databases<\/code>&nbsp;class is imported from the&nbsp;<code>appwrite.services.databases<\/code>&nbsp;module.<\/p>\n\n\n\n<p><code>from appwrite.id import ID<\/code>: To generate unique IDs that can be used within Appwrite, the&nbsp;<code>ID<\/code>&nbsp;class is imported from the&nbsp;<code>appwrite.id<\/code>&nbsp;module.<\/p>\n\n\n\n<p><code>import os<\/code>: To interact with the operating system, the&nbsp;<code>os<\/code>&nbsp;module is imported.<\/p>\n\n\n\n<p><code>from dotenv import load_dotenv<\/code>: To load environment variables to the Python file. The library can be installed with&nbsp;<code>pip install python-dotenv<\/code>, if it is not installed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-configuring-the-appwrite-client-for-api-access\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-configuring-the-appwrite-client-for-api-access\"><\/a>Configuring the Appwrite Client for API Access<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\"\"\" Configuring Appwrite Client \"\"\"\n# Instantiating Appwrite Client\nclient = Client()\n\n# To load environment variables\nload_dotenv()\n\n# Configuring Appwrite Client\n(client\n # Setting API Endpoint\n .set_endpoint('https:\/\/cloud.appwrite.io\/v1')\n # Setting Project ID\n .set_project(os.getenv('PROJECT_ID'))\n # Setting API Key\n .set_key(os.getenv('API_KEY'))\n )<\/pre><\/div>\n\n\n\n<p><strong>Instantiating Appwrite Client<\/strong>: The&nbsp;<code>Client<\/code>&nbsp;class instance is created and stored in the&nbsp;<code>client<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>The environment variables (<code>PROJECT_ID<\/code>&nbsp;and&nbsp;<code>API_KEY<\/code>) are loaded into the script using the&nbsp;<code>load_dotenv()<\/code>&nbsp;function.<\/p>\n\n\n\n<p><strong>Setting API Endpoint<\/strong>: The Appwrite client&#8217;s&nbsp;<code>set_endpoint()<\/code>&nbsp;method is used to set the API endpoint to the URL&nbsp;<code>'<\/code><a target=\"_blank\" href=\"https:\/\/cloud.appwrite.io\/v1\" rel=\"noreferrer noopener\"><code>https:\/\/cloud.appwrite.io\/v1<\/code><\/a><code>'<\/code>.<\/p>\n\n\n\n<p><strong>Setting Project ID<\/strong>: The Appwrite client&#8217;s&nbsp;<code>set_project()<\/code>&nbsp;method is used to set the Project ID of the project by retrieving it from the environment variable&nbsp;<code>'PROJECT ID'<\/code>&nbsp;using&nbsp;<code>os.getenv('PROJECT ID')<\/code>.<\/p>\n\n\n\n<p><strong>Setting API Key<\/strong>: Similarly, the API key is set with the&nbsp;<code>set_key()<\/code>&nbsp;method, and it was retrieved from the environment variable&nbsp;<code>'API_KEY'<\/code>&nbsp;with&nbsp;<code>os.getenv('API_KEY')<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-creating-a-new-database\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-creating-a-new-database\"><\/a>Creating a New Database<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\"\"\" Creating Database \"\"\"\n# Initializing databases service\ndatabases = Databases(client)\n\n# To generate unique database ID\ndb_id = ID.unique()\n\n# Creating a new database\ncreate_db = databases.create(db_id, 'BooksDB')\nprint(\"Database Successfully Created.\")<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>Databases<\/code>&nbsp;instance is created with&nbsp;<code>Databases(client)<\/code>&nbsp;and saved in the&nbsp;<code>databases<\/code>&nbsp;variable. This enables interaction with the Appwrite Database API and the execution of various database-related tasks.<\/p>\n\n\n\n<p>The&nbsp;<code>ID.unique()<\/code>&nbsp;method is used to generate a unique ID for the database, which is then stored in the&nbsp;<code>db_id<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>The code then creates a database by calling the&nbsp;<code>databases.create()<\/code>&nbsp;method, which takes two parameters: the database ID in this case,&nbsp;<code>db_id<\/code>, and the database name, which in this case is&nbsp;<code>'BooksDB'<\/code>.<\/p>\n\n\n\n<p>If you run the file, the database will be created and will be visible on the Appwrite cloud.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1107\" height=\"540\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/db_created.png\" alt=\"Database Created\" class=\"wp-image-1193\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/db_created.png 1107w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/db_created-300x146.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/db_created-1024x500.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/db_created-768x375.png 768w\" sizes=\"auto, (max-width: 1107px) 100vw, 1107px\" \/><\/figure>\n\n\n\n<p>Creating a database is not sufficient, especially if you intend to connect it to a web app. It&#8217;s required for CRUD operations like adding new data, updating it, reading it, and even deleting it.<\/p>\n\n\n\n<p>To create a fully functional database for data storage, the following must be created:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Collections<\/strong><\/li>\n\n\n\n<li><strong>Attributes<\/strong><\/li>\n\n\n\n<li><strong>Documents<\/strong><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-collections\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-creating-collections\"><\/a>Creating Collections<\/h1>\n\n\n\n<p>Collections in the Appwrite database are data storage containers similar to tables in traditional databases.<\/p>\n\n\n\n<p>You can create multiple collections in a single Appwrite database to store and manage data from various sources, which will aid in data management.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\"\"\"Creating Collections\"\"\"\n# Database ID\ndatabase_id = create_db['$id']\n# For Generating Unique Collection ID\ncollection_id = ID.unique()\n\n# Creating a New Collection\nnew_collection = databases.create_collection(database_id=database_id,\n                                             collection_id=collection_id,\n                                             name='Books')\n\nprint('Collection Successfully Created.')<\/pre><\/div>\n\n\n\n<p>The database ID is retrieved using&nbsp;<code>create_db['$id']<\/code>&nbsp;and stored in the&nbsp;<code>database_id<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>The&nbsp;<code>ID.unique()<\/code>&nbsp;method is used to generate a unique ID for the collection in the database and the resulting ID is stored in the&nbsp;<code>collection_id<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>To create a new collection, the&nbsp;<code>databases.create_collection()<\/code>&nbsp;method is used. It accepts three required arguments:&nbsp;<code>database_id<\/code>, which represents the ID of the database where the collection will be created,&nbsp;<code>collection_id<\/code>, a unique ID to ensure no conflicts with existing collections, and&nbsp;<code>name<\/code>, which specifies the name of the new collection, which is&nbsp;<code>\"Books\"<\/code>&nbsp;in this case.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1112\" height=\"551\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/collection_name.png\" alt=\"Collection Created\" class=\"wp-image-1194\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/collection_name.png 1112w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/collection_name-300x149.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/collection_name-1024x507.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/collection_name-768x381.png 768w\" sizes=\"auto, (max-width: 1112px) 100vw, 1112px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-attributes\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-creating-attributes\"><\/a>Creating Attributes<\/h1>\n\n\n\n<p>Following the completion of collections, the next step is to create attributes. The attributes define the schema of the data. Attributes are of different&nbsp;<a target=\"_blank\" href=\"https:\/\/appwrite.io\/docs\/databases#attributes\" rel=\"noreferrer noopener\">types<\/a>, you can choose as per requirement.<\/p>\n\n\n\n<p>Attributes are similar to fields in a traditional database table, where data is stored under the respective field. This ensures a standardized structure of the documents in the Appwrite database.<\/p>\n\n\n\n<p>Since the database is for&nbsp;<strong>Book<\/strong>&nbsp;details, the schema will be as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>id<\/code>&nbsp;&#8211;&nbsp;<code>integer<\/code>: Used to store the book&#8217;s ID.<\/li>\n\n\n\n<li><code>image<\/code>&nbsp;&#8211;&nbsp;<code>url<\/code>: Book image<\/li>\n\n\n\n<li><code>title<\/code>&nbsp;&#8211;&nbsp;<code>string<\/code>: The title of the book<\/li>\n\n\n\n<li><code>author<\/code>&nbsp;&#8211;&nbsp;<code>string<\/code>: The author of the book<\/li>\n\n\n\n<li><code>genre<\/code>&nbsp;&#8211;&nbsp;<code>string<\/code>: The book&#8217;s genre<\/li>\n<\/ul>\n\n\n\n<p>The attributes will be generated based on the fields listed above.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\"\"\"Creating Attributes\"\"\"\n# Collection ID of Book\nc_id = new_collection['$id']\n\n\"\"\" Creating integer attribute \"\"\"\n# ID Attribute\nbook_id = databases.create_integer_attribute(database_id=database_id,\n                                             collection_id=c_id,\n                                             key=\"id\",\n                                             required=True)\n\n\"\"\" Creating url attribute \"\"\"\n# URL Attribute\nbook_url = databases.create_url_attribute(database_id=database_id,\n                                          collection_id=c_id,\n                                          key=\"image\",\n                                          required=True)\n\n\"\"\" Creating string attribute \"\"\"\n# Title Attribute\nbook_title = databases.create_string_attribute(database_id=database_id,\n                                               collection_id=c_id,\n                                               key=\"title\",\n                                               required=True,\n                                               size=100)\n\n# Author Attribute\nbook_author = databases.create_string_attribute(database_id=database_id,\n                                                collection_id=c_id,\n                                                key=\"author\",\n                                                required=True,\n                                                size=50)\n\n# Genre Attribute\nbook_genre = databases.create_string_attribute(database_id=database_id,\n                                               collection_id=c_id,\n                                               key=\"genre\",\n                                               required=True,\n                                               size=50)\n\nprint(\"Attributes Successfully Created.\")<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>create_integer_attribute()<\/code>&nbsp;method is used to create the integer attribute&nbsp;<strong>&#8220;id&#8221;<\/strong>&nbsp;for the&nbsp;<strong>&#8220;Books&#8221;<\/strong>&nbsp;collection. This method is invoked with four mandatory arguments:&nbsp;<code>database_id<\/code>&nbsp;(set to&nbsp;<code>database_id<\/code>),&nbsp;<code>collection_id<\/code>&nbsp;(set to&nbsp;<code>c_id<\/code>),&nbsp;<code>key<\/code>&nbsp;(set to&nbsp;<code>\"id\"<\/code>&nbsp;as the attribute name), and&nbsp;<code>required<\/code>&nbsp;(set to&nbsp;<code>True<\/code>&nbsp;to indicate that the value of this attribute cannot be left empty).<\/p>\n\n\n\n<p>The&nbsp;<code>create_url_attribute()<\/code>&nbsp;method is used to create the URL attribute&nbsp;<strong>&#8220;image&#8221;<\/strong>&nbsp;for the&nbsp;<strong>&#8220;Books&#8221;<\/strong>&nbsp;collection. This method is also called with four mandatory arguments:&nbsp;<code>database_id<\/code>,&nbsp;<code>collection_id<\/code>,&nbsp;<code>key<\/code>&nbsp;set to&nbsp;<code>\"image\"<\/code>, and&nbsp;<code>required<\/code>&nbsp;parameter set to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>Finally, the&nbsp;<code>create_string_attribute()<\/code>&nbsp;method is used to create three string attributes, namely&nbsp;<strong>&#8220;title&#8221;<\/strong>,&nbsp;<strong>&#8220;author&#8221;<\/strong>, and&nbsp;<strong>&#8220;genre&#8221;<\/strong>&nbsp;for the&nbsp;<strong>&#8220;Books&#8221;<\/strong>&nbsp;collection. This method requires five parameters:&nbsp;<code>database_id<\/code>,&nbsp;<code>collection_id<\/code>,&nbsp;<code>key<\/code>,&nbsp;<code>required<\/code>, and&nbsp;<code>size<\/code>, which is the maximum number of characters allowed for the attribute.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1113\" height=\"570\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1.jpeg\" alt=\"Attributes Created\" class=\"wp-image-1195\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1.jpeg 1113w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1-300x154.jpeg 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1-1024x524.jpeg 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1-768x393.jpeg 768w\" sizes=\"auto, (max-width: 1113px) 100vw, 1113px\" \/><\/figure>\n\n\n\n<p>Now that all processes have been completed and the database is ready, you can add data from the frontend and store it as documents within the database.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-adding-documents\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-adding-documents\"><\/a>Adding Documents<\/h1>\n\n\n\n<p>Following the schema that was created in the previous section, data will be added programmatically within the collection called&nbsp;<strong>&#8220;Books&#8221;<\/strong>&nbsp;in the database called&nbsp;<strong>&#8220;BooksDB&#8221;<\/strong>&nbsp;in this section.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\"\"\" Adding Documents \"\"\"\n# Unique Identifier for Document ID\ndocument_id = ID.unique()\n\n\"\"\" Function for Adding Documents(data) in the Database \"\"\"\ndef add_doc(document):\n    try:\n        doc = databases.create_document(\n            database_id=database_id,\n            collection_id=c_id,\n            document_id=document_id,\n            data=document\n        )\n\n        print(\"Id:\", doc['id'])\n        print(\"Image:\", doc['image'])\n        print(\"Title:\", doc['title'])\n        print(\"Author:\", doc['author'])\n        print(\"Genre:\", doc['genre'])\n        print(\"-\" * 20)\n\n    except Exception as e:\n        print(e)<\/pre><\/div>\n\n\n\n<p>The code defines an&nbsp;<code>add_doc<\/code>&nbsp;function that takes a single argument named&nbsp;<code>document<\/code>. Within the function&#8217;s&nbsp;<code>try<\/code>&nbsp;block, the&nbsp;<code>databases.create_document()<\/code>&nbsp;method is invoked to generate a document, which is subsequently stored in the&nbsp;<code>doc<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>This method requires four mandatory parameters:&nbsp;<code>database_id<\/code>&nbsp;(set as&nbsp;<code>database_id<\/code>),&nbsp;<code>collection_id<\/code>&nbsp;(set as&nbsp;<code>c_id<\/code>),&nbsp;<code>document_id<\/code>&nbsp;(representing a unique ID for the document, specifically assigned as&nbsp;<code>document_id<\/code>), and&nbsp;<code>data<\/code>&nbsp;(representing the document&#8217;s content formatted in a dictionary).<\/p>\n\n\n\n<p>The&nbsp;<code>doc<\/code>&nbsp;variable stores a dictionary returned by the&nbsp;<code>databases.create_document()<\/code>. Utilizing this&nbsp;<code>doc<\/code>&nbsp;variable, the code retrieves the&nbsp;<strong>id<\/strong>,&nbsp;<strong>image<\/strong>,&nbsp;<strong>title<\/strong>,&nbsp;<strong>author<\/strong>, and&nbsp;<strong>genre<\/strong>&nbsp;of the added document and prints them.<\/p>\n\n\n\n<p>If an exception arises during the&nbsp;<code>try<\/code>&nbsp;block&#8217;s execution, the code captures the error and displays an error message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-data\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-data\"><\/a>Data<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Data To Be Added\nbook_1 = {\n    \"id\": 1,\n    \"image\": \"https:\/\/i.pinimg.com\/474x\/dc\/17\/2d\/dc172d6fa3f5461d94e6d384aded2cb4.jpg\",\n    \"title\": \"The Great Gatsby\",\n    \"author\": \"F. Scott Fitzgerald\",\n    \"genre\": \"Fiction\"\n}\n\nbook_2 = {\n    \"id\": 2,\n    \"image\": \"https:\/\/i.pinimg.com\/originals\/0b\/bf\/b5\/0bbfb59b4d5592e2e7fac9930012ce6d.jpg\",\n    \"title\": \"To Kill a Mockingbird\",\n    \"author\": \"Harper Lee\",\n    \"genre\": \"Fiction\"\n}\n\nbook_3 = {\n    \"id\": 3,\n    \"image\": \"https:\/\/i.pinimg.com\/736x\/66\/1d\/17\/661d179ab722e67eed274d24b8965b0d.jpg\",\n    \"title\": \"Pride and Prejudice\",\n    \"author\": \"Jane Austen\",\n    \"genre\": \"Romance\"\n}\n\nbook_4 = {\n    \"id\": 4,\n    \"image\": \"https:\/\/i.pinimg.com\/originals\/68\/c5\/4c\/68c54c9599ba37d9ab98c0c51afe2298.png\",\n    \"title\": \"Crime and Punishment\",\n    \"author\": \"Fyodor Dostoevsky\",\n    \"genre\": \"Psychological Fiction\"\n}\n\n# Calling function with the data to be added\nadd_doc(book_1)\nadd_doc(book_2)\nadd_doc(book_3)\nadd_doc(book_4)\nprint(\"Documents Successfully Added.\")<\/pre><\/div>\n\n\n\n<p>When you run the whole code, the following output will be prompted and the database, collection, and attributes will be created and documents will then be added.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Database Successfully Created.\nCollection Successfully Created.\nAttributes Successfully Created.\nId: 1\nImage: https:\/\/i.pinimg.com\/474x\/dc\/17\/2d\/dc172d6fa3f5461d94e6d384aded2cb4.jpg\nTitle: The Great Gatsby\nAuthor: F. Scott Fitzgerald\nGenre: Fiction\n--------------------\nId: 2\nImage: https:\/\/i.pinimg.com\/originals\/0b\/bf\/b5\/0bbfb59b4d5592e2e7fac9930012ce6d.jpg\nTitle: To Kill a Mockingbird\nAuthor: Harper Lee\nGenre: Fiction\n--------------------\nId: 3\nImage: https:\/\/i.pinimg.com\/736x\/66\/1d\/17\/661d179ab722e67eed274d24b8965b0d.jpg\nTitle: Pride and Prejudice\nAuthor: Jane Austen\nGenre: Romance\n--------------------\nId: 4\nImage: https:\/\/i.pinimg.com\/originals\/68\/c5\/4c\/68c54c9599ba37d9ab98c0c51afe2298.png\nTitle: Crime and Punishment\nAuthor: Fyodor Dostoevsky\nGenre: Psychological Fiction\n--------------------\nDocuments Successfully Added.<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1144\" height=\"573\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2.jpeg\" alt=\"Documents Added\" class=\"wp-image-1196\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2.jpeg 1144w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2-300x150.jpeg 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2-1024x513.jpeg 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2-768x385.jpeg 768w\" sizes=\"auto, (max-width: 1144px) 100vw, 1144px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-source-code\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-source-code\"><\/a>Source Code<\/h1>\n\n\n\n<p>Access the full source code from the following GitHub repository, clone or download the code and run it in your favorite IDE.<\/p>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/github.com\/Sachin-crypto\/PythonxAppwrite\" rel=\"noreferrer noopener\">Python Script For Creating Appwrite Database<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>The tutorial walked you through the steps of setting up a new database in the Appwrite cloud. It also includes instructions for creating a new project, creating an API key for the project, and obtaining the project ID and API key from the Appwrite cloud.<\/p>\n\n\n\n<p>Following the creation of the database, the tutorial takes you through the steps of making it fully functional by adding collections and attributes. The documents (data) are then added programmatically.<\/p>\n\n\n\n<p>Let&#8217;s go over the steps in this tutorial for creating a new database:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Obtaining the necessary Appwrite cloud credentials<\/strong><\/li>\n\n\n\n<li><strong>Installing the Python package<\/strong>&nbsp;<code>appwrite<\/code><\/li>\n\n\n\n<li><strong>Making a database<\/strong><\/li>\n\n\n\n<li><strong>Making a collection<\/strong><\/li>\n\n\n\n<li><strong>Adding the attributes<\/strong><\/li>\n\n\n\n<li><strong>Adding the documents programmatically<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/integrate-postgresql-database-in-python\" rel=\"noreferrer noopener\">How to connect the PostgreSQL database with Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/render-images-from-flask\" rel=\"noreferrer noopener\">Upload and display images on the frontend using Flask in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/flask-app-for-image-recognition\" rel=\"noreferrer noopener\">Building a Flask image recognition webapp using a deep learning model<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model\" rel=\"noreferrer noopener\">Building a custom deep learning model using transfer learning<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/data-augmentation-in-deep-learning\" rel=\"noreferrer noopener\">How to augment the data for training using Keras and Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/argparse-in-python\" rel=\"noreferrer noopener\">How to build a CLI command in a few steps using argparse in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/asyncio-how-to-use-asyncawait-in-python\" rel=\"noreferrer noopener\">How to use Async\/Await like JavaScript in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/web-scraping-in-python-using-beautifulsoup\" rel=\"noreferrer noopener\">How to scrape a webpage&#8217;s content using BeautifulSoup in Python<\/a>?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Appwrite is an open-source backend platform that reduces a developer&#8217;s effort and time spent building a backend server from scratch. It is a backend-as-a-service solution that handles backend tasks for web, mobile, and Flutter apps. Appwrite offers databases, authentication, storage, real-time communication, and many other services. Creating and configuring a database in a web app [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1181,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,37],"tags":[38,12,31],"class_list":["post-1179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-database","tag-database","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1179"}],"version-history":[{"count":6,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1179\/revisions"}],"predecessor-version":[{"id":1646,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1179\/revisions\/1646"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1181"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}