Python Psycopg2 |在数据库中计数加上

发布于 2025-02-12 21:01:27 字数 702 浏览 0 评论 0原文

我得到了带有自旋框和一个按钮的Pyqt5 GUI。我已经写了一篇文字,当我单击按钮时,这就是数据库中的Spinbox中的数字。 可悲的是,它代替了我的旧价值。在我的数据库中,我得到了一个名为“小时”的值。 假设该值现在为12。如果我现在单击我的按钮,它应该从GUI中输入的Spinbox中添加我的号码到12。

    def test():
        conn = None
        try:
            conn = psycopg2.connect("dbname=ueberstunden user=postgres password=admin")
            value = self.box_test.value()
            cur = conn.cursor()
            cur.execute("UPDATE ueberstunden SET stunden = %s WHERE name ='test'", value)
            conn.commit()
            cur.close()
        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
        finally:
            if conn is not None:
                conn.close()

I got a PyQt5 GUI with a spinbox and a button. I already wrote a statement that writes the number, thats in the spinbox in the database, when I click my button.
Sadly it substitutes my old value. In my database I got a value named "hours".
Let's say the value right now is 12. And if I click my button now it should add my number from the spinbox I typed in the GUI to the 12.

    def test():
        conn = None
        try:
            conn = psycopg2.connect("dbname=ueberstunden user=postgres password=admin")
            value = self.box_test.value()
            cur = conn.cursor()
            cur.execute("UPDATE ueberstunden SET stunden = %s WHERE name ='test'", value)
            conn.commit()
            cur.close()
        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
        finally:
            if conn is not None:
                conn.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2025-02-19 21:01:27

您可以在作业右侧使用表达式。在这里,您需要用当前值分配studen,即,studen加上新值:

cur.execute("UPDATE ueberstunden SET stunden = studen + %s WHERE name ='test'", value)
# Here ----------------------------------------^

You can use expressions on the right side of an assignment. Here, you'd want to assign studen with the current value, i.e., studen plus the new value:

cur.execute("UPDATE ueberstunden SET stunden = studen + %s WHERE name ='test'", value)
# Here ----------------------------------------^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文