From 98559c8f8c924d04237b693369b3dfa5ab2a2af5 Mon Sep 17 00:00:00 2001 From: Katarina Supe <61758502+katarinasupe@users.noreply.github.com> Date: Thu, 4 Jan 2024 11:54:59 +0100 Subject: [PATCH] Update connect to Memgraph (#297) * Update connect to Memgraph * Update link to installation guide * Remove prerequisite --- docs/index.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 8daad5c0..46acb848 100644 --- a/docs/index.md +++ b/docs/index.md @@ -18,7 +18,70 @@ source](installation.md#source). If you are using [Conda](https://docs.conda.io/ ### 2. Connect to Memgraph -Check the [Python quick start guide](https://memgraph.com/docs) to learn how to connect to Memgraph using GQLAlchemy. +**Prerequisites** + +To follow this guide, you will need: + +- A **running Memgraph instance**. If you need to set up Memgraph, take a look + at the [Installation guide](https://memgraph.com/docs/getting-started/install-memgraph). + +**Basic setup** + +We'll be using a **Python program** to demonstrate how to connect to a running +Memgraph database instance.
+ +Let's jump in and connect a simple program to Memgraph. + +**1.** Create a new directory for your program, for example, `/memgraph_python` +and position yourself in it.
+ +**2.** Create a new Python script and name it `program.py` . Add the following +code to it: + +```python +from gqlalchemy import Memgraph + +# Make a connection to the database +memgraph = Memgraph(host='127.0.0.1', port=7687) + +# Delete all nodes and relationships +query = "MATCH (n) DETACH DELETE n" + +# Execute the query +memgraph.execute(query) + +# Create a node with the label FirstNode and message property with the value "Hello, World!" +query = """CREATE (n:FirstNode) + SET n.message = '{message}' + RETURN 'Node ' + id(n) + ': ' + n.message AS result""".format(message="Hello, World!") + +# Execute the query +results = memgraph.execute_and_fetch(query) + +# Print the first member +print(list(results)[0]['result']) +``` + +!!! note + If the program fails to connect to a Memgraph instance that was started with Docker, you may need to use a different IP address (not the default `localhost` / `127.0.0.1` ) to connect to the instance. You can find the **`CONTAINER_ID`** with `docker ps` and use it in the following command to retrieve the address: + ``` + docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_ID + ``` + + +**3.** Now, you can run the application with the following command: + +``` +python ./program.py +``` + +You should see an output similar to the following: + +``` +Node 1: Hello, World! +``` + + ### 3. Learn how to use GQLAlchemy