SQLite-signup 1.0.0__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.
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: SQLite_signup
3
+ Version: 1.0.0
4
+ Summary: Simple SQLite user management library
5
+ Author: Your Name
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "SQLite_signup"
7
+ version = "1.0.0"
8
+ description = "Simple SQLite user management library"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ authors = [
12
+ {name = "Your Name"}
13
+ ]
14
+
15
+ [tool.setuptools]
16
+ package-dir = {"" = "src"}
17
+
18
+ [tool.setuptools.packages.find]
19
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,13 @@
1
+ from .database import (
2
+ register_user,
3
+ delete_user,
4
+ get_all_users,
5
+ initialize_database,
6
+ )
7
+
8
+ __all__ = [
9
+ "register_user",
10
+ "delete_user",
11
+ "get_all_users",
12
+ "initialize_database",
13
+ ]
@@ -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,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: SQLite_signup
3
+ Version: 1.0.0
4
+ Summary: Simple SQLite user management library
5
+ Author: Your Name
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
@@ -0,0 +1,7 @@
1
+ pyproject.toml
2
+ src/SQLite_signup/__init__.py
3
+ src/SQLite_signup/database.py
4
+ src/SQLite_signup.egg-info/PKG-INFO
5
+ src/SQLite_signup.egg-info/SOURCES.txt
6
+ src/SQLite_signup.egg-info/dependency_links.txt
7
+ src/SQLite_signup.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ SQLite_signup