-1

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?

Matheus Farias
  • 716
  • 1
  • 10

6 Answers6

1

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

it should be replaced with

conn.commit()
1

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')
Tito
  • 289
  • 8
-1

You forgot the parenthesis. here is the updated working code which will resolve the problem

import psycopg2
from age import *

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')
Humza Tareen
  • 146
  • 6
-1

Your code is fine, only that you need to invoke the commit method on session connection. conn.commit()

Peter
  • 43
  • 4
-1

You are missing brackets after your con.commit function . The correct way is this con.commit () . I hope this would solve your problem.

-1

The graph was not created because the age module has not been imported and also the commit method was not properly called. In order to remedy this you have to import age using the import statement; from age import * and use conn.commit() to save to the database.