Gianni
Gianni

Reputation: 11

parameterize queries in OrientDB using placeholders

I am using pyorient and and want to parameterize queries -- I am assuming that command() allows placeholders, but am not able to find any documentation. IN particular I'd like to use dict() args as per postgres' %(name)s construct, but could make tuples/lists work as well.

Upvotes: 1

Views: 308

Answers (1)

LucaS
LucaS

Reputation: 1418

I tried your case with my python_test database:

Dataset:

enter image description here

I used two parameters:

  • name ---> string;
  • surname ---> string;

and I passed them into the command() function.

PyOrient Code:

import pyorient 

db_name = 'python_test'
user = 'root'
pwd = 'root'

print("Connecting...")
client = pyorient.OrientDB("localhost",2424)
session_id = client.connect(user, pwd)
print("OK - sessionID: ",session_id,"\n")

if client.db_exists( db_name, pyorient.STORAGE_TYPE_PLOCAL ):
        print("DB "+db_name+" already exists\n")
        client.db_open(db_name, user, pwd, pyorient.DB_TYPE_GRAPH, pyorient.STORAGE_TYPE_PLOCAL)
        name = 'test name'
        surname = 'test surname'
        vertexes = client.command("SELECT FROM TestClass WHERE name = '" + name + "' AND surname = '" + surname + "'")
        for vertex in vertexes:
            print(vertex)
else:
        print("Creating DB "+ db_name + "...")
        client.db_create( db_name, pyorient.DB_TYPE_GRAPH, pyorient.STORAGE_TYPE_PLOCAL )
        print("DB " + db_name + " created\n")

client.db_close()

Output:

Connecting...
OK - sessionID:  40 

DB python_test already exists

{'@TestClass':{'surname': 'test surname', 'name': 'test name'},'version':1,'rid':'#12:0'}

Hope it helps

Upvotes: 1

Related Questions