How to insert/update JSON data into postgreSQL - python

This is my table schema,
[column] [type]
tablename json
word varchar
text json
I implemented using psycopg2 with Python,
cur.execute("INSERT INTO json (word,text) VALUES (%s,%s);",(word,text))
word contains list object type but inside are string,
['a','b','c']
text contains list object type but inside is dict (json),
[{'a':'b'},{'c':'d'}]
When I run the function. I got this error wanring below,
" can't adapt type 'dict' "
The question is, How to insert json into postgreSQL, As you see type of text. It's look like dict, But how to assign text variable is json?. or I'm missing something?

json.dumps() could be used to switch to a string for the database.
import json
postgres_string = json.dumps(text)
# Save postres_string into postgress here
# When you want to retrieve the dictionary, do so as below:
text = json.loads(postgres_string)

Well,you use execute function to execute a SQL, just construct the right SQL, it would get success.You want insert a json type data, just use "::" to transform a string type into a json type, like below, it works:
postgres=# insert into json_test(word,text) values('abcd_word','[{"a":"b"},{"c":"d"}]'::json);
INSERT 0 1
postgres=#
postgres=# select * from json_test ;
tablename | word | text
-----------+-----------+-----------------------
| abcd_word | [{"a":"b"},{"c":"d"}]

Related

insert list,values into mysql table

I need to insert the list,some values into table
I have tried executemany but it's not worked.
list1=['a','b','c','d','e',['f','g','h','i']]
query="insert into "+metadata_table_name+"(created_by,Created_on,File_Path,Category,File_name,Fields) values(%s,%s,%s,%s,%s,%s)" # inserting the new record
cursor.executemany(query,list1)
list should be entered into the last(Fileds) Column
Please help me.
Thanks in Advance.
You have to think about data types. Does MySQL have a suitable data type for python's nested lists? I don't know such types.
A possible solution is to use JSON encoding and storing the lists as strings in MySQL table. Encode last element of your list to JSON string:
import json
list1=['a','b','c','d','e',['f','g','h','i']]
query_params = list1[0:-1] + [json.dumps(list1[:-1])]
query="insert into "+metadata_table_name+" .(created_by,Created_on,File_Path,Category,File_name,Fields) values(%s,%s,%s,%s,%s,%s)" # inserting the new record
cursor.executemany(query, query_params)
For using stored data later you have to convert back JSON string to a list:
fields_list = json.loads(fields_str)

Can Postgres resolve character escapes passed down from RESP API to JSONB?

I have a Python API that inserts data into Postgres table using stored procedure that takes in a jsonb and does an insert into table. example:
create function insert_new_employee(i jsonb) returns json as:
$$
begin
insert into newhire(name, payload) values (i ->> 'name', i)
end
$$
When I do a test insert using SQL client such as datagrip as such it inserts successfully:
select insert_new_employee('{"name":"alfred"}')
However, when I use Python API the payload gets transformed into:
"{\"name\":\"alfred\"}"
Because of the escaping the name doesn't get parsed and stored into the name column however the jsonb payload column takes it in.
Is there a way for Postgres to clean up and deal with character escape when request are passed through API?

Update jsonb field in PostgreSQL with json that contains single quote

I'm using PostgreSQL v9.4.12 and I'm trying to update a jsonb column. I want to update the whole json object and not a specific key of the object.
I'm using a Python dict to store my object and before using it I'm using json.dumps() to transform it to a json formatted String.
However, a value of the json is having a single quote ' that throws an psycopg2.ProgrammingError: syntax error while trying to update.
So far, I've tried:
"UPDATE table "
"SET jsonb_column='{} ".format(json.dumps(new_data)) + ""
"WHERE id='12345'"
Note that new_data is my dict and jsonb_column is the name of the column holding the json data.
The error I'm getting:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...code": "BR3", "short_description": "This property's price
is...
^
I was assuming that json.dumps() escapes the single quote but doesn't seem that to be the case. Is there any solution to overcome this error?
Thanks in advance.
json is very fine with single quote, eg:
t=# select $${"short_description": "This property's price is..."}$$::jsonb;
jsonb
------------------------------------------------------
{"short_description": "This property's price is..."}
(1 row)
so I assume you could try using dollar sign quotes, to avoid statement structuring exception with single quotes
The practice of string concatenation is not a good practice.
Better use the way documented in PsyCoPg2 docs.
cur.execute("UPDATE table SET jsonb_column = %s WHERE id = %s", [json, id])

sqlalchemy + postgresql hstore to string

How do I convert an sqlalchemy hstore value to a string?
from sqlalchemy.dialects.postgresql import array, hstore
hs = hstore(array(['key1', 'key2', 'key3']), array(['value1', 'value2', 'value3']))
# this triggers sqlalchemy.exc.UnsupportedCompilationError
str(hs)
I expect something like "key1"=>"value1", "key2"=>"value2", "key3"=>"value3"
I would like to use an sqlalchemy api rather than write a custom string formatting function that approximates what I want. I'm working with a legacy code base that uses sqlalchemy: I need to preserve any internal quirks and escaping logic that formatting does.
However, the existing code base uses sqlalchemy via an ORM table insert, while I want to directly convert an sqlalchemy hstore value to a string?
UPDATE: I am trying to do something like this:
I have an existing table with schema
create table my_table
(
id bigint default nextval('my_table_id_seq'::regclass),
ts timestamp default now(),
text_col_a text,
text_col_b text
);
I want to get the following Python sqlalchemy code working:
str_value = some_function()
# Existing code is building an sqlalchemy hstore and inserting
# into a column of type `text`, not an `hstore` column.
# I want it to work with hstore text formatting
hstore_value = legacy_build_my_hstore()
# as is this triggers error:
# ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'hstore'
return db_connection.execute(
"""
insert into my_table(text_col_a, text_col_b) values (%s, %s)
returning id, ts
""",
(str_value, hstore_value).first()
Let Postgresql do the cast for you instead of trying to manually convert the hstore construct to a string, and SQLAlchemy handle the conversion to suitable text representation:
return db_connection.execute(
my_table.insert().
values(text_col_a=str_value,
text_col_b=cast(hstore_value, Text)).
returning(my_table.c.id, my_table.c.ts)).first()
As soon as you can, alter your schema to use hstore type instead of text, if that is what the column contains.

plpython function to update a row is not working

I am using PostgreSQL 9.4.7 and Python 2.7.6. I am writing a plpython function to update a row in user table and my code is as below -
CREATE FUNCTION update_user(myid int, mymail text, myname text) RETURNS text AS $$
from plpy import spiexceptions
plan=plpy.prepare("UPDATE auth_user SET email=$2, username=$3 WHERE id = $1",
["int"] ["text"]["text"])
rv=plpy.execute(plan, [myid,myemail,myusername])
return rv
$$ LANGUAGE plpythonu;
I am able to create this function successfully in postgres DB but while I am trying to execute it via below command on postgres shell-
select update_user(1,"xyz#xyz.com#sifymail.com","updatedname");
I am getting following error -
ERROR: column "amarshukla#sifymail.com" does not exist
LINE 1: select update_user(1,"amarshukla#sifymail.com","hell");
Can someone point me where am I making a mistake?
First of all, string literals in postgresql must be surrounded by single quotes, not the double ones:
select update_user(1,'xyz#xyz.com#sifymail.com','updatedname');
Double quotes are used to refer columns.
Second. The arguments list in your prepare function isn't correct python list. It should look like:
plpy.prepare("UPDATE auth_user SET email=$2, username=$3 WHERE id = $1",
["int", "text", "text"])

Categories