簡體   English   中英

我的Postgresql數據庫不響應python命令

[英]My postgresql database does not respond to python commands

我正在使用Python(PyCharm)應用SQL命令,由於某種原因,我無法理解以下方法無法執行:

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))
    print('here save_to_db')    # as a trace
    connection.commit()
    connection.close()

從以下腳本中調用它:

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()

無論是更新數據庫,還是用作跟蹤的print命令都無法在pycharm的運行窗口中提供任何結果。 即使我使用無效的密碼,也不會出現錯誤,而是顯示“進程以退出代碼0完成”。 但是,該方法一次有效,這是我第一次應用它,使我可以在數據庫中保存一些數據。 從那時起,我僅通過pgadmin4從數據庫獲得響應。 如果能在此問題上獲得幫助,我將感到非常高興。 先感謝您。

對於相同的問題,我在上一個線程中可能還有更多詳細信息: python-postgresql,似乎已連接,但沒有響應

編輯:我正在放置應用程序的完整代碼。 具有User類的文件如下:

import psycopg2


class User:
def __init__(self, email, first_name, last_name, id):
    self.email = email
    self.first_name = first_name
    self.last_name = last_name
    self.id = id

def __repr__(self):
    return "<User {}>".format(self.email)

def save_to_db(self):
    with 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))
    print('here save_to_db')    # as a trace to define wether or not the method is executed


@classmethod
def load_from_db_by_email(cls, email):
    with psycopg2.connect(user='postgres', password='valid', database='learning', host='localhost') as connection:
        with connection.cursor() as cursor:
            cursor.execute('SELECT * FROM users WHERE email=%s', (email,))
            user_data = cursor.fetchone()
            return cls(user_data[1], user_data[2], user_data[3], user_data[0])

並且下面是同時調用這兩種方法的文件。

from user import User

saveToDb = 1 # 1 to save, else to load

if saveToDb==2:
    print('save')    # a print method again as a trace
    my_user = User('email@email.com', 'first_name', 'last_name', None)
    my_user.save_to_db()
else:
    print('load')    # a print method again as a trace
    my_user = User.load_from_db_by_email('email@email.com')
    print(my_user)

再次,我將打印方法用作跟蹤,結果是調用該方法的代碼也未執行。

看起來正在生成異常,您沒有看到它。 或包裝此函數的某些代碼正在捕獲它的except:而不是報告它。

這個變量如何:

編輯:添加了幾個print()s。

def save_to_db(self):
    print("DEBUG: ENTER save_to_db()");
    rc = False
    my_database = 'learning'
    try:
        connection = psycopg2.connect(user='postgres', password='zSvG3$', database=my_database, host='localhost')
    except Exception as e:
        print("Unable to connect to [" + my_database + "] - " + str(e))
        connection = None

    if (connection != None):
        cursor = connection.cursor()
        try:
            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()
            rc = True
        except Exception as e:
            print("INSERT FAILED - "+str(e))
    print("DEBUG: EXIT save_to_db()");
    return rc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM