I am applying SQL commands with Python (PyCharm), but without getting any error, I get no respond from the database. Even if deliberately I use wrong password still no error, and in the run window I get "Process finished with exit code 0". There are two methods that I applied, the first initially worked properly. When I use pgAdmin4, I can apply the SQL commands without any problem. Two block of code are following, and they are for the same method. The first version worked once, but then I encounter what I'm describing above. It mustn't be the absence of commit() command, because I am using 'with' for the connection in the proper way I think. That's for the first block, because in the second I use commit(), with the same outcome.
def save_to_db(self):
connection = psycopg2.connect(user='postgres', password='valid', database='learning', host='localhost')
with connection.cursor() as cursor:
cursor.execute('INSERT INTO users (email, first_name, last_name) VALUES (%s, %s, %s)', (self.email, self.first_name, self.last_name))
connection.commit()
connection.close()
And the second one (with commit() instead of using a 'with' for the connection), also without the expected outcome
def save_to_db(self):
with psycopg2.connect(user='postgres', password='valid', database='learning', host='localhost') as connection:
with connection.cursor() as cursor:
cursor.execute('INSERT INTO users (email, first_name, last_name) VALUES (%s, %s, %s)', (self.email, self.first_name, self.last_name))
I would be glad if I could understand what is wrong. Thanks in advance.
edit: Here is the script from which I run the methods:
from user import User # the class where the methods are
my_user = User('email@email.com', 'first name', 'last name', None) # in the database there is auto-increment for the primary key (4th argument)
my_user.save_to_db()