My inserts are not being saved

Asked

Viewed 25 times

0

I installed a postgre cluster using pgpool in Docker containers, https://github.com/paunin/PostDock. I can connect to pgpool and make insertions and other normal sql commands.

psql monkey_db -h localhost -U monkey_user -p 5430

However, when trying to connect the database using python psycopg2, I make insertions and apparently are saved, but I exit the connection and look at psql and see that nothing was saved. When returning to python I see that nothing was saved.

(docker) root@prisvo-asus:~# psql monkey_db -h localhost -U monkey_user -p 5430
monkey_db=# create table teste(num bigint);
CREATE TABLE
monkey_db=# insert into teste values(123);
INSERT 0 1
monkey_db=# select * from teste;
 num
----
123
(1 row)
monkey_db=# \q
(docker) root@prisvo-asus:~# ipython

In [1]: import psycopg2
In [2]: con = psycopg2.connect(dbname='monkey_db',user='monkey_user',password='monkey_pass', host='localhost',port='5430')
In [3]: cur = con.cursor()
In [4]: query = 'select * from teste;' 
In [5]: cur.execute(query);
In [6]: rows = cur.fetchall()
In [7]: for row in rows:
    ..:     print(row[0])
123
In [8]: query = 'insert into teste values(1111)';
In [9]: cur.execute(query);
In [10]: query = 'select * from teste;' 
In [11]: cur.execute(query);
In [12]: rows = cur.fetchall()
In [13]: for row in rows:
    ..:     print(row[0])
123
1111
In [14]: exit()
(docker) root@prisvo-asus:~# psql monkey_db -h localhost -U monkey_user -p 5430
monkey_db=# select * from teste;
 num
----
123
(1 row)
monkey_db=# \q
(docker) root@prisvo-asus:~# ipython
In [1]: import psycopg2
In [2]: con = psycopg2.connect(dbname='monkey_db',user='monkey_user',password='monkey_pass', host='localhost',port='5430')
In [3]: cur = con.cursor()
In [4]: query = 'select * from teste;' 
In [5]: cur.execute(query);
In [6]: rows = cur.fetchall()
In [7]: for row in rows:
    ..:     print(row[0])
123

1 answer

1


You need to give a commit to save changes to the bank.

cur.execute(query);
con.commit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.