SQLite-signup 1.0.0__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.
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module for managing user data persistence using SQLite.
|
|
3
|
+
|
|
4
|
+
This module provides a robust interface for performing CRUD operations on a
|
|
5
|
+
user database, ensuring schema integrity and connection safety through
|
|
6
|
+
context management.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import sqlite3
|
|
10
|
+
from contextlib import contextmanager
|
|
11
|
+
|
|
12
|
+
DATABASE_NAME = 'users_database.db'
|
|
13
|
+
|
|
14
|
+
@contextmanager
|
|
15
|
+
def get_db_connection():
|
|
16
|
+
"""
|
|
17
|
+
Provides a managed connection to the SQLite database.
|
|
18
|
+
|
|
19
|
+
Yields:
|
|
20
|
+
sqlite3.Connection: An active database connection object.
|
|
21
|
+
|
|
22
|
+
Notes:
|
|
23
|
+
Ensures that the connection is automatically closed after the block
|
|
24
|
+
execution, preventing memory leaks and dangling file handles.
|
|
25
|
+
"""
|
|
26
|
+
conn = sqlite3.connect(DATABASE_NAME)
|
|
27
|
+
try:
|
|
28
|
+
yield conn
|
|
29
|
+
finally:
|
|
30
|
+
conn.close()
|
|
31
|
+
|
|
32
|
+
def initialize_database():
|
|
33
|
+
"""
|
|
34
|
+
Ensures the required schema exists in the database.
|
|
35
|
+
|
|
36
|
+
This function acts as a safety check to guarantee the table structure is
|
|
37
|
+
present before any data operations occur, eliminating the need for
|
|
38
|
+
external migration scripts in simple deployments.
|
|
39
|
+
"""
|
|
40
|
+
with get_db_connection() as conn:
|
|
41
|
+
conn.execute('''
|
|
42
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
43
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
44
|
+
first_name TEXT,
|
|
45
|
+
last_name TEXT,
|
|
46
|
+
phone TEXT,
|
|
47
|
+
email TEXT
|
|
48
|
+
)
|
|
49
|
+
''')
|
|
50
|
+
conn.commit()
|
|
51
|
+
|
|
52
|
+
def register_user(first_name: str, last_name: str, phone: str, email: str) -> bool:
|
|
53
|
+
"""
|
|
54
|
+
Persists a new user record into the database.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
first_name (str): User's first name.
|
|
58
|
+
last_name (str): User's last name.
|
|
59
|
+
phone (str): Contact phone number.
|
|
60
|
+
email (str): Contact email address.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
bool: True if insertion was successful, False otherwise.
|
|
64
|
+
"""
|
|
65
|
+
try:
|
|
66
|
+
initialize_database()
|
|
67
|
+
with get_db_connection() as conn:
|
|
68
|
+
conn.execute(
|
|
69
|
+
"INSERT INTO users (first_name, last_name, phone, email) VALUES (?, ?, ?, ?)",
|
|
70
|
+
(first_name, last_name, phone, email)
|
|
71
|
+
)
|
|
72
|
+
conn.commit()
|
|
73
|
+
return True
|
|
74
|
+
except sqlite3.Error as e:
|
|
75
|
+
print(f"Database error during registration: {e}")
|
|
76
|
+
return False
|
|
77
|
+
|
|
78
|
+
def delete_user(user_id: int) -> bool:
|
|
79
|
+
"""
|
|
80
|
+
Removes a user by ID and re-indexes the table to maintain sequential IDs.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
user_id (int): The primary key of the user to remove.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
bool: True if the user was found and deleted, False otherwise.
|
|
87
|
+
"""
|
|
88
|
+
try:
|
|
89
|
+
with get_db_connection() as conn:
|
|
90
|
+
cursor = conn.cursor()
|
|
91
|
+
cursor.execute("DELETE FROM users WHERE id = ?", (user_id,))
|
|
92
|
+
|
|
93
|
+
# Verify if the target ID existed before proceeding with re-indexing
|
|
94
|
+
if cursor.rowcount == 0:
|
|
95
|
+
return False
|
|
96
|
+
|
|
97
|
+
# Cache remaining records to reconstruct the table without gaps
|
|
98
|
+
remaining_users = cursor.execute(
|
|
99
|
+
"SELECT first_name, last_name, phone, email FROM users"
|
|
100
|
+
).fetchall()
|
|
101
|
+
|
|
102
|
+
# Clear existing data and reset the auto-increment counter
|
|
103
|
+
cursor.execute("DELETE FROM users")
|
|
104
|
+
cursor.execute("DELETE FROM sqlite_sequence WHERE name='users'")
|
|
105
|
+
|
|
106
|
+
# Re-insert records to ensure primary keys remain contiguous
|
|
107
|
+
cursor.executemany(
|
|
108
|
+
"INSERT INTO users (first_name, last_name, phone, email) VALUES (?, ?, ?, ?)",
|
|
109
|
+
remaining_users
|
|
110
|
+
)
|
|
111
|
+
conn.commit()
|
|
112
|
+
return True
|
|
113
|
+
except sqlite3.Error as e:
|
|
114
|
+
print(f"Database error during deletion: {e}")
|
|
115
|
+
return False
|
|
116
|
+
|
|
117
|
+
def get_all_users() -> list:
|
|
118
|
+
"""
|
|
119
|
+
Retrieves all user records currently stored in the database.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
list: A list of tuples containing user data, or an empty list on failure.
|
|
123
|
+
"""
|
|
124
|
+
try:
|
|
125
|
+
with get_db_connection() as conn:
|
|
126
|
+
cursor = conn.cursor()
|
|
127
|
+
cursor.execute("SELECT * FROM users")
|
|
128
|
+
return cursor.fetchall()
|
|
129
|
+
except sqlite3.Error as e:
|
|
130
|
+
print(f"Database error during retrieval: {e}")
|
|
131
|
+
return []
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
SQLite_signup/__init__.py,sha256=IUWFJWDReaVs9RjOM2XlxjlWH3210ASOvJ3qyCpAls0,220
|
|
2
|
+
SQLite_signup/database.py,sha256=aJqsrb2_vgWyt6FvYfCCx-48XKOuah3piIiy8tnQa_o,4343
|
|
3
|
+
sqlite_signup-1.0.0.dist-info/METADATA,sha256=Jux5G76OU4_abZ7UU9UgYUPEzzLWFslOK4pDjTRSqzw,193
|
|
4
|
+
sqlite_signup-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
sqlite_signup-1.0.0.dist-info/top_level.txt,sha256=6ro-6HZrOKOBUynxvc3MgzqYL71SwnpE317XpJIryZE,14
|
|
6
|
+
sqlite_signup-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
SQLite_signup
|