0

I have an array of objects in a crate table and when I make a SELECT and fetchall() command, it gives me the following list of dictionaries:

[{"key": "two", "id": "1", "value": "three", "tag": False}, etc..]

After that I want to change a value from one of these dictionaries and update the array using sql UPDATE. In hard-code it should look like this:

cur.execute('UPDATE servers SET 
tags =[{key= "two", id= "1", value= "three", tag= False}, etc..]')

It means that I have to parse this dictionary:

{"key": "two", "id": "1", "value": "three", "tag": False}

Into a string like this:

{key = "two", id = "1", value = "three", tag = False}

No " for the keys, = instead of : . How could I do it in python?

Vasile
  • 801
  • 2
  • 13
  • 31

2 Answers2

1

You don't need to convert the dictionary to the Crate specific object syntax if you use the parameter support. (Which you really should use anyway)

If you only need to update a single value of the dictionary you could do something like this:

cur.execute("update servers set tags['value'] = ?", ('three',))

In your case with a list of dictionaries you need to update the whole value, like this:

cur.execute("update servers set tags = ?", (updated_list,))
mfussenegger
  • 3,931
  • 23
  • 18
0

I would suggest using an ORM such as SQLAlchemy to connect to the crate database, which seems to be supported out of the box. Then, you can use python objects to update the database values. This would save you trouble of converting types to the appropriate schema by hand.

bear
  • 123
  • 1
  • 9