SQLPyHelper 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl
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/__init__.py +2 -0
- sqlpyhelper/db_helper.py +21 -14
- {sqlpyhelper-0.1.0.dist-info → sqlpyhelper-0.1.2.dist-info}/METADATA +30 -5
- sqlpyhelper-0.1.2.dist-info/RECORD +7 -0
- sqlpyhelper-0.1.0.dist-info/RECORD +0 -7
- {sqlpyhelper-0.1.0.dist-info → sqlpyhelper-0.1.2.dist-info}/WHEEL +0 -0
- {sqlpyhelper-0.1.0.dist-info → sqlpyhelper-0.1.2.dist-info}/licenses/LICENSE +0 -0
- {sqlpyhelper-0.1.0.dist-info → sqlpyhelper-0.1.2.dist-info}/top_level.txt +0 -0
sqlpyhelper/__init__.py
CHANGED
sqlpyhelper/db_helper.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import sqlite3
|
|
2
|
-
import psycopg2
|
|
3
|
-
import mysql.connector
|
|
4
|
-
import pyodbc
|
|
5
|
-
import cx_Oracle
|
|
6
1
|
import csv
|
|
7
|
-
from psycopg2 import pool
|
|
8
2
|
from dotenv import load_dotenv
|
|
9
3
|
import os
|
|
10
4
|
|
|
11
5
|
load_dotenv() # Load environment variables from .env file
|
|
12
6
|
|
|
13
7
|
|
|
8
|
+
def log_query(query):
|
|
9
|
+
"""Logs queries for debugging purposes."""
|
|
10
|
+
with open("query_log.txt", "a") as f:
|
|
11
|
+
f.write(query + "\n")
|
|
12
|
+
|
|
13
|
+
|
|
14
14
|
class SQLPyHelper:
|
|
15
15
|
def __init__(self):
|
|
16
16
|
self.db_type = os.getenv("DB_TYPE").lower()
|
|
@@ -20,19 +20,25 @@ class SQLPyHelper:
|
|
|
20
20
|
self.database = os.getenv("DB_NAME")
|
|
21
21
|
self.driver = os.getenv("DB_DRIVER")
|
|
22
22
|
self.oracle_sid = os.getenv("ORACLE_SID")
|
|
23
|
+
self.pool = None
|
|
23
24
|
|
|
24
25
|
if self.db_type == "sqlite":
|
|
26
|
+
import sqlite3
|
|
25
27
|
self.connection = sqlite3.connect(self.database)
|
|
26
28
|
elif self.db_type == "postgres":
|
|
29
|
+
import psycopg2
|
|
27
30
|
self.connection = psycopg2.connect(host=self.host, user=self.user,
|
|
28
31
|
password=self.password, dbname=self.database)
|
|
29
32
|
elif self.db_type == "mysql":
|
|
33
|
+
import mysql.connector
|
|
30
34
|
self.connection = mysql.connector.connect(host=self.host, user=self.user,
|
|
31
35
|
password=self.password, database=self.database)
|
|
32
36
|
elif self.db_type == "sqlserver":
|
|
37
|
+
import pyodbc
|
|
33
38
|
self.connection = pyodbc.connect(f"DRIVER={self.driver};SERVER={self.host};DATABASE={self.database};"
|
|
34
39
|
f"UID={self.user};PWD={self.password}")
|
|
35
40
|
elif self.db_type == "oracle":
|
|
41
|
+
import cx_Oracle
|
|
36
42
|
oracle_port = os.getenv("ORACLE_DB_PORT", "1521") # Default to 1521 if not set
|
|
37
43
|
dsn = cx_Oracle.makedsn(self.host, oracle_port, self.oracle_sid)
|
|
38
44
|
self.connection = cx_Oracle.connect(self.user, self.password, dsn)
|
|
@@ -52,10 +58,18 @@ class SQLPyHelper:
|
|
|
52
58
|
except Exception as e:
|
|
53
59
|
print(f"Error executing query: {e}")
|
|
54
60
|
|
|
61
|
+
def fetch_one(self):
|
|
62
|
+
return self.cursor.fetchone()
|
|
63
|
+
|
|
55
64
|
def fetch_all(self):
|
|
56
65
|
"""Fetches all rows from the last executed query"""
|
|
57
66
|
return self.cursor.fetchall()
|
|
58
67
|
|
|
68
|
+
def fetch_by_param(self, table_name, column_name, value):
|
|
69
|
+
query = f"SELECT * FROM {table_name} WHERE {column_name} = %s"
|
|
70
|
+
self.cursor.execute(query, (value,))
|
|
71
|
+
return self.cursor.fetchall() # Fetch matching rows
|
|
72
|
+
|
|
59
73
|
def close(self):
|
|
60
74
|
"""Closes the connection"""
|
|
61
75
|
self.cursor.close()
|
|
@@ -84,11 +98,6 @@ class SQLPyHelper:
|
|
|
84
98
|
self.cursor.executemany(query, values)
|
|
85
99
|
self.connection.commit()
|
|
86
100
|
|
|
87
|
-
def log_query(self, query):
|
|
88
|
-
"""Logs queries for debugging purposes."""
|
|
89
|
-
with open("query_log.txt", "a") as f:
|
|
90
|
-
f.write(query + "\n")
|
|
91
|
-
|
|
92
101
|
def backup_table(self, table_name, backup_file):
|
|
93
102
|
"""
|
|
94
103
|
Exports table data into a CSV file.
|
|
@@ -110,6 +119,7 @@ class SQLPyHelper:
|
|
|
110
119
|
Example:
|
|
111
120
|
setup_postgres_pool(min_conn=2, max_conn=10)
|
|
112
121
|
"""
|
|
122
|
+
from psycopg2 import pool
|
|
113
123
|
self.pool = pool.SimpleConnectionPool(min_conn, max_conn,
|
|
114
124
|
host=self.host,
|
|
115
125
|
user=self.user,
|
|
@@ -123,6 +133,3 @@ class SQLPyHelper:
|
|
|
123
133
|
def return_connection_to_pool(self, conn):
|
|
124
134
|
"""Returns a connection back to the pool."""
|
|
125
135
|
self.pool.putconn(conn)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SQLPyHelper
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A simple SQL database helper package for Python.
|
|
5
5
|
Author: Adebayo Olaonipekun
|
|
6
6
|
Author-email: pekunmi@live.com
|
|
7
7
|
Classifier: Programming Language :: Python :: 3
|
|
8
|
-
Classifier:
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
9
11
|
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
13
|
Requires-Python: >=3.8
|
|
11
14
|
Description-Content-Type: text/markdown
|
|
12
15
|
License-File: LICENSE
|
|
@@ -15,12 +18,22 @@ Requires-Dist: mysql-connector-python
|
|
|
15
18
|
Requires-Dist: pyodbc
|
|
16
19
|
Requires-Dist: cx_Oracle
|
|
17
20
|
Requires-Dist: python-dotenv
|
|
21
|
+
Provides-Extra: mysql
|
|
22
|
+
Requires-Dist: mysql-connector-python; extra == "mysql"
|
|
23
|
+
Provides-Extra: postgres
|
|
24
|
+
Requires-Dist: psycopg2; extra == "postgres"
|
|
25
|
+
Provides-Extra: oracle
|
|
26
|
+
Requires-Dist: cx_Oracle; extra == "oracle"
|
|
27
|
+
Provides-Extra: sqlserver
|
|
28
|
+
Requires-Dist: pyodbc; extra == "sqlserver"
|
|
29
|
+
Provides-Extra: sqlite
|
|
18
30
|
Dynamic: author
|
|
19
31
|
Dynamic: author-email
|
|
20
32
|
Dynamic: classifier
|
|
21
33
|
Dynamic: description
|
|
22
34
|
Dynamic: description-content-type
|
|
23
35
|
Dynamic: license-file
|
|
36
|
+
Dynamic: provides-extra
|
|
24
37
|
Dynamic: requires-dist
|
|
25
38
|
Dynamic: requires-python
|
|
26
39
|
Dynamic: summary
|
|
@@ -54,9 +67,12 @@ A Python library for simplified database interactions across **SQLite, PostgreSQ
|
|
|
54
67
|
|
|
55
68
|
---
|
|
56
69
|
## 📦 Installation
|
|
70
|
+
#### Install via PyPI:
|
|
57
71
|
```sh
|
|
58
72
|
pip install sqlpyhelper
|
|
59
73
|
```
|
|
74
|
+
📌 Package on PyPI: [SQLPyHelper on PyPI](https://pypi.org/project/SQLPyHelper/)
|
|
75
|
+
|
|
60
76
|
Or, if working from source:
|
|
61
77
|
```sh
|
|
62
78
|
git clone https://github.com/adebayopeter/sqlpyhelper.git
|
|
@@ -75,11 +91,10 @@ DB_TYPE=postgres
|
|
|
75
91
|
DB_HOST=localhost
|
|
76
92
|
DB_USER=your_user
|
|
77
93
|
DB_PASSWORD=your_secure_password
|
|
78
|
-
DB_NAME=
|
|
79
|
-
DB_PORT=5432
|
|
94
|
+
DB_NAME=database_name
|
|
80
95
|
DB_DRIVER={ODBC Driver 17 for SQL Server}
|
|
81
96
|
ORACLE_SID=XE
|
|
82
|
-
|
|
97
|
+
ORACLE_DB_PORT=1521
|
|
83
98
|
```
|
|
84
99
|
### Loading `.env` in Code
|
|
85
100
|
```pycon
|
|
@@ -124,6 +139,16 @@ db.execute_query("SELECT * FROM customers")
|
|
|
124
139
|
print(db.fetch_all())
|
|
125
140
|
db.close()
|
|
126
141
|
```
|
|
142
|
+
```pycon
|
|
143
|
+
db = SQLPyHelper()
|
|
144
|
+
|
|
145
|
+
# Fetch rows where customer_id = 3
|
|
146
|
+
customers = db.fetch_by_param("customers", "id", 3)
|
|
147
|
+
print(customers)
|
|
148
|
+
|
|
149
|
+
db.close()
|
|
150
|
+
```
|
|
151
|
+
|
|
127
152
|
### SQL Server Example
|
|
128
153
|
```pycon
|
|
129
154
|
db = SQLPyHelper()
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
sqlpyhelper/__init__.py,sha256=kJA4mgypZmLd3SnwwTNkVwB0yIJ8PKP4h9cjOCIPfTA,54
|
|
2
|
+
sqlpyhelper/db_helper.py,sha256=TT93q3f1EVjkgUSXA4iXCy9Uw5gdGuRbqH-Wl-8OjeI,5269
|
|
3
|
+
sqlpyhelper-0.1.2.dist-info/licenses/LICENSE,sha256=9XzXxZ_8mWFM9-2TlqyE3L69zvRf4VPY_xIzSj5iU-g,1076
|
|
4
|
+
sqlpyhelper-0.1.2.dist-info/METADATA,sha256=ATC92p-IcBVUFEBHKXqqMBkYC524FsdQpxDVBmhQ3JI,5954
|
|
5
|
+
sqlpyhelper-0.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
+
sqlpyhelper-0.1.2.dist-info/top_level.txt,sha256=FrLqTmqTGDa8jHnnf2ZVkYO-gFvLXX9QonpUCE6wKGs,12
|
|
7
|
+
sqlpyhelper-0.1.2.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
sqlpyhelper/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
sqlpyhelper/db_helper.py,sha256=9z2WjA7tQaNZ1fd1TYZZAooJS2N8szescOtTMNDTCgE,4896
|
|
3
|
-
sqlpyhelper-0.1.0.dist-info/licenses/LICENSE,sha256=9XzXxZ_8mWFM9-2TlqyE3L69zvRf4VPY_xIzSj5iU-g,1076
|
|
4
|
-
sqlpyhelper-0.1.0.dist-info/METADATA,sha256=YTa0XzA-AliUNOIlRKZBolekfrTcXaVIpS6NRDea8dI,5211
|
|
5
|
-
sqlpyhelper-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
-
sqlpyhelper-0.1.0.dist-info/top_level.txt,sha256=FrLqTmqTGDa8jHnnf2ZVkYO-gFvLXX9QonpUCE6wKGs,12
|
|
7
|
-
sqlpyhelper-0.1.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|