SQLPyHelper 0.1.2__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlpyhelper-0.1.4/PKG-INFO +238 -0
- sqlpyhelper-0.1.4/README.md +184 -0
- sqlpyhelper-0.1.4/SQLPyHelper.egg-info/PKG-INFO +238 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.4}/SQLPyHelper.egg-info/SOURCES.txt +5 -0
- sqlpyhelper-0.1.4/SQLPyHelper.egg-info/entry_points.txt +2 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.4}/SQLPyHelper.egg-info/requires.txt +4 -3
- sqlpyhelper-0.1.4/setup.py +63 -0
- sqlpyhelper-0.1.4/sqlpyhelper/__init__.py +9 -0
- sqlpyhelper-0.1.4/sqlpyhelper/automation_utils.py +145 -0
- sqlpyhelper-0.1.4/sqlpyhelper/cli.py +146 -0
- sqlpyhelper-0.1.4/sqlpyhelper/db_helper.py +284 -0
- sqlpyhelper-0.1.4/sqlpyhelper/py.typed +0 -0
- sqlpyhelper-0.1.4/test/test_automation.py +15 -0
- sqlpyhelper-0.1.4/test/test_sqlpyhelper.py +86 -0
- sqlpyhelper-0.1.2/PKG-INFO +0 -211
- sqlpyhelper-0.1.2/README.md +0 -171
- sqlpyhelper-0.1.2/SQLPyHelper.egg-info/PKG-INFO +0 -211
- sqlpyhelper-0.1.2/setup.py +0 -38
- sqlpyhelper-0.1.2/sqlpyhelper/__init__.py +0 -2
- sqlpyhelper-0.1.2/sqlpyhelper/db_helper.py +0 -135
- sqlpyhelper-0.1.2/test/test_sqlpyhelper.py +0 -71
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.4}/LICENSE +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.4}/SQLPyHelper.egg-info/dependency_links.txt +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.4}/SQLPyHelper.egg-info/top_level.txt +0 -0
- {sqlpyhelper-0.1.2 → sqlpyhelper-0.1.4}/setup.cfg +0 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: SQLPyHelper
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: A simple SQL database helper package for Python.
|
|
5
|
+
Home-page: https://github.com/adebayopeter/sqlpyhelper
|
|
6
|
+
Author: Adebayo Olaonipekun
|
|
7
|
+
Author-email: pekunmi@live.com
|
|
8
|
+
Project-URL: Source, https://github.com/adebayopeter/sqlpyhelper
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/adebayopeter/sqlpyhelper/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/adebayopeter/sqlpyhelper/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: database,sql,sqlite,postgresql,mysql,sqlserver,oracle,db,query,helper
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Development Status :: 4 - Beta
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
23
|
+
Requires-Python: >=3.8
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: python-dotenv
|
|
27
|
+
Requires-Dist: click
|
|
28
|
+
Provides-Extra: postgres
|
|
29
|
+
Requires-Dist: psycopg2; extra == "postgres"
|
|
30
|
+
Provides-Extra: mysql
|
|
31
|
+
Requires-Dist: mysql-connector-python; extra == "mysql"
|
|
32
|
+
Provides-Extra: sqlserver
|
|
33
|
+
Requires-Dist: pyodbc; extra == "sqlserver"
|
|
34
|
+
Provides-Extra: oracle
|
|
35
|
+
Requires-Dist: cx_Oracle; extra == "oracle"
|
|
36
|
+
Provides-Extra: all
|
|
37
|
+
Requires-Dist: psycopg2; extra == "all"
|
|
38
|
+
Requires-Dist: mysql-connector-python; extra == "all"
|
|
39
|
+
Requires-Dist: pyodbc; extra == "all"
|
|
40
|
+
Requires-Dist: cx_Oracle; extra == "all"
|
|
41
|
+
Dynamic: author
|
|
42
|
+
Dynamic: author-email
|
|
43
|
+
Dynamic: classifier
|
|
44
|
+
Dynamic: description
|
|
45
|
+
Dynamic: description-content-type
|
|
46
|
+
Dynamic: home-page
|
|
47
|
+
Dynamic: keywords
|
|
48
|
+
Dynamic: license-file
|
|
49
|
+
Dynamic: project-url
|
|
50
|
+
Dynamic: provides-extra
|
|
51
|
+
Dynamic: requires-dist
|
|
52
|
+
Dynamic: requires-python
|
|
53
|
+
Dynamic: summary
|
|
54
|
+
|
|
55
|
+
# SQLPyHelper
|
|
56
|
+
|
|
57
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
58
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
59
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
60
|
+
[](https://github.com/adebayopeter/sqlpyhelper/blob/main/LICENSE)
|
|
61
|
+
[](https://github.com/adebayopeter/sqlpyhelper)
|
|
62
|
+
|
|
63
|
+
# 📌 SQLPyHelper v.0.1.4 🚀
|
|
64
|
+
|
|
65
|
+
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.
|
|
66
|
+
|
|
67
|
+
## 📖 Table of Contents
|
|
68
|
+
- [🚀 Features](#-features)
|
|
69
|
+
- [📦 Installation](#-installation)
|
|
70
|
+
- [⚙️ Setup Using `.env`](#️-setup-using-env)
|
|
71
|
+
- [🛠 Usage Examples](#-usage-examples)
|
|
72
|
+
- [SQLite Example](#sqlite-example)
|
|
73
|
+
- [PostgreSQL Example](#postgresql-example)
|
|
74
|
+
- [MySQL Example](#mysql-example)
|
|
75
|
+
- [SQL Server Example](#sql-server-example)
|
|
76
|
+
- [Oracle Example](#oracle-example)
|
|
77
|
+
- [📂 Project Structure](#-project-structure)
|
|
78
|
+
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
79
|
+
- [🌍 Contributing](#-contributing)
|
|
80
|
+
- [☕ Support the Project](#-support-the-project)
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 🚀 Features in v0.1.3
|
|
85
|
+
- Unified connection pooling for multiple databases.
|
|
86
|
+
- Automatic reconnection for lost connections.
|
|
87
|
+
- Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
88
|
+
- Secure parameterized queries to prevent SQL injection.
|
|
89
|
+
- Bulk insertion & dynamic table creation.
|
|
90
|
+
- Logging & error handling for better debugging.
|
|
91
|
+
- CSV export & database backups.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
## 📦 Installation
|
|
95
|
+
#### Install via PyPI:
|
|
96
|
+
```sh
|
|
97
|
+
pip install sqlpyhelper
|
|
98
|
+
```
|
|
99
|
+
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
100
|
+
|
|
101
|
+
For local development:
|
|
102
|
+
```sh
|
|
103
|
+
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
104
|
+
cd sqlpyhelper
|
|
105
|
+
pip install -r requirements.txt
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## ⚙️ Setup Using `.env`
|
|
111
|
+
Create a `.env` file in your project root to manage database configurations securely by renaming `.env_example`.
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
# .env_example (Rename to .env)
|
|
115
|
+
DB_TYPE=postgres
|
|
116
|
+
DB_HOST=localhost
|
|
117
|
+
DB_USER=your_user
|
|
118
|
+
DB_PASSWORD=your_secure_password
|
|
119
|
+
DB_NAME=database_name
|
|
120
|
+
DB_DRIVER={ODBC Driver 17 for SQL Server}
|
|
121
|
+
ORACLE_SID=XE
|
|
122
|
+
ORACLE_DB_PORT=1521
|
|
123
|
+
```
|
|
124
|
+
### Loading `.env` in Code
|
|
125
|
+
```pycon
|
|
126
|
+
from dotenv import load_dotenv
|
|
127
|
+
import os
|
|
128
|
+
|
|
129
|
+
load_dotenv()
|
|
130
|
+
db_type = os.getenv("DB_TYPE")
|
|
131
|
+
host = os.getenv("DB_HOST")
|
|
132
|
+
user = os.getenv("DB_USER")
|
|
133
|
+
password = os.getenv("DB_PASSWORD")
|
|
134
|
+
database = os.getenv("DB_NAME")
|
|
135
|
+
```
|
|
136
|
+
---
|
|
137
|
+
## 🛠 Usage Examples
|
|
138
|
+
### Initialize SQLPyHelper
|
|
139
|
+
```pycon
|
|
140
|
+
from sqlpyhelper.db_helper import SQLPyHelper
|
|
141
|
+
db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
|
|
142
|
+
```
|
|
143
|
+
### SQLite Example
|
|
144
|
+
```pycon
|
|
145
|
+
db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
146
|
+
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
147
|
+
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
|
|
148
|
+
db.close()
|
|
149
|
+
```
|
|
150
|
+
### PostgreSQL Example
|
|
151
|
+
```pycon
|
|
152
|
+
db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
|
|
153
|
+
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
|
|
154
|
+
db.begin_transaction()
|
|
155
|
+
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
|
|
156
|
+
db.rollback_transaction() # Undo delete
|
|
157
|
+
```
|
|
158
|
+
### MySQL Example
|
|
159
|
+
```pycon
|
|
160
|
+
db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
|
|
161
|
+
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
|
|
162
|
+
print(db.fetch_by_param("users", "id", 1)) # Expected Output: [(1, 'Alice')]
|
|
163
|
+
db.close()
|
|
164
|
+
```
|
|
165
|
+
### SQL Server Example
|
|
166
|
+
```pycon
|
|
167
|
+
db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
|
|
168
|
+
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
|
|
169
|
+
db.backup_table("orders", "orders_backup.csv") # Export data to CSV
|
|
170
|
+
```
|
|
171
|
+
### Oracle Example
|
|
172
|
+
```pycon
|
|
173
|
+
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
174
|
+
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
|
|
175
|
+
db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better performance
|
|
176
|
+
conn = db.get_connection_from_pool()
|
|
177
|
+
db.return_connection_to_pool(conn)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 📂 Project Structure
|
|
181
|
+
```
|
|
182
|
+
📦 SQLPyHelper/
|
|
183
|
+
├─ sqlpyhelper/
|
|
184
|
+
│ ├─ __init__.py
|
|
185
|
+
│ └─ db_helper.py
|
|
186
|
+
├─ tests/
|
|
187
|
+
│ └─ test_sqlpyhelper.py
|
|
188
|
+
├─ .env_example
|
|
189
|
+
├─ .gitignore
|
|
190
|
+
├─ setup.py
|
|
191
|
+
├─ README.md
|
|
192
|
+
└─ requirements.txt
|
|
193
|
+
```
|
|
194
|
+
---
|
|
195
|
+
## 📌 Available Methods in SQLPyHelper
|
|
196
|
+
|
|
197
|
+
| Method | Description |
|
|
198
|
+
|--------|-------------|
|
|
199
|
+
| `execute_query(query, params=None)` | Executes a SQL query with optional parameters. |
|
|
200
|
+
| `fetch_one()` | Retrieves a **single row** from query results. |
|
|
201
|
+
| `fetch_all()` | Retrieves **all rows** from query results. |
|
|
202
|
+
| `fetch_by_param(table, column, value)` | Fetches **rows dynamically** based on a given parameter. |
|
|
203
|
+
| `create_table(table_name, columns_dict)` | Creates a table dynamically with a dictionary format. |
|
|
204
|
+
| `insert_bulk(table, data_list)` | Inserts **multiple rows at once** efficiently. |
|
|
205
|
+
| `backup_table(table, backup_file.csv)` | Exports table data to **CSV format**. |
|
|
206
|
+
| `setup_connection_pool()` | Initializes **database connection pooling**. |
|
|
207
|
+
| `get_connection_from_pool()` | Fetches a connection from the pool. |
|
|
208
|
+
| `return_connection_to_pool(conn)` | Returns connection back to pool. |
|
|
209
|
+
| `begin_transaction()` | Begins an **explicit transaction**. |
|
|
210
|
+
| `rollback_transaction()` | Rolls back **uncommitted transactions**. |
|
|
211
|
+
| `close()` | Closes the database connection safely. |
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
## 🌍 Contributing
|
|
215
|
+
We welcome contributions from the **open-source community**! Follow these steps to contribute:
|
|
216
|
+
|
|
217
|
+
1. Fork the repo: [SQLPyHelper GitHub Repository](https://github.com/adebayopeter/sqlpyhelper)
|
|
218
|
+
2. Clone your fork:
|
|
219
|
+
```sh
|
|
220
|
+
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
221
|
+
```
|
|
222
|
+
3. Create a new branch:
|
|
223
|
+
```sh
|
|
224
|
+
git checkout -b feature-new-functionality
|
|
225
|
+
```
|
|
226
|
+
4. Make changes, commit, and push:
|
|
227
|
+
```sh
|
|
228
|
+
git commit -m "Added new feature"
|
|
229
|
+
git push origin feature-new-functionality
|
|
230
|
+
```
|
|
231
|
+
5. Submit a Pull Request!
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
## ☕ Support the Project
|
|
235
|
+
|
|
236
|
+
If you find SQLPyHelper useful, consider buying me a coffee to support continued development!
|
|
237
|
+
Donate Here: [PayPal](https://paypal.me/adebayopeter?country.x=GB&locale.x=en_GB)
|
|
238
|
+
---
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# SQLPyHelper
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
4
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
5
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
6
|
+
[](https://github.com/adebayopeter/sqlpyhelper/blob/main/LICENSE)
|
|
7
|
+
[](https://github.com/adebayopeter/sqlpyhelper)
|
|
8
|
+
|
|
9
|
+
# 📌 SQLPyHelper v.0.1.4 🚀
|
|
10
|
+
|
|
11
|
+
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.
|
|
12
|
+
|
|
13
|
+
## 📖 Table of Contents
|
|
14
|
+
- [🚀 Features](#-features)
|
|
15
|
+
- [📦 Installation](#-installation)
|
|
16
|
+
- [⚙️ Setup Using `.env`](#️-setup-using-env)
|
|
17
|
+
- [🛠 Usage Examples](#-usage-examples)
|
|
18
|
+
- [SQLite Example](#sqlite-example)
|
|
19
|
+
- [PostgreSQL Example](#postgresql-example)
|
|
20
|
+
- [MySQL Example](#mysql-example)
|
|
21
|
+
- [SQL Server Example](#sql-server-example)
|
|
22
|
+
- [Oracle Example](#oracle-example)
|
|
23
|
+
- [📂 Project Structure](#-project-structure)
|
|
24
|
+
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
25
|
+
- [🌍 Contributing](#-contributing)
|
|
26
|
+
- [☕ Support the Project](#-support-the-project)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 🚀 Features in v0.1.3
|
|
31
|
+
- Unified connection pooling for multiple databases.
|
|
32
|
+
- Automatic reconnection for lost connections.
|
|
33
|
+
- Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
34
|
+
- Secure parameterized queries to prevent SQL injection.
|
|
35
|
+
- Bulk insertion & dynamic table creation.
|
|
36
|
+
- Logging & error handling for better debugging.
|
|
37
|
+
- CSV export & database backups.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
## 📦 Installation
|
|
41
|
+
#### Install via PyPI:
|
|
42
|
+
```sh
|
|
43
|
+
pip install sqlpyhelper
|
|
44
|
+
```
|
|
45
|
+
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
46
|
+
|
|
47
|
+
For local development:
|
|
48
|
+
```sh
|
|
49
|
+
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
50
|
+
cd sqlpyhelper
|
|
51
|
+
pip install -r requirements.txt
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## ⚙️ Setup Using `.env`
|
|
57
|
+
Create a `.env` file in your project root to manage database configurations securely by renaming `.env_example`.
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
# .env_example (Rename to .env)
|
|
61
|
+
DB_TYPE=postgres
|
|
62
|
+
DB_HOST=localhost
|
|
63
|
+
DB_USER=your_user
|
|
64
|
+
DB_PASSWORD=your_secure_password
|
|
65
|
+
DB_NAME=database_name
|
|
66
|
+
DB_DRIVER={ODBC Driver 17 for SQL Server}
|
|
67
|
+
ORACLE_SID=XE
|
|
68
|
+
ORACLE_DB_PORT=1521
|
|
69
|
+
```
|
|
70
|
+
### Loading `.env` in Code
|
|
71
|
+
```pycon
|
|
72
|
+
from dotenv import load_dotenv
|
|
73
|
+
import os
|
|
74
|
+
|
|
75
|
+
load_dotenv()
|
|
76
|
+
db_type = os.getenv("DB_TYPE")
|
|
77
|
+
host = os.getenv("DB_HOST")
|
|
78
|
+
user = os.getenv("DB_USER")
|
|
79
|
+
password = os.getenv("DB_PASSWORD")
|
|
80
|
+
database = os.getenv("DB_NAME")
|
|
81
|
+
```
|
|
82
|
+
---
|
|
83
|
+
## 🛠 Usage Examples
|
|
84
|
+
### Initialize SQLPyHelper
|
|
85
|
+
```pycon
|
|
86
|
+
from sqlpyhelper.db_helper import SQLPyHelper
|
|
87
|
+
db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
|
|
88
|
+
```
|
|
89
|
+
### SQLite Example
|
|
90
|
+
```pycon
|
|
91
|
+
db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
92
|
+
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
93
|
+
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
|
|
94
|
+
db.close()
|
|
95
|
+
```
|
|
96
|
+
### PostgreSQL Example
|
|
97
|
+
```pycon
|
|
98
|
+
db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
|
|
99
|
+
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
|
|
100
|
+
db.begin_transaction()
|
|
101
|
+
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
|
|
102
|
+
db.rollback_transaction() # Undo delete
|
|
103
|
+
```
|
|
104
|
+
### MySQL Example
|
|
105
|
+
```pycon
|
|
106
|
+
db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
|
|
107
|
+
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
|
|
108
|
+
print(db.fetch_by_param("users", "id", 1)) # Expected Output: [(1, 'Alice')]
|
|
109
|
+
db.close()
|
|
110
|
+
```
|
|
111
|
+
### SQL Server Example
|
|
112
|
+
```pycon
|
|
113
|
+
db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
|
|
114
|
+
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
|
|
115
|
+
db.backup_table("orders", "orders_backup.csv") # Export data to CSV
|
|
116
|
+
```
|
|
117
|
+
### Oracle Example
|
|
118
|
+
```pycon
|
|
119
|
+
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
120
|
+
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
|
|
121
|
+
db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better performance
|
|
122
|
+
conn = db.get_connection_from_pool()
|
|
123
|
+
db.return_connection_to_pool(conn)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## 📂 Project Structure
|
|
127
|
+
```
|
|
128
|
+
📦 SQLPyHelper/
|
|
129
|
+
├─ sqlpyhelper/
|
|
130
|
+
│ ├─ __init__.py
|
|
131
|
+
│ └─ db_helper.py
|
|
132
|
+
├─ tests/
|
|
133
|
+
│ └─ test_sqlpyhelper.py
|
|
134
|
+
├─ .env_example
|
|
135
|
+
├─ .gitignore
|
|
136
|
+
├─ setup.py
|
|
137
|
+
├─ README.md
|
|
138
|
+
└─ requirements.txt
|
|
139
|
+
```
|
|
140
|
+
---
|
|
141
|
+
## 📌 Available Methods in SQLPyHelper
|
|
142
|
+
|
|
143
|
+
| Method | Description |
|
|
144
|
+
|--------|-------------|
|
|
145
|
+
| `execute_query(query, params=None)` | Executes a SQL query with optional parameters. |
|
|
146
|
+
| `fetch_one()` | Retrieves a **single row** from query results. |
|
|
147
|
+
| `fetch_all()` | Retrieves **all rows** from query results. |
|
|
148
|
+
| `fetch_by_param(table, column, value)` | Fetches **rows dynamically** based on a given parameter. |
|
|
149
|
+
| `create_table(table_name, columns_dict)` | Creates a table dynamically with a dictionary format. |
|
|
150
|
+
| `insert_bulk(table, data_list)` | Inserts **multiple rows at once** efficiently. |
|
|
151
|
+
| `backup_table(table, backup_file.csv)` | Exports table data to **CSV format**. |
|
|
152
|
+
| `setup_connection_pool()` | Initializes **database connection pooling**. |
|
|
153
|
+
| `get_connection_from_pool()` | Fetches a connection from the pool. |
|
|
154
|
+
| `return_connection_to_pool(conn)` | Returns connection back to pool. |
|
|
155
|
+
| `begin_transaction()` | Begins an **explicit transaction**. |
|
|
156
|
+
| `rollback_transaction()` | Rolls back **uncommitted transactions**. |
|
|
157
|
+
| `close()` | Closes the database connection safely. |
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
## 🌍 Contributing
|
|
161
|
+
We welcome contributions from the **open-source community**! Follow these steps to contribute:
|
|
162
|
+
|
|
163
|
+
1. Fork the repo: [SQLPyHelper GitHub Repository](https://github.com/adebayopeter/sqlpyhelper)
|
|
164
|
+
2. Clone your fork:
|
|
165
|
+
```sh
|
|
166
|
+
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
167
|
+
```
|
|
168
|
+
3. Create a new branch:
|
|
169
|
+
```sh
|
|
170
|
+
git checkout -b feature-new-functionality
|
|
171
|
+
```
|
|
172
|
+
4. Make changes, commit, and push:
|
|
173
|
+
```sh
|
|
174
|
+
git commit -m "Added new feature"
|
|
175
|
+
git push origin feature-new-functionality
|
|
176
|
+
```
|
|
177
|
+
5. Submit a Pull Request!
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
## ☕ Support the Project
|
|
181
|
+
|
|
182
|
+
If you find SQLPyHelper useful, consider buying me a coffee to support continued development!
|
|
183
|
+
Donate Here: [PayPal](https://paypal.me/adebayopeter?country.x=GB&locale.x=en_GB)
|
|
184
|
+
---
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: SQLPyHelper
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: A simple SQL database helper package for Python.
|
|
5
|
+
Home-page: https://github.com/adebayopeter/sqlpyhelper
|
|
6
|
+
Author: Adebayo Olaonipekun
|
|
7
|
+
Author-email: pekunmi@live.com
|
|
8
|
+
Project-URL: Source, https://github.com/adebayopeter/sqlpyhelper
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/adebayopeter/sqlpyhelper/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/adebayopeter/sqlpyhelper/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: database,sql,sqlite,postgresql,mysql,sqlserver,oracle,db,query,helper
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Development Status :: 4 - Beta
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
23
|
+
Requires-Python: >=3.8
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: python-dotenv
|
|
27
|
+
Requires-Dist: click
|
|
28
|
+
Provides-Extra: postgres
|
|
29
|
+
Requires-Dist: psycopg2; extra == "postgres"
|
|
30
|
+
Provides-Extra: mysql
|
|
31
|
+
Requires-Dist: mysql-connector-python; extra == "mysql"
|
|
32
|
+
Provides-Extra: sqlserver
|
|
33
|
+
Requires-Dist: pyodbc; extra == "sqlserver"
|
|
34
|
+
Provides-Extra: oracle
|
|
35
|
+
Requires-Dist: cx_Oracle; extra == "oracle"
|
|
36
|
+
Provides-Extra: all
|
|
37
|
+
Requires-Dist: psycopg2; extra == "all"
|
|
38
|
+
Requires-Dist: mysql-connector-python; extra == "all"
|
|
39
|
+
Requires-Dist: pyodbc; extra == "all"
|
|
40
|
+
Requires-Dist: cx_Oracle; extra == "all"
|
|
41
|
+
Dynamic: author
|
|
42
|
+
Dynamic: author-email
|
|
43
|
+
Dynamic: classifier
|
|
44
|
+
Dynamic: description
|
|
45
|
+
Dynamic: description-content-type
|
|
46
|
+
Dynamic: home-page
|
|
47
|
+
Dynamic: keywords
|
|
48
|
+
Dynamic: license-file
|
|
49
|
+
Dynamic: project-url
|
|
50
|
+
Dynamic: provides-extra
|
|
51
|
+
Dynamic: requires-dist
|
|
52
|
+
Dynamic: requires-python
|
|
53
|
+
Dynamic: summary
|
|
54
|
+
|
|
55
|
+
# SQLPyHelper
|
|
56
|
+
|
|
57
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
58
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
59
|
+
[](https://pypi.org/project/sqlpyhelper/)
|
|
60
|
+
[](https://github.com/adebayopeter/sqlpyhelper/blob/main/LICENSE)
|
|
61
|
+
[](https://github.com/adebayopeter/sqlpyhelper)
|
|
62
|
+
|
|
63
|
+
# 📌 SQLPyHelper v.0.1.4 🚀
|
|
64
|
+
|
|
65
|
+
A Python library for simplified database interactions across **SQLite, PostgreSQL, MySQL, SQL Server, and Oracle**. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.
|
|
66
|
+
|
|
67
|
+
## 📖 Table of Contents
|
|
68
|
+
- [🚀 Features](#-features)
|
|
69
|
+
- [📦 Installation](#-installation)
|
|
70
|
+
- [⚙️ Setup Using `.env`](#️-setup-using-env)
|
|
71
|
+
- [🛠 Usage Examples](#-usage-examples)
|
|
72
|
+
- [SQLite Example](#sqlite-example)
|
|
73
|
+
- [PostgreSQL Example](#postgresql-example)
|
|
74
|
+
- [MySQL Example](#mysql-example)
|
|
75
|
+
- [SQL Server Example](#sql-server-example)
|
|
76
|
+
- [Oracle Example](#oracle-example)
|
|
77
|
+
- [📂 Project Structure](#-project-structure)
|
|
78
|
+
- [📌 Available Methods in SQLPyHelper](#-available-methods-in-sqlpyhelper)
|
|
79
|
+
- [🌍 Contributing](#-contributing)
|
|
80
|
+
- [☕ Support the Project](#-support-the-project)
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 🚀 Features in v0.1.3
|
|
85
|
+
- Unified connection pooling for multiple databases.
|
|
86
|
+
- Automatic reconnection for lost connections.
|
|
87
|
+
- Transaction support (BEGIN, ROLLBACK, COMMIT).
|
|
88
|
+
- Secure parameterized queries to prevent SQL injection.
|
|
89
|
+
- Bulk insertion & dynamic table creation.
|
|
90
|
+
- Logging & error handling for better debugging.
|
|
91
|
+
- CSV export & database backups.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
## 📦 Installation
|
|
95
|
+
#### Install via PyPI:
|
|
96
|
+
```sh
|
|
97
|
+
pip install sqlpyhelper
|
|
98
|
+
```
|
|
99
|
+
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
100
|
+
|
|
101
|
+
For local development:
|
|
102
|
+
```sh
|
|
103
|
+
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
104
|
+
cd sqlpyhelper
|
|
105
|
+
pip install -r requirements.txt
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## ⚙️ Setup Using `.env`
|
|
111
|
+
Create a `.env` file in your project root to manage database configurations securely by renaming `.env_example`.
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
# .env_example (Rename to .env)
|
|
115
|
+
DB_TYPE=postgres
|
|
116
|
+
DB_HOST=localhost
|
|
117
|
+
DB_USER=your_user
|
|
118
|
+
DB_PASSWORD=your_secure_password
|
|
119
|
+
DB_NAME=database_name
|
|
120
|
+
DB_DRIVER={ODBC Driver 17 for SQL Server}
|
|
121
|
+
ORACLE_SID=XE
|
|
122
|
+
ORACLE_DB_PORT=1521
|
|
123
|
+
```
|
|
124
|
+
### Loading `.env` in Code
|
|
125
|
+
```pycon
|
|
126
|
+
from dotenv import load_dotenv
|
|
127
|
+
import os
|
|
128
|
+
|
|
129
|
+
load_dotenv()
|
|
130
|
+
db_type = os.getenv("DB_TYPE")
|
|
131
|
+
host = os.getenv("DB_HOST")
|
|
132
|
+
user = os.getenv("DB_USER")
|
|
133
|
+
password = os.getenv("DB_PASSWORD")
|
|
134
|
+
database = os.getenv("DB_NAME")
|
|
135
|
+
```
|
|
136
|
+
---
|
|
137
|
+
## 🛠 Usage Examples
|
|
138
|
+
### Initialize SQLPyHelper
|
|
139
|
+
```pycon
|
|
140
|
+
from sqlpyhelper.db_helper import SQLPyHelper
|
|
141
|
+
db = SQLPyHelper() # Auto-detects database type based on `DB_TYPE`
|
|
142
|
+
```
|
|
143
|
+
### SQLite Example
|
|
144
|
+
```pycon
|
|
145
|
+
db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
|
|
146
|
+
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
|
|
147
|
+
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
|
|
148
|
+
db.close()
|
|
149
|
+
```
|
|
150
|
+
### PostgreSQL Example
|
|
151
|
+
```pycon
|
|
152
|
+
db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
|
|
153
|
+
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
|
|
154
|
+
db.begin_transaction()
|
|
155
|
+
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
|
|
156
|
+
db.rollback_transaction() # Undo delete
|
|
157
|
+
```
|
|
158
|
+
### MySQL Example
|
|
159
|
+
```pycon
|
|
160
|
+
db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
|
|
161
|
+
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
|
|
162
|
+
print(db.fetch_by_param("users", "id", 1)) # Expected Output: [(1, 'Alice')]
|
|
163
|
+
db.close()
|
|
164
|
+
```
|
|
165
|
+
### SQL Server Example
|
|
166
|
+
```pycon
|
|
167
|
+
db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
|
|
168
|
+
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
|
|
169
|
+
db.backup_table("orders", "orders_backup.csv") # Export data to CSV
|
|
170
|
+
```
|
|
171
|
+
### Oracle Example
|
|
172
|
+
```pycon
|
|
173
|
+
db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
|
|
174
|
+
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
|
|
175
|
+
db.setup_connection_pool(min_conn=2, max_conn=10) # Enable pooling for better performance
|
|
176
|
+
conn = db.get_connection_from_pool()
|
|
177
|
+
db.return_connection_to_pool(conn)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 📂 Project Structure
|
|
181
|
+
```
|
|
182
|
+
📦 SQLPyHelper/
|
|
183
|
+
├─ sqlpyhelper/
|
|
184
|
+
│ ├─ __init__.py
|
|
185
|
+
│ └─ db_helper.py
|
|
186
|
+
├─ tests/
|
|
187
|
+
│ └─ test_sqlpyhelper.py
|
|
188
|
+
├─ .env_example
|
|
189
|
+
├─ .gitignore
|
|
190
|
+
├─ setup.py
|
|
191
|
+
├─ README.md
|
|
192
|
+
└─ requirements.txt
|
|
193
|
+
```
|
|
194
|
+
---
|
|
195
|
+
## 📌 Available Methods in SQLPyHelper
|
|
196
|
+
|
|
197
|
+
| Method | Description |
|
|
198
|
+
|--------|-------------|
|
|
199
|
+
| `execute_query(query, params=None)` | Executes a SQL query with optional parameters. |
|
|
200
|
+
| `fetch_one()` | Retrieves a **single row** from query results. |
|
|
201
|
+
| `fetch_all()` | Retrieves **all rows** from query results. |
|
|
202
|
+
| `fetch_by_param(table, column, value)` | Fetches **rows dynamically** based on a given parameter. |
|
|
203
|
+
| `create_table(table_name, columns_dict)` | Creates a table dynamically with a dictionary format. |
|
|
204
|
+
| `insert_bulk(table, data_list)` | Inserts **multiple rows at once** efficiently. |
|
|
205
|
+
| `backup_table(table, backup_file.csv)` | Exports table data to **CSV format**. |
|
|
206
|
+
| `setup_connection_pool()` | Initializes **database connection pooling**. |
|
|
207
|
+
| `get_connection_from_pool()` | Fetches a connection from the pool. |
|
|
208
|
+
| `return_connection_to_pool(conn)` | Returns connection back to pool. |
|
|
209
|
+
| `begin_transaction()` | Begins an **explicit transaction**. |
|
|
210
|
+
| `rollback_transaction()` | Rolls back **uncommitted transactions**. |
|
|
211
|
+
| `close()` | Closes the database connection safely. |
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
## 🌍 Contributing
|
|
215
|
+
We welcome contributions from the **open-source community**! Follow these steps to contribute:
|
|
216
|
+
|
|
217
|
+
1. Fork the repo: [SQLPyHelper GitHub Repository](https://github.com/adebayopeter/sqlpyhelper)
|
|
218
|
+
2. Clone your fork:
|
|
219
|
+
```sh
|
|
220
|
+
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
221
|
+
```
|
|
222
|
+
3. Create a new branch:
|
|
223
|
+
```sh
|
|
224
|
+
git checkout -b feature-new-functionality
|
|
225
|
+
```
|
|
226
|
+
4. Make changes, commit, and push:
|
|
227
|
+
```sh
|
|
228
|
+
git commit -m "Added new feature"
|
|
229
|
+
git push origin feature-new-functionality
|
|
230
|
+
```
|
|
231
|
+
5. Submit a Pull Request!
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
## ☕ Support the Project
|
|
235
|
+
|
|
236
|
+
If you find SQLPyHelper useful, consider buying me a coffee to support continued development!
|
|
237
|
+
Donate Here: [PayPal](https://paypal.me/adebayopeter?country.x=GB&locale.x=en_GB)
|
|
238
|
+
---
|
|
@@ -4,8 +4,13 @@ setup.py
|
|
|
4
4
|
SQLPyHelper.egg-info/PKG-INFO
|
|
5
5
|
SQLPyHelper.egg-info/SOURCES.txt
|
|
6
6
|
SQLPyHelper.egg-info/dependency_links.txt
|
|
7
|
+
SQLPyHelper.egg-info/entry_points.txt
|
|
7
8
|
SQLPyHelper.egg-info/requires.txt
|
|
8
9
|
SQLPyHelper.egg-info/top_level.txt
|
|
9
10
|
sqlpyhelper/__init__.py
|
|
11
|
+
sqlpyhelper/automation_utils.py
|
|
12
|
+
sqlpyhelper/cli.py
|
|
10
13
|
sqlpyhelper/db_helper.py
|
|
14
|
+
sqlpyhelper/py.typed
|
|
15
|
+
test/test_automation.py
|
|
11
16
|
test/test_sqlpyhelper.py
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
python-dotenv
|
|
2
|
+
click
|
|
3
|
+
|
|
4
|
+
[all]
|
|
1
5
|
psycopg2
|
|
2
6
|
mysql-connector-python
|
|
3
7
|
pyodbc
|
|
4
8
|
cx_Oracle
|
|
5
|
-
python-dotenv
|
|
6
9
|
|
|
7
10
|
[mysql]
|
|
8
11
|
mysql-connector-python
|
|
@@ -13,7 +16,5 @@ cx_Oracle
|
|
|
13
16
|
[postgres]
|
|
14
17
|
psycopg2
|
|
15
18
|
|
|
16
|
-
[sqlite]
|
|
17
|
-
|
|
18
19
|
[sqlserver]
|
|
19
20
|
pyodbc
|