commit 9383faa009b4e2b61a86c9bdbb6514aec9637d06 Author: iridiumR Date: Sun May 21 23:00:12 2023 +0800 init diff --git a/scripts/create_table.sql b/scripts/create_table.sql new file mode 100644 index 0000000..7bede83 --- /dev/null +++ b/scripts/create_table.sql @@ -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 +); + + diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..91ea182 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,3 @@ +.venv/* +.vscode/* + diff --git a/src/budget/__init__.py b/src/budget/__init__.py new file mode 100644 index 0000000..4a6f3ac --- /dev/null +++ b/src/budget/__init__.py @@ -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 \ No newline at end of file diff --git a/src/budget/_hook.py b/src/budget/_hook.py new file mode 100644 index 0000000..60e2bc6 --- /dev/null +++ b/src/budget/_hook.py @@ -0,0 +1,2 @@ +from PyQt6.QtWidgets import * +from PyQt6.QtCore import * diff --git a/src/budget/_mw.py b/src/budget/_mw.py new file mode 100644 index 0000000..8860f05 --- /dev/null +++ b/src/budget/_mw.py @@ -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) diff --git a/src/budget/_pg.py b/src/budget/_pg.py new file mode 100644 index 0000000..2c560dd --- /dev/null +++ b/src/budget/_pg.py @@ -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') diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..e96bc01 --- /dev/null +++ b/src/main.py @@ -0,0 +1,7 @@ +from budget import * +import sys +if __name__ == '__main__': + app = QApplication(sys.argv) + mw = MainWindow() + mw.show() + sys.exit(app.exec())