skip to Main Content

I wanted to create a Python file that has multiple functions for creating graphs, nodes and edges. The code that I wrote only includes the function for creating a graph:

import psycopg2

GRAPH_NAME = "test_graph"
conn = psycopg2.connect(host="localhost", port="5432", dbname="demo", user="postgres", password="password")


def create_graph(graph_name):
    with conn.cursor() as cursor:
        try:
            cursor.execute("SELECT * FROM ag_catalog.create_graph(%s);", (graph_name,))
            conn.commit
        except Exception as ex:
            print(type(ex), ex)
            conn.rollback()

create_graph('testing_py')

But, when I run it, the graph is not created and it throws no errors. What is the proper way to create graphs via python scripts?

2

Answers


  1. You should call conn.commit method, you have forgotten the ()

    it should be replaced with

    conn.commit()
    
    Login or Signup to reply.
  2. You forgot to add parenthesis to the commit method and import age in the script

    Alternatively, it can be created like so:

    import psycopg2
    from age import *
    
    conn = psycopg2.connect(host="localhost", port="5432", dbname="demo", user="postgres", password="password")
    
    def create_graph(graph_name):
        age.setUpAge(conn, graph_name)
    
    create_graph('testing_py')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search