Reputation: 1180
I have Memgraph Lab installed on my computer as well as mgconsole
. I know how to connect to Memgraph Cloud using them, but I'd like to connect to them using Python. How can I do that?
Upvotes: 0
Views: 62
Reputation: 1180
First, you will to install a Python driver. You will actually use GQLAlchemy.
You can use pip (pip install gqlalchemy
) or poetry (poetry add gqlalchemy
).
In the following code, replace YOUR_MEMGRAPH_PASSWORD
, YOUR_MEMGRAPH_USERNAME
and MEMGRAPH_HOST_ADDRESS
with your values:
from gqlalchemy import Memgraph
MEMGRAPH_HOST = 'MEMGRAPH_HOST_ADDRESS'
MEMGRAPH_PORT = 7687
MEMGRAPH_USERNAME = 'YOUR_MEMGRAPH_USERNAME'
# Place your Memgraph password that was created during Project creation
MEMGRAPH_PASSWORD = 'YOUR_MEMGRAPH_PASSWORD'
def hello_memgraph(host: str, port: int, username: str, password: str):
connection = Memgraph(host, port, username, password, encrypted=True)
results = connection.execute_and_fetch(
'CREATE (n:FirstNode { message: "Hello Memgraph from Python!" }) RETURN n.message AS message'
)
print("Created node with message:", next(results)["message"])
if __name__ == "__main__":
hello_memgraph(MEMGRAPH_HOST, MEMGRAPH_PORT, MEMGRAPH_USERNAME, MEMGRAPH_PASSWORD)
Upvotes: 0