This commit is contained in:
iridiumR 2023-05-21 23:00:12 +08:00
commit 9383faa009
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
7 changed files with 144 additions and 0 deletions

25
scripts/create_table.sql Normal file
View file

@ -0,0 +1,25 @@
-- 创建账户表
CREATE TABLE account (
a_id SERIAL PRIMARY KEY,
balance DECIMAL(10, 2),
meta JSONB
);
-- 创建类别表
CREATE TABLE category (
c_id SERIAL PRIMARY KEY,
type VARCHAR(10),
meta JSONB
);
-- 创建流水表
CREATE TABLE transaction (
t_id SERIAL PRIMARY KEY,
amount DECIMAL(10, 2),
date DATE,
c_id INTEGER REFERENCES category(c_id),
a_id INTEGER REFERENCES account(a_id),
meta JSONB
);

3
src/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.venv/*
.vscode/*

14
src/budget/__init__.py Normal file
View file

@ -0,0 +1,14 @@
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
class MainWindow(QMainWindow):
cur = None
conn = None
def __init__(self):
super().__init__()
self.initUI()
from ._mw import initUI
from ._pg import connectToDatabase, disconnectFromDatabase, onConnectClicked

2
src/budget/_hook.py Normal file
View file

@ -0,0 +1,2 @@
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *

56
src/budget/_mw.py Normal file
View file

@ -0,0 +1,56 @@
from PyQt6.QtWidgets import *
def initUI(self):
self.setWindowTitle('budget')
self.setGeometry(100, 100, 800, 600)
# 创建连接信息控件
self.hostLabel = QLabel('Host:')
self.hostLineEdit = QLineEdit()
self.hostLineEdit.setText('localhost')
self.portLabel = QLabel('Port:')
self.portLineEdit = QLineEdit()
self.portLineEdit.setText('5432')
self.dbNameLabel = QLabel('Database:')
self.dbNameLineEdit = QLineEdit()
self.dbNameLineEdit.setText('budget')
self.userLabel = QLabel('Username:')
self.userLineEdit = QLineEdit()
self.userLineEdit.setText('postgres')
self.passwordLabel = QLabel('Password:')
self.passwordLineEdit = QLineEdit()
self.passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password)
self.passwordLineEdit.setText('')
self.connectButton = QPushButton('Connect')
self.connectButton.clicked.connect(self.onConnectClicked)
# 创建连接信息控件布局
connectLayout = QHBoxLayout()
connectLayout.addWidget(self.hostLabel)
connectLayout.addWidget(self.hostLineEdit)
connectLayout.addWidget(self.portLabel)
connectLayout.addWidget(self.portLineEdit)
connectLayout.addWidget(self.dbNameLabel)
connectLayout.addWidget(self.dbNameLineEdit)
connectLayout.addWidget(self.userLabel)
connectLayout.addWidget(self.userLineEdit)
connectLayout.addWidget(self.passwordLabel)
connectLayout.addWidget(self.passwordLineEdit)
connectLayout.addWidget(self.connectButton)
connectWidget = QWidget()
connectWidget.setLayout(connectLayout)
# 创建主窗口布局
mainLayout = QVBoxLayout()
mainLayout.addWidget(connectWidget)
mainWidget = QWidget()
mainWidget.setLayout(mainLayout)
self.setCentralWidget(mainWidget)

37
src/budget/_pg.py Normal file
View file

@ -0,0 +1,37 @@
from PyQt6.QtWidgets import *
import psycopg2
def connectToDatabase(self):
try:
self.conn = psycopg2.connect(database=self.dbName, user=self.user, password=self.password, host=self.host, port=self.port)
self.cur = self.conn.cursor()
except Exception as e:
print(e)
self.cur = None
QMessageBox.critical(self, 'Error', str(e))
def disconnectFromDatabase(self):
try:
if (self.cur is None):
raise Exception('Not connected to database')
self.cur.close()
self.conn.close()
self.cur = None
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))
def onConnectClicked(self):
if self.connectButton.text() == 'Connect':
self.host = self.hostLineEdit.text()
self.port = self.portLineEdit.text()
self.dbName = self.dbNameLineEdit.text()
self.user = self.userLineEdit.text()
self.password = self.passwordLineEdit.text()
# 连接到 PostgreSQL 数据库
self.connectToDatabase()
self.connectButton.setText('Disconnect')
else:
self.disconnectFromDatabase()
self.connectButton.setText('Connect')

7
src/main.py Normal file
View file

@ -0,0 +1,7 @@
from budget import *
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())